diff --git a/src/index.ts b/src/index.ts index e051b72..0db26e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ -import type { BunyanLikeLogger } from './decorator'; -import { Bunyamin } from './decorator'; -import { noopLogger } from './noopLogger'; +import realm from './realm'; export * from './noopLogger'; export * from './traceEventStream'; @@ -8,12 +6,7 @@ export * from './uniteTraceEvents'; export * from './wrapLogger'; export * from './is-debug'; -const threadGroups: any[] = []; -export const bunyamin = new Bunyamin({ logger: noopLogger(), threadGroups }); -export const nobunyamin = new Bunyamin({ - logger: noopLogger(), - threadGroups, - immutable: true, -}); +export const bunyamin = realm.bunyamin; +export const nobunyamin = realm.nobunyamin; export default bunyamin; diff --git a/src/realm.ts b/src/realm.ts new file mode 100644 index 0000000..c38d2cb --- /dev/null +++ b/src/realm.ts @@ -0,0 +1,31 @@ +import type { BunyanLikeLogger } from './decorator'; +import { Bunyamin } from './decorator'; +import { noopLogger } from './noopLogger'; + +type Realm = { + bunyamin: Bunyamin; + nobunyamin: Bunyamin; +}; + +function create() { + const threadGroups: any[] = []; + const bunyamin = new Bunyamin({ logger: noopLogger(), threadGroups }); + const nobunyamin = new Bunyamin({ + logger: noopLogger(), + threadGroups, + immutable: true, + }); + + return { bunyamin, nobunyamin }; +} + +function getCached(): Realm | undefined { + return (globalThis as any).__BUNYAMIN__; +} + +function setCached(realm: Realm) { + (globalThis as any).__BUNYAMIN__ = realm; + return realm; +} + +export default setCached(getCached() ?? create());