Skip to content

Commit

Permalink
Remove singleton(), add createSingleton()
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Dec 30, 2024
1 parent 6f835e1 commit a28ae6f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-dogs-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/util": major
---

Remove `singleton()`, add `createSingleton()`
15 changes: 10 additions & 5 deletions packages/ncm/src/types.ts
Original file line number Diff line number Diff line change
@@ -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<ArrayBufferView, NcmContentId>();
const contentMetaKeys = new WeakMap<ArrayBufferView, NcmContentMetaKey>();
const contentIdCache = createSingleton(
(self: NcmContentInfo) => new NcmContentId(self, 0x0),
);
const contentMetaKeyCache = createSingleton(
(self: NcmContentStorageRecord) => new NcmContentMetaKey(self, 0x0),
);

/// StorageId
export enum NcmStorageId {
Expand Down Expand Up @@ -93,14 +97,15 @@ export class NcmPlaceHolderId extends ArrayBufferStruct {
static sizeof = 0x10 as const;
}

/// ContentStorageRecord
export class NcmContentStorageRecord extends ArrayBufferStruct {
//NcmContentMetaKey key;
//u8 storage_id;
//u8 padding[0x7];
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);
Expand Down Expand Up @@ -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);
Expand Down
45 changes: 18 additions & 27 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
export function singleton<K extends object, V>(
map: WeakMap<K, V>,
key: K,
init: () => V,
) {
let value = map.get(key);
if (!value) {
value = init();
map.set(key, value);
}
return value;
export function createSingleton<K extends object, V>(init: (k: K) => V) {
const map = new WeakMap<K, V>();
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<ArrayBufferView, DataView>();
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<ArrayBufferView, Uint8Array>();
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);
Expand Down

0 comments on commit a28ae6f

Please sign in to comment.