From a28ae6f7501e10a16e62e3efc1e5135a1228c6c0 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 30 Dec 2024 00:11:58 -0800 Subject: [PATCH] Remove `singleton()`, add `createSingleton()` --- .changeset/khaki-dogs-reflect.md | 5 ++++ packages/ncm/src/types.ts | 15 +++++++---- packages/util/src/index.ts | 45 +++++++++++++------------------- 3 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 .changeset/khaki-dogs-reflect.md diff --git a/.changeset/khaki-dogs-reflect.md b/.changeset/khaki-dogs-reflect.md new file mode 100644 index 00000000..01b9513e --- /dev/null +++ b/.changeset/khaki-dogs-reflect.md @@ -0,0 +1,5 @@ +--- +"@nx.js/util": major +--- + +Remove `singleton()`, add `createSingleton()` diff --git a/packages/ncm/src/types.ts b/packages/ncm/src/types.ts index 6a9c0204..4ce0920d 100644 --- a/packages/ncm/src/types.ts +++ b/packages/ncm/src/types.ts @@ -1,7 +1,11 @@ -import { ArrayBufferStruct, singleton, u8, view } from '@nx.js/util'; +import { ArrayBufferStruct, createSingleton, u8, view } from '@nx.js/util'; -const contentIds = new WeakMap(); -const contentMetaKeys = new WeakMap(); +const contentIdCache = createSingleton( + (self: NcmContentInfo) => new NcmContentId(self, 0x0), +); +const contentMetaKeyCache = createSingleton( + (self: NcmContentStorageRecord) => new NcmContentMetaKey(self, 0x0), +); /// StorageId export enum NcmStorageId { @@ -93,6 +97,7 @@ export class NcmPlaceHolderId extends ArrayBufferStruct { static sizeof = 0x10 as const; } +/// ContentStorageRecord export class NcmContentStorageRecord extends ArrayBufferStruct { //NcmContentMetaKey key; //u8 storage_id; @@ -100,7 +105,7 @@ export class NcmContentStorageRecord extends ArrayBufferStruct { static sizeof = 0x18 as const; get key() { - return singleton(contentMetaKeys, this, () => new NcmContentMetaKey(this)); + return contentMetaKeyCache(this); } set key(v: NcmContentMetaKey) { u8(this).set(u8(v), 0); @@ -163,7 +168,7 @@ export class NcmContentInfo extends ArrayBufferStruct { static sizeof = 0x18 as const; get contentId() { - return singleton(contentIds, this, () => new NcmContentId(this)); + return contentIdCache(this); } set contentId(v: NcmContentId) { u8(this).set(u8(v), 0); diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 75b9a2c4..205afcca 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -1,33 +1,24 @@ -export function singleton( - map: WeakMap, - key: K, - init: () => V, -) { - let value = map.get(key); - if (!value) { - value = init(); - map.set(key, value); - } - return value; +export function createSingleton(init: (k: K) => V) { + const map = new WeakMap(); + return (k: K) => { + if (!map.has(k)) { + const v = init(k); + map.set(k, v); + return v; + } + return map.get(k)!; + }; } -const views = new WeakMap(); -export function view(view: ArrayBufferView): DataView { - return singleton( - views, - view, - () => new DataView(view.buffer, view.byteOffset, view.byteLength), - ); -} +export const view = createSingleton( + (view: ArrayBufferView) => + new DataView(view.buffer, view.byteOffset, view.byteLength), +); -const u8s = new WeakMap(); -export function u8(view: ArrayBufferView): Uint8Array { - return singleton( - u8s, - view, - () => new Uint8Array(view.buffer, view.byteOffset, view.byteLength), - ); -} +export const u8 = createSingleton( + (view: ArrayBufferView) => + new Uint8Array(view.buffer, view.byteOffset, view.byteLength), +); export function decodeCString(view: ArrayBufferView): string { const arr = u8(view);