Skip to content

Instantly share code, notes, and snippets.

@kilip
Created August 8, 2025 02:02
Show Gist options
  • Save kilip/15dceca53049ee8bfb171a73c8885fa1 to your computer and use it in GitHub Desktop.
Save kilip/15dceca53049ee8bfb171a73c8885fa1 to your computer and use it in GitHub Desktop.
// Borrowed & modified from https://github.com/jenseng/abuse-the-platform/blob/main/app/utils/singleton.ts
// Thanks @jenseng!
export const singleton = <Value>(
name: string,
valueFactory: () => Value,
): Value => {
const g = global as unknown as { __singletons: Record<string, unknown> };
g.__singletons ??= {};
g.__singletons[name] ??= valueFactory();
return g.__singletons[name] as Value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment