Skip to content

Commit 07af2a4

Browse files
committed
refer to memoization methods by name rather than number
1 parent 8583aa4 commit 07af2a4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/utils/src/memo.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33

4-
export type MemoFunc = [(obj: any) => boolean, (obj: any) => void];
4+
export type MemoFunc = [
5+
// memoize
6+
(obj: any) => boolean,
7+
// unmemoize
8+
(obj: any) => void,
9+
];
510

611
/**
712
* Helper to decycle json objects

packages/utils/src/object.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ function makeSerializable<T>(value: T, key?: any): T | string {
310310
*/
311311
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
312312
export function walk(key: string, value: any, depth: number = +Infinity, memo: MemoFunc = memoBuilder()): any {
313+
const [memoize, unmemoize] = memo;
314+
313315
// If we reach the maximum depth, serialize whatever is left
314316
if (depth === 0) {
315317
return serializeValue(value);
@@ -338,7 +340,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M
338340
const acc: { [key: string]: any } = Array.isArray(value) ? [] : {};
339341

340342
// If we already walked that branch, bail out, as it's circular reference
341-
if (memo[0](value)) {
343+
if (memoize(value)) {
342344
return '[Circular ~]';
343345
}
344346

@@ -354,7 +356,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M
354356
}
355357

356358
// Once walked through all the branches, remove the parent from memo storage
357-
memo[1](value);
359+
unmemoize(value);
358360

359361
// Return accumulated values
360362
return acc;

0 commit comments

Comments
 (0)