Skip to content

Commit

Permalink
feat: use structured clone for sent state
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jan 21, 2023
1 parent 37b691a commit c46ba17
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"broadcast-channel": "^4.20.2",
"devalue": "^4.2.2",
"realistic-structured-clone": "^3.0.0",
"vue-demi": "^0.13.11"
},
"devDependencies": {
Expand Down
59 changes: 56 additions & 3 deletions pnpm-lock.yaml

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

6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { watch } from 'vue-demi';
import type { MethodType } from 'broadcast-channel';
import { BroadcastChannel as BroadcastChannelImpl } from 'broadcast-channel';
import type { PiniaPluginContext, Store } from 'pinia';
import * as devalue from 'devalue';
import structuredClone from 'realistic-structured-clone';

/**
* Share state across browser tabs.
Expand Down Expand Up @@ -43,7 +43,7 @@ export function share<T extends Store, K extends keyof T['$state']>(
timestamp = Date.now();
channel.postMessage({
timestamp,
state: devalue.parse(devalue.stringify(state)),
state: structuredClone(state),
});
}
externalUpdate = false;
Expand All @@ -55,7 +55,7 @@ export function share<T extends Store, K extends keyof T['$state']>(
if (evt === undefined) {
channel.postMessage({
timestamp,
state: devalue.parse(devalue.stringify(store[key])),
state: structuredClone(store[key]),
});
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"noEmit": true
},
"include": ["./src"]
"include": ["./src", "types.d.ts"]
}
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'realistic-structured-clone' {
const structuredClone: <T>(obj: T) => T

export default structuredClone
}

0 comments on commit c46ba17

Please sign in to comment.