11import SuperJSON from 'superjson'
22import {
33 FLYTRAP_CIRCULAR ,
4+ FLYTRAP_CLASS ,
45 FLYTRAP_DOM_EVENT ,
56 FLYTRAP_FUNCTION ,
67 FLYTRAP_HEADERS ,
@@ -17,7 +18,7 @@ import {
1718} from './types'
1819import { deepEqual } from 'fast-equals'
1920import { decrypt } from './encryption'
20- import { copy } from 'copy-anything'
21+ // import { copy } from 'copy-anything'
2122
2223export function superJsonRegisterCustom ( superJsonInstance : typeof SuperJSON ) {
2324 // Fetch API classes
@@ -46,17 +47,17 @@ export function superJsonRegisterCustom(superJsonInstance: typeof SuperJSON) {
4647 'Request'
4748 )
4849
49- // handle functions
50+ // Functions
5051 superJsonInstance . registerCustom < any , string > (
5152 {
5253 isApplicable : ( v ) : v is Function => typeof v === 'function' ,
5354 serialize : ( ) => FLYTRAP_FUNCTION ,
5455 deserialize : ( ) => FLYTRAP_FUNCTION
5556 } ,
56- 'functions '
57+ 'Functions '
5758 )
5859
59- // handle DOM events
60+ // DOM Events
6061 superJsonInstance . registerCustom < any , string > (
6162 {
6263 isApplicable : ( v ) : v is Event => {
@@ -65,7 +66,29 @@ export function superJsonRegisterCustom(superJsonInstance: typeof SuperJSON) {
6566 serialize : ( ) => FLYTRAP_DOM_EVENT ,
6667 deserialize : ( ) => FLYTRAP_DOM_EVENT
6768 } ,
68- 'dom events'
69+ 'DOM Events'
70+ )
71+
72+ // Classes
73+ superJsonInstance . registerCustom < any , string > (
74+ {
75+ isApplicable : ( v ) : v is Request => {
76+ return isClassInstance ( v )
77+ } ,
78+ serialize : ( ) => FLYTRAP_CLASS ,
79+ deserialize : ( ) => FLYTRAP_CLASS
80+ } ,
81+ 'Classes'
82+ )
83+ }
84+
85+ export function isClassInstance < T > ( obj : T ) : boolean {
86+ return (
87+ obj !== null &&
88+ typeof obj === 'object' &&
89+ ! ( obj instanceof Array ) &&
90+ obj . constructor &&
91+ obj . constructor !== Object
6992 )
7093}
7194
@@ -113,7 +136,6 @@ export function removeCircularDependencies<T>(obj: T, seenObjects = new Set()):
113136 // It's a circular reference
114137 // @ts -expect-error
115138 return FLYTRAP_CIRCULAR
116- // return null; // Or replace with some placeholder if needed
117139 }
118140
119141 // Keep track of this object so we don't process it again
@@ -137,16 +159,6 @@ export function removeCircularDependencies<T>(obj: T, seenObjects = new Set()):
137159 return newObj as T
138160}
139161
140- /*export function removeCircularDependencies<T>(obj: T): T {
141- superJsonRegisterCustom(SuperJSON)
142- const serialized = SuperJSON.serialize(obj)
143- if (serialized.meta?.referentialEqualities) {
144- delete serialized.meta.referentialEqualities
145- }
146-
147- return SuperJSON.deserialize(serialized)
148- }*/
149-
150162function _extract ( captures : ( CapturedCall | CapturedFunction ) [ ] , key : 'args' | 'output' = 'args' ) {
151163 const values = captures . reduce (
152164 ( acc , curr ) => [ ...acc , ...curr . invocations . map ( ( i ) => i [ key ] ) ] ,
@@ -171,7 +183,7 @@ export function extractArgs(captures: (CapturedCall | CapturedFunction)[]): any[
171183 return _extract ( captures , 'args' )
172184}
173185
174- export function extractOutputs ( captures : ( CapturedCall | CapturedFunction ) [ ] ) : any [ ] [ ] {
186+ export function extractOutputs ( captures : ( CapturedCall | CapturedFunction ) [ ] ) : any [ ] {
175187 return _extract ( captures , 'output' )
176188}
177189
@@ -188,7 +200,8 @@ export function addLinks(
188200 invocations : CaptureInvocation [ ] ,
189201 { args, outputs } : { args : any [ ] [ ] ; outputs : any [ ] }
190202) {
191- const invocationsCopy = copy ( invocations )
203+ // const invocationsCopy = copy(invocations)
204+ const invocationsCopy = invocations
192205
193206 for ( let i = 0 ; i < invocationsCopy . length ; i ++ ) {
194207 // Args
@@ -210,7 +223,8 @@ export function addLinksToCaptures(
210223 captures : ( CapturedCall | CapturedFunction ) [ ] ,
211224 { args, outputs } : { args : any [ ] [ ] ; outputs : any [ ] }
212225) {
213- const capturesCopy = copy ( captures )
226+ // const capturesCopy = copy(captures)
227+ const capturesCopy = captures
214228 for ( let i = 0 ; i < capturesCopy . length ; i ++ ) {
215229 // captures[i].invocations
216230 const linkedInvocations = addLinks ( capturesCopy [ i ] . invocations , { args, outputs } )
@@ -275,7 +289,8 @@ export function reviveLinks(
275289 invocations : CaptureInvocationWithLinks [ ] ,
276290 { args, outputs } : { args : any [ ] [ ] ; outputs : any [ ] }
277291) : CaptureInvocation [ ] {
278- const invocationsCopy = copy ( invocations )
292+ // const invocationsCopy = copy(invocations)
293+ const invocationsCopy = invocations
279294
280295 for ( let i = 0 ; i < invocationsCopy . length ; i ++ ) {
281296 // Revive args
@@ -289,3 +304,11 @@ export function reviveLinks(
289304
290305 return invocationsCopy as unknown as CaptureInvocation [ ]
291306}
307+
308+ export function processCaptures ( captures : ( CapturedCall | CapturedFunction ) [ ] ) {
309+ superJsonRegisterCustom ( SuperJSON )
310+ for ( let i = 0 ; i < captures . length ; i ++ ) {
311+ captures [ i ] = SuperJSON . deserialize ( SuperJSON . serialize ( captures [ i ] ) )
312+ }
313+ return captures
314+ }
0 commit comments