Skip to content

Commit

Permalink
fix: deep cloning state and original drp (#296)
Browse files Browse the repository at this point in the history
Co-authored-by: Phạm Xuân Trung <[email protected]>
  • Loading branch information
JanLewDev and trungnotchung authored Dec 27, 2024
1 parent 4bce724 commit 89454ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"devDependencies": {
"@bufbuild/protobuf": "^2.0.0",
"benchmark": "^2.1.4",
"tsx": "4.19.1"
"tsx": "4.19.1",
"es-toolkit": "1.30.1"
},
"dependencies": {
"@ts-drp/logger": "^0.4.4"
Expand Down
13 changes: 4 additions & 9 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ObjectSet } from "./utils/objectSet.js";

export * as ObjectPb from "./proto/drp/object/v1/object_pb.js";
export * from "./hashgraph/index.js";
import { cloneDeep } from "es-toolkit";

export interface IACL {
isWriter: (peerId: string) => boolean;
Expand Down Expand Up @@ -96,10 +97,7 @@ export class DRPObject implements IDRPObject {
);
this.subscriptions = [];
this.states = new Map([[HashGraph.rootHash, { state: new Map() }]]);
this.originalDRP = Object.create(
Object.getPrototypeOf(drp),
Object.getOwnPropertyDescriptors(structuredClone(drp)),
);
this.originalDRP = cloneDeep(drp);
this.vertices = this.hashGraph.getAllVertices();
}

Expand Down Expand Up @@ -202,17 +200,14 @@ export class DRPObject implements IDRPObject {
? []
: this.hashGraph.linearizeOperations(lca, subgraph);

const drp = Object.create(
Object.getPrototypeOf(this.originalDRP),
Object.getOwnPropertyDescriptors(structuredClone(this.originalDRP)),
) as DRP;
const drp = cloneDeep(this.originalDRP);

const fetchedState = this.states.get(lca);
if (!fetchedState) {
throw new Error("State is undefined");
}

const state = structuredClone(fetchedState);
const state = cloneDeep(fetchedState);

for (const [key, value] of state.state) {
drp[key] = value;
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 89454ae

Please sign in to comment.