@plugjs/plug
Version:
PlugJS Build System ===================
20 lines • 809 B
JavaScript
/**
* Get the instance of a _singleton_ variable.
*
* Sometimes we need unique instances _per process_ (for example our async
* context). The problem is that the code might get called from two (or three)
* different versions of this file: the .cjs transpiled code, the .mjs
* transpiled one, or the .ts dynamically transpiled by our dynamic loader.
*
* A _singleton_ associates an instance with a symbol in `globalThis` and ensure
* there is only _one_ instance per process (per `globalThis`).
*/
export function getSingleton(symbol, factory) {
const anyGlobalThis = globalThis;
if (anyGlobalThis[symbol])
return anyGlobalThis[symbol];
const value = factory();
Object.defineProperty(anyGlobalThis, symbol, { value });
return value;
}
//# sourceMappingURL=singleton.js.map