Skip to content

Commit

Permalink
Refactor commit methods to consolidate transaction handling and impro…
Browse files Browse the repository at this point in the history
…ve clarity
  • Loading branch information
77it committed Jan 6, 2025
1 parent 6e6bf88 commit f200089
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
19 changes: 6 additions & 13 deletions src/engine/ledger/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,38 +215,31 @@ class Ledger {
//#region COMMIT methods
/**
* Commit the current transaction, if any.
* Commit means pushing the transaction to the ledger dump, then reset the current transaction.
* @throws {Error} If the transaction is not valid, not squared, etc.
*/
commit () {
if (this.#isLocked)
throw new Error('Ledger is locked');

if (this.#currentTransaction.length === 0) return;

// TODO validate trn: errore se non quadra transazione/unit, se il tipo non è un tipo riconosciuto, etc;

// convert this.#currentTransaction to SimObjectJsonDumpDto, then stringify
const simObjectJsonDumpDtoArray = this.#currentTransaction.map(simObject => simObjectToJsonDumpDto(simObject));
this.#appendTrnDump(JSON.stringify(simObjectJsonDumpDtoArray));

// reset the current transaction
this.#currentTransaction = [];
this.forceCommitWithoutValidation();
}

/**
* BEWARE: this method must be called only by the engine, then must not be exported to modules.
* Commit the current transaction without any validation
* Commit the current transaction without any validation.
* Commit means pushing the transaction to the ledger dump, then reset the current transaction.
*/
forceCommitWithoutValidation () {
if (this.#isLocked)
throw new Error('Ledger is locked');

if (this.#currentTransaction.length === 0) return;

// convert this.#currentTransaction to SimObjectJsonDumpDto, then stringify
const simObjectJsonDumpDtoArray = this.#currentTransaction.map(simObject => simObjectToJsonDumpDto(simObject));
this.#appendTrnDump(JSON.stringify(simObjectJsonDumpDtoArray));

// TODO switch on SimObjectDebugTypes_enum to call the right callbacks to append the debug info

// reset the current transaction
this.#currentTransaction = [];
}
Expand Down
5 changes: 3 additions & 2 deletions test/lib_test/sanitization_utils_a__sanitize()__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ t('test sanitize() - number type + validation', async () => {

t('test sanitize() - boolean type', async () => {
const t = S.BOOLEAN_TYPE;
assert.deepStrictEqual(false, s.sanitize({ value: false, sanitization: t }));
assert.deepStrictEqual(false, s.sanitize({ value: 'false', sanitization: t })); // string 'false' = false
assert.deepStrictEqual(false, s.sanitize({ value: 'FaLsE', sanitization: t })); // string 'false' = false
assert.deepStrictEqual(false, s.sanitize({ value: ' FaLsE ', sanitization: t })); // string 'false' = false
Expand All @@ -154,14 +155,14 @@ t('test sanitize() - boolean type', async () => {
assert.deepStrictEqual(false, s.sanitize({ value: null, sanitization: t }));
assert.deepStrictEqual(false, s.sanitize({ value: -0, sanitization: t }));
assert.deepStrictEqual(false, s.sanitize({ value: 0, sanitization: t }));

assert.deepStrictEqual(true, s.sanitize({ value: true, sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: 1, sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: -1, sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: 999, sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: 'abc', sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: new Date(2022, 11, 25), sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: new Date(NaN), sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: true, sanitization: t }));
assert.deepStrictEqual(false, s.sanitize({ value: false, sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: 'true', sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: 'TrUe', sanitization: t }));
assert.deepStrictEqual(true, s.sanitize({ value: ' TrUe ', sanitization: t }));
Expand Down

0 comments on commit f200089

Please sign in to comment.