From 710b6cb5359513f8809fbc66875162b122512d27 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 6 Aug 2022 21:57:29 -0500 Subject: [PATCH] Add workaround for global registry async context in REPL --- package.json | 2 +- src/util/support/global.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index aac88af..b93d669 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.13.2", + "version": "0.13.3", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/util/support/global.ts b/src/util/support/global.ts index c0649d5..9e01fac 100644 --- a/src/util/support/global.ts +++ b/src/util/support/global.ts @@ -19,6 +19,8 @@ export class AccessGlobalRegistryOutsideAsyncLifecycleError extends Error { export class GlobalRegistry { protected readonly storage: AsyncLocalStorage> = new AsyncLocalStorage>() + protected override?: Collection + /** Run a closure with a global registry. */ public run(closure: () => T): T { return this.storage.run(new Collection(), (): T => { @@ -33,6 +35,12 @@ export class GlobalRegistry { return this } + /** Force the current globals to be the global defaults for ALL contexts. */ + public forceContextOverride(): this { + this.override = this.getCollection() + return this + } + /** * Store the given `value` associated by `key`. * @param key @@ -61,7 +69,11 @@ export class GlobalRegistry { } /** Get the globals collection or throw an error if outside async context. */ - public getCollection(): Collection { + protected getCollection(): Collection { + if ( this.override ) { + return this.override + } + const coll = this.storage.getStore() if ( !coll ) { throw new AccessGlobalRegistryOutsideAsyncLifecycleError()