11import type { Primitive } from '../types-hoist' ;
22
33import { isSyntheticEvent , isVueViewModel } from './is' ;
4- import type { MemoFunc } from './memo' ;
5- import { memoBuilder } from './memo' ;
64import { convertToPlainObject } from './object' ;
75import { getFunctionName } from './stacktrace' ;
86
@@ -13,6 +11,13 @@ type Prototype = { constructor: (...args: unknown[]) => unknown };
1311// might be arrays.
1412type ObjOrArray < T > = { [ key : string ] : T } ;
1513
14+ type MemoFunc = [
15+ // memoize
16+ ( obj : object ) => boolean ,
17+ // unmemoize
18+ ( obj : object ) => void ,
19+ ] ;
20+
1621/**
1722 * Recursively normalizes the given object.
1823 *
@@ -74,8 +79,7 @@ function visit(
7479 value : unknown ,
7580 depth : number = + Infinity ,
7681 maxProperties : number = + Infinity ,
77- // eslint-disable-next-line deprecation/deprecation
78- memo : MemoFunc = memoBuilder ( ) ,
82+ memo = memoBuilder ( ) ,
7983) : Primitive | ObjOrArray < unknown > {
8084 const [ memoize , unmemoize ] = memo ;
8185
@@ -304,3 +308,22 @@ export function normalizeUrlToBase(url: string, basePath: string): string {
304308 . replace ( new RegExp ( `(file://)?/*${ escapedBase } /*` , 'ig' ) , 'app:///' )
305309 ) ;
306310}
311+
312+ /**
313+ * Helper to decycle json objects
314+ */
315+ function memoBuilder ( ) : MemoFunc {
316+ const inner = new WeakSet < object > ( ) ;
317+ function memoize ( obj : object ) : boolean {
318+ if ( inner . has ( obj ) ) {
319+ return true ;
320+ }
321+ inner . add ( obj ) ;
322+ return false ;
323+ }
324+
325+ function unmemoize ( obj : object ) : void {
326+ inner . delete ( obj ) ;
327+ }
328+ return [ memoize , unmemoize ] ;
329+ }
0 commit comments