From b6e40e382259cabf54acb4955eb3d86df465e71e Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Wed, 5 Feb 2025 21:43:50 +0100 Subject: [PATCH] refactor: :recycle: make the function more readable --- packages/object/src/index.ts | 85 +++++++++++++++--------------------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/packages/object/src/index.ts b/packages/object/src/index.ts index 3714857e..1fd7d764 100644 --- a/packages/object/src/index.ts +++ b/packages/object/src/index.ts @@ -36,6 +36,13 @@ export interface DRPObjectConfig { finality_config?: FinalityConfig; } +export interface callFnResult { + drp: DRP; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + appliedOperationResult: any; + isACL: boolean; +} + export let log: Logger; export class DRPObject implements ObjectPb.DRPObjectBase { @@ -181,76 +188,54 @@ export class DRPObject implements ObjectPb.DRPObjectBase { // eslint-disable-next-line @typescript-eslint/no-explicit-any args: any, drpType: DrpType - ): { - drp: DRP; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - appliedOperationResult: any; - isACL: boolean; - } { + ): callFnResult { if (!this.hashGraph) { throw new Error("Hashgraph is undefined"); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let preOperationDRP: any; - let isACL = false; - if (drpType === DrpType.ACL) { - isACL = true; - preOperationDRP = this._computeObjectACL(this.hashGraph.getFrontier()); - } else { - preOperationDRP = this._computeDRP(this.hashGraph.getFrontier()); - } + + const isACL = drpType === DrpType.ACL; + const computeFn = isACL ? this._computeObjectACL : this._computeDRP; + const frontier = this.hashGraph.getFrontier(); + const preOperationDRP = computeFn(frontier); + const drp = cloneDeep(preOperationDRP); - let appliedOperationResult = undefined; + const appliedOperationResult: callFnResult = { isACL, drp, appliedOperationResult: undefined }; try { - appliedOperationResult = this._applyOperation(drp, { opType: fn, value: args, drpType }); + appliedOperationResult.appliedOperationResult = this._applyOperation(drp, { + opType: fn, + value: args, + drpType, + }); } catch (e) { log.error(`::drpObject::callFn: ${e}`); - return { - drp, - appliedOperationResult, - isACL, - }; - } - - let stateChanged = false; - for (const key of Object.keys(preOperationDRP)) { - if (!deepEqual(preOperationDRP[key], drp[key])) { - stateChanged = true; - break; - } + return appliedOperationResult; } + const stateChanged = Object.keys(preOperationDRP).some( + (key) => !deepEqual(preOperationDRP[key], drp[key]) + ); if (!stateChanged) { - return { - drp, - appliedOperationResult, - isACL, - }; + return appliedOperationResult; } const vertexTimestamp = Date.now(); const vertexOperation = { drpType: drpType, opType: fn, value: args }; - const vertexDependencies = this.hashGraph.getFrontier(); const vertex = ObjectPb.Vertex.create({ - hash: computeHash(this.peerId, vertexOperation, vertexDependencies, vertexTimestamp), + hash: computeHash(this.peerId, vertexOperation, frontier, vertexTimestamp), peerId: this.peerId, operation: vertexOperation, - dependencies: vertexDependencies, + dependencies: frontier, timestamp: vertexTimestamp, }); - this.hashGraph.addToFrontier(vertex); + this.hashGraph.addToFrontier(vertex); this._setState(vertex, this._getDRPState(drp)); this._initializeFinalityState(vertex.hash); this.vertices.push(vertex); this._notify("callFn", [vertex]); - return { - drp, - appliedOperationResult, - isACL, - }; + return appliedOperationResult; } /* Merges the vertices into the hashgraph @@ -372,11 +357,11 @@ export class DRPObject implements ObjectPb.DRPObjectBase { } // compute the DRP based on all dependencies of the current vertex using partial linearization - private _computeDRP( + private _computeDRP = ( vertexDependencies: Hash[], preCompute?: LcaAndOperations, vertexOperation?: Operation - ): DRP { + ): DRP => { if (!this.drp || !this.originalDRP) { throw new Error("DRP is undefined"); } @@ -406,13 +391,13 @@ export class DRPObject implements ObjectPb.DRPObjectBase { } return drp; - } + }; - private _computeObjectACL( + private _computeObjectACL = ( vertexDependencies: Hash[], preCompute?: LcaAndOperations, vertexOperation?: Operation - ): DRP { + ): DRP => { if (!this.acl || !this.originalObjectACL) { throw new Error("ObjectACL is undefined"); } @@ -441,7 +426,7 @@ export class DRPObject implements ObjectPb.DRPObjectBase { } return acl; - } + }; private computeLCA(vertexDependencies: string[]) { if (!this.hashGraph) {