From 42f5a58ffc30457ab8c4cbe80f0f8cbc7ebbc9db Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Tue, 14 Dec 2021 14:48:27 -0500 Subject: [PATCH] ref(utils): convert memo from class to function (#4283) --- packages/utils/src/memo.ts | 55 ++++++++++++++---------------------- packages/utils/src/object.ts | 8 +++--- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/packages/utils/src/memo.ts b/packages/utils/src/memo.ts index 81e1c9c74a79..735c9446e16b 100644 --- a/packages/utils/src/memo.ts +++ b/packages/utils/src/memo.ts @@ -1,57 +1,44 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -/** - * Memo class used for decycle json objects. Uses WeakSet if available otherwise array. - */ -export class Memo { - /** Determines if WeakSet is available */ - private readonly _hasWeakSet: boolean; - /** Either WeakSet or Array */ - private readonly _inner: any; - public constructor() { - this._hasWeakSet = typeof WeakSet === 'function'; - this._inner = this._hasWeakSet ? new WeakSet() : []; - } +export type MemoFunc = [(obj: any) => boolean, (obj: any) => void]; - /** - * Sets obj to remember. - * @param obj Object to remember - */ - public memoize(obj: any): boolean { - if (this._hasWeakSet) { - if (this._inner.has(obj)) { +/** + * Helper to decycle json objects + */ +export function memoBuilder(): MemoFunc { + const hasWeakSet = typeof WeakSet === 'function'; + const inner: any = hasWeakSet ? new WeakSet() : []; + function memoize(obj: any): boolean { + if (hasWeakSet) { + if (inner.has(obj)) { return true; } - this._inner.add(obj); + inner.add(obj); return false; } // eslint-disable-next-line @typescript-eslint/prefer-for-of - for (let i = 0; i < this._inner.length; i++) { - const value = this._inner[i]; + for (let i = 0; i < inner.length; i++) { + const value = inner[i]; if (value === obj) { return true; } } - this._inner.push(obj); + inner.push(obj); return false; } - /** - * Removes object from internal storage. - * @param obj Object to forget - */ - public unmemoize(obj: any): void { - if (this._hasWeakSet) { - this._inner.delete(obj); + function unmemoize(obj: any): void { + if (hasWeakSet) { + inner.delete(obj); } else { - for (let i = 0; i < this._inner.length; i++) { - if (this._inner[i] === obj) { - this._inner.splice(i, 1); + for (let i = 0; i < inner.length; i++) { + if (inner[i] === obj) { + inner.splice(i, 1); break; } } } } + return [memoize, unmemoize]; } diff --git a/packages/utils/src/object.ts b/packages/utils/src/object.ts index b44575ca92cd..77213bc2bba4 100644 --- a/packages/utils/src/object.ts +++ b/packages/utils/src/object.ts @@ -4,7 +4,7 @@ import { ExtendedError, WrappedFunction } from '@sentry/types'; import { htmlTreeAsString } from './browser'; import { isElement, isError, isEvent, isInstanceOf, isPlainObject, isPrimitive, isSyntheticEvent } from './is'; -import { Memo } from './memo'; +import { memoBuilder, MemoFunc } from './memo'; import { getFunctionName } from './stacktrace'; import { truncate } from './string'; @@ -277,7 +277,7 @@ function normalizeValue(value: T, key?: any): T | string { * @param memo Optional Memo class handling decycling */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export function walk(key: string, value: any, depth: number = +Infinity, memo: Memo = new Memo()): any { +export function walk(key: string, value: any, depth: number = +Infinity, memo: MemoFunc = memoBuilder()): any { // If we reach the maximum depth, serialize whatever has left if (depth === 0) { return serializeValue(value); @@ -303,7 +303,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M const acc = Array.isArray(value) ? [] : {}; // If we already walked that branch, bail out, as it's circular reference - if (memo.memoize(value)) { + if (memo[0](value)) { return '[Circular ~]'; } @@ -318,7 +318,7 @@ export function walk(key: string, value: any, depth: number = +Infinity, memo: M } // Once walked through all the branches, remove the parent from memo storage - memo.unmemoize(value); + memo[1](value); // Return accumulated values return acc;