Skip to content

Commit 42f5a58

Browse files
authored
ref(utils): convert memo from class to function (#4283)
1 parent f9fb1d6 commit 42f5a58

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

packages/utils/src/memo.ts

+21-34
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,44 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
3-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
4-
/**
5-
* Memo class used for decycle json objects. Uses WeakSet if available otherwise array.
6-
*/
7-
export class Memo {
8-
/** Determines if WeakSet is available */
9-
private readonly _hasWeakSet: boolean;
10-
/** Either WeakSet or Array */
11-
private readonly _inner: any;
123

13-
public constructor() {
14-
this._hasWeakSet = typeof WeakSet === 'function';
15-
this._inner = this._hasWeakSet ? new WeakSet() : [];
16-
}
4+
export type MemoFunc = [(obj: any) => boolean, (obj: any) => void];
175

18-
/**
19-
* Sets obj to remember.
20-
* @param obj Object to remember
21-
*/
22-
public memoize(obj: any): boolean {
23-
if (this._hasWeakSet) {
24-
if (this._inner.has(obj)) {
6+
/**
7+
* Helper to decycle json objects
8+
*/
9+
export function memoBuilder(): MemoFunc {
10+
const hasWeakSet = typeof WeakSet === 'function';
11+
const inner: any = hasWeakSet ? new WeakSet() : [];
12+
function memoize(obj: any): boolean {
13+
if (hasWeakSet) {
14+
if (inner.has(obj)) {
2515
return true;
2616
}
27-
this._inner.add(obj);
17+
inner.add(obj);
2818
return false;
2919
}
3020
// eslint-disable-next-line @typescript-eslint/prefer-for-of
31-
for (let i = 0; i < this._inner.length; i++) {
32-
const value = this._inner[i];
21+
for (let i = 0; i < inner.length; i++) {
22+
const value = inner[i];
3323
if (value === obj) {
3424
return true;
3525
}
3626
}
37-
this._inner.push(obj);
27+
inner.push(obj);
3828
return false;
3929
}
4030

41-
/**
42-
* Removes object from internal storage.
43-
* @param obj Object to forget
44-
*/
45-
public unmemoize(obj: any): void {
46-
if (this._hasWeakSet) {
47-
this._inner.delete(obj);
31+
function unmemoize(obj: any): void {
32+
if (hasWeakSet) {
33+
inner.delete(obj);
4834
} else {
49-
for (let i = 0; i < this._inner.length; i++) {
50-
if (this._inner[i] === obj) {
51-
this._inner.splice(i, 1);
35+
for (let i = 0; i < inner.length; i++) {
36+
if (inner[i] === obj) {
37+
inner.splice(i, 1);
5238
break;
5339
}
5440
}
5541
}
5642
}
43+
return [memoize, unmemoize];
5744
}

packages/utils/src/object.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ExtendedError, WrappedFunction } from '@sentry/types';
44

55
import { htmlTreeAsString } from './browser';
66
import { isElement, isError, isEvent, isInstanceOf, isPlainObject, isPrimitive, isSyntheticEvent } from './is';
7-
import { Memo } from './memo';
7+
import { memoBuilder, MemoFunc } from './memo';
88
import { getFunctionName } from './stacktrace';
99
import { truncate } from './string';
1010

@@ -277,7 +277,7 @@ function normalizeValue<T>(value: T, key?: any): T | string {
277277
* @param memo Optional Memo class handling decycling
278278
*/
279279
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
280-
export function walk(key: string, value: any, depth: number = +Infinity, memo: Memo = new Memo()): any {
280+
export function walk(key: string, value: any, depth: number = +Infinity, memo: MemoFunc = memoBuilder()): any {
281281
// If we reach the maximum depth, serialize whatever has left
282282
if (depth === 0) {
283283
return serializeValue(value);
@@ -303,7 +303,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M
303303
const acc = Array.isArray(value) ? [] : {};
304304

305305
// If we already walked that branch, bail out, as it's circular reference
306-
if (memo.memoize(value)) {
306+
if (memo[0](value)) {
307307
return '[Circular ~]';
308308
}
309309

@@ -318,7 +318,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M
318318
}
319319

320320
// Once walked through all the branches, remove the parent from memo storage
321-
memo.unmemoize(value);
321+
memo[1](value);
322322

323323
// Return accumulated values
324324
return acc;

0 commit comments

Comments
 (0)