Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use structured clone for transactions #4456

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/builder/app/shared/sync-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export class ImmerhinSyncObject implements SyncObject {
}
setState(state: Map<string, unknown>) {
for (const [namespace, $store] of this.store.containers) {
// immer is not able to work with Map instances from another realm
// use clone to recreate data with current realm classes
// Immer cannot handle Map instances from another realm.
// Use `clone` to recreate the data with the current realm's classes.
// This works because the structured clone algorithm skips prototype chains; classes must be defined in both realms.
$store.set(structuredClone(state.get(namespace)));
}
}
Expand Down Expand Up @@ -86,7 +87,10 @@ export class NanostoresSyncObject implements SyncObject {
}
addTransaction(transaction: Transaction) {
this.operation = "add";
this.store.set(transaction.payload);
// `instanceof` checks do not work with instances like Map, File, etc., from another realm.
// Use `clone` to recreate the data with the current realm's classes.
// This works because the structured clone algorithm skips prototype chains; classes must be defined in both realms.
this.store.set(structuredClone(transaction.payload));
this.operation = "local";
}
revertTransaction(_transaction: RevertedTransaction) {
Expand Down