Add workaround for global registry async context in REPL

This commit is contained in:
Garrett Mills 2022-08-06 21:57:29 -05:00
parent 91d76f44b5
commit 710b6cb535
2 changed files with 14 additions and 2 deletions

View File

@ -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",

View File

@ -19,6 +19,8 @@ export class AccessGlobalRegistryOutsideAsyncLifecycleError extends Error {
export class GlobalRegistry {
protected readonly storage: AsyncLocalStorage<Collection<GlobalRegistrant>> = new AsyncLocalStorage<Collection<GlobalRegistrant>>()
protected override?: Collection<GlobalRegistrant>
/** Run a closure with a global registry. */
public run<T>(closure: () => T): T {
return this.storage.run(new Collection<GlobalRegistrant>(), (): 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<GlobalRegistrant> {
protected getCollection(): Collection<GlobalRegistrant> {
if ( this.override ) {
return this.override
}
const coll = this.storage.getStore()
if ( !coll ) {
throw new AccessGlobalRegistryOutsideAsyncLifecycleError()