-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(utils): convert memo from class to function (#4283)
- Loading branch information
1 parent
0b2249d
commit 2ede32d
Showing
2 changed files
with
25 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters