Skip to content

Commit

Permalink
fix(core, plugin-history-sync): accept only serializable parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfromundefined authored Jul 9, 2024
1 parent 85f5638 commit 7df36f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .changeset/green-bulldogs-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@stackflow/plugin-history-sync": patch
"@stackflow/core": patch
---

accept only serializable parameters when making domain event
6 changes: 5 additions & 1 deletion core/src/event-utils/makeEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { DomainEvent } from "../event-types";
import type { BaseDomainEvent } from "../event-types/_base";
import { id, time } from "../utils";

function clone<T extends {}>(input: T): T {
return JSON.parse(JSON.stringify(input));
}

export function makeEvent<T extends DomainEvent["name"]>(
name: T,
parameters: Omit<
Expand All @@ -13,7 +17,7 @@ export function makeEvent<T extends DomainEvent["name"]>(
return {
id: id(),
eventDate: time(),
...parameters,
...clone(parameters),
name,
} as Extract<DomainEvent, { name: T }>;
}
27 changes: 6 additions & 21 deletions extensions/plugin-history-sync/src/historyState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@ interface SerializedState extends State {
_TAG: typeof STATE_TAG;
}

function clone<T>(input: T): T {
return JSON.parse(JSON.stringify(input));
}

function serializeStep(step: ActivityStep): ActivityStep {
return {
...step,
enteredBy:
"activityContext" in step.enteredBy
? {
...step.enteredBy,
activityContext: undefined,
}
: {
...step.enteredBy,
},
};
return clone(step);
}

function serializeActivity(activity: Activity): Activity {
return {
...activity,
context: undefined,
enteredBy: {
...activity.enteredBy,
activityContext: undefined,
},
steps: activity.steps.map(serializeStep),
};
return clone(activity);
}

function serializeState(state: State): SerializedState {
Expand Down

0 comments on commit 7df36f1

Please sign in to comment.