|
1 | 1 | import SuperJSON from 'superjson' |
2 | | -import { FLYTRAP_UNSERIALIZABLE_VALUE } from './constants' |
3 | | -import { ignoreCircularReferences } from './util' |
| 2 | +import { FLYTRAP_CIRCULAR, FLYTRAP_DOM_EVENT, FLYTRAP_FUNCTION } from './constants' |
| 3 | + |
| 4 | +function superJsonRegisterCustom() { |
| 5 | + // handle functions |
| 6 | + SuperJSON.registerCustom<any, string>( |
| 7 | + { |
| 8 | + isApplicable: (v): v is Function => typeof v === 'function', |
| 9 | + serialize: () => FLYTRAP_FUNCTION, |
| 10 | + deserialize: () => FLYTRAP_FUNCTION |
| 11 | + }, |
| 12 | + 'functions' |
| 13 | + ) |
| 14 | + |
| 15 | + // handle DOM events |
| 16 | + SuperJSON.registerCustom<any, string>( |
| 17 | + { |
| 18 | + isApplicable: (v): v is Event => { |
| 19 | + return typeof window !== 'undefined' ? v instanceof window.Event : false |
| 20 | + }, |
| 21 | + serialize: () => FLYTRAP_DOM_EVENT, |
| 22 | + deserialize: () => FLYTRAP_DOM_EVENT |
| 23 | + }, |
| 24 | + 'dom events' |
| 25 | + ) |
| 26 | +} |
4 | 27 |
|
5 | 28 | /** |
6 | 29 | * Stringifies an object, and removes all cyclical dependencies. When parsing, cyclical values become `null`. |
7 | 30 | * @param obj object to stringify |
8 | 31 | * @returns stringified object with cyclical dependencies removed |
9 | 32 | */ |
10 | 33 | export function stringify(obj: any): string { |
11 | | - // handle functions appropriately |
12 | | - SuperJSON.registerCustom<any, string>( |
13 | | - { |
14 | | - isApplicable: (v): v is Function => typeof v === 'function', |
15 | | - serialize: () => FLYTRAP_UNSERIALIZABLE_VALUE, |
16 | | - deserialize: () => FLYTRAP_UNSERIALIZABLE_VALUE |
17 | | - }, |
18 | | - 'unserializable functions' |
19 | | - ) |
| 34 | + superJsonRegisterCustom() |
20 | 35 |
|
21 | 36 | const serialized = SuperJSON.serialize(obj) |
22 | 37 | if (serialized.meta?.referentialEqualities) { |
23 | 38 | delete serialized.meta.referentialEqualities |
24 | 39 | } |
25 | 40 |
|
| 41 | + function ignoreCircularReferences() { |
| 42 | + const seen = new WeakSet() |
| 43 | + return (key: any, value: any) => { |
| 44 | + if (key.startsWith('_')) return // Don't compare React's internal props. |
| 45 | + if (typeof value === 'object' && value !== null) { |
| 46 | + if (seen.has(value)) return FLYTRAP_CIRCULAR |
| 47 | + seen.add(value) |
| 48 | + } |
| 49 | + return value |
| 50 | + } |
| 51 | + } |
| 52 | + |
26 | 53 | return JSON.stringify(serialized, ignoreCircularReferences()) |
27 | 54 | } |
28 | 55 |
|
29 | 56 | export function parse<T = unknown>(stringified: string): T { |
30 | | - // handle functions appropriately |
31 | | - SuperJSON.registerCustom<any, string>( |
32 | | - { |
33 | | - isApplicable: (v): v is Function => typeof v === 'function', |
34 | | - serialize: () => FLYTRAP_UNSERIALIZABLE_VALUE, |
35 | | - deserialize: () => FLYTRAP_UNSERIALIZABLE_VALUE |
36 | | - }, |
37 | | - 'unserializable functions' |
38 | | - ) |
39 | | - |
| 57 | + superJsonRegisterCustom() |
40 | 58 | return SuperJSON.parse<T>(stringified) |
41 | 59 | } |
0 commit comments