Skip to content

Commit

Permalink
feat: add force serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoravida committed Sep 1, 2022
1 parent a86295d commit c24aa4d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/recoil-toolkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recoil-toolkit",
"version": "0.2.7",
"version": "0.2.9",
"description": "recoil-toolkit",
"main": "./build/es5/index.js",
"module": "./build/es6/index.js",
Expand Down
23 changes: 20 additions & 3 deletions packages/recoil-toolkit/src/devtools/RecoilDevTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ import { useRecoilSnapshot } from 'recoil';
import { RecoilToolkitDevTools } from './consts';
import { getSnapshotState } from './getSnapshotState';

export const RecoilDevTools: React.FC<{ enableConsole?: boolean }> = ({ enableConsole }) => {
const stringifyCircularJSON = obj => {
const seen = new WeakSet();
return JSON.stringify(obj, (k, v) => {
if (v !== null && typeof v === 'object') {
if (seen.has(v)) return '[Circular]';
seen.add(v);
}
return v;
});
};

export const RecoilDevTools: React.FC<{ enableConsole?: boolean; forceSerialize?: boolean }> = ({
enableConsole,
forceSerialize,
}) => {
const snapshot = useRecoilSnapshot();

useEffect(() => {
Expand All @@ -17,10 +31,13 @@ export const RecoilDevTools: React.FC<{ enableConsole?: boolean }> = ({ enableCo
useEffect(() => {
const snapState = getSnapshotState(snapshot);
window.postMessage(
{ type: RecoilToolkitDevTools.eventMessageFromPage, data: snapState },
{
type: RecoilToolkitDevTools.eventMessageFromPage,
data: forceSerialize ? JSON.parse(stringifyCircularJSON(snapState)) : snapState,
},
'*',
);
}, [snapshot]);
}, [snapshot]); //eslint-disable-line

/** Listen messages from DevTools **/
useEffect(() => {
Expand Down

0 comments on commit c24aa4d

Please sign in to comment.