-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: store external value in separated WeakMap (#134)
- Loading branch information
1 parent
7fca935
commit a341c1c
Showing
7 changed files
with
42 additions
and
24 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
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** @public */ | ||
export interface External extends Record<any, any> {} | ||
|
||
const externalValue = new WeakMap<External, number | bigint>() | ||
|
||
/** @public */ | ||
export function isExternal (object: unknown): object is External { | ||
return externalValue.has(object as any) | ||
} | ||
|
||
/** @public */ // eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export const External = (() => { | ||
function External (this: External, value: number | bigint): void { | ||
Object.setPrototypeOf(this, null) | ||
externalValue.set(this, value) | ||
} | ||
External.prototype = null as any | ||
return External as unknown as { | ||
new (value: number | bigint): External | ||
prototype: null | ||
} | ||
})() | ||
|
||
/** @public */ | ||
export function getExternalValue (external: External): number | bigint { | ||
if (!isExternal(external)) { | ||
throw new TypeError('not external') | ||
} | ||
return externalValue.get(external)! | ||
} |
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
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
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
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