Add workaround for global registry async context in REPL
This commit is contained in:
parent
91d76f44b5
commit
710b6cb535
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@extollo/lib",
|
"name": "@extollo/lib",
|
||||||
"version": "0.13.2",
|
"version": "0.13.3",
|
||||||
"description": "The framework library that lifts up your code.",
|
"description": "The framework library that lifts up your code.",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
|
@ -19,6 +19,8 @@ export class AccessGlobalRegistryOutsideAsyncLifecycleError extends Error {
|
|||||||
export class GlobalRegistry {
|
export class GlobalRegistry {
|
||||||
protected readonly storage: AsyncLocalStorage<Collection<GlobalRegistrant>> = new AsyncLocalStorage<Collection<GlobalRegistrant>>()
|
protected readonly storage: AsyncLocalStorage<Collection<GlobalRegistrant>> = new AsyncLocalStorage<Collection<GlobalRegistrant>>()
|
||||||
|
|
||||||
|
protected override?: Collection<GlobalRegistrant>
|
||||||
|
|
||||||
/** Run a closure with a global registry. */
|
/** Run a closure with a global registry. */
|
||||||
public run<T>(closure: () => T): T {
|
public run<T>(closure: () => T): T {
|
||||||
return this.storage.run(new Collection<GlobalRegistrant>(), (): T => {
|
return this.storage.run(new Collection<GlobalRegistrant>(), (): T => {
|
||||||
@ -33,6 +35,12 @@ export class GlobalRegistry {
|
|||||||
return this
|
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`.
|
* Store the given `value` associated by `key`.
|
||||||
* @param key
|
* @param key
|
||||||
@ -61,7 +69,11 @@ export class GlobalRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Get the globals collection or throw an error if outside async context. */
|
/** 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()
|
const coll = this.storage.getStore()
|
||||||
if ( !coll ) {
|
if ( !coll ) {
|
||||||
throw new AccessGlobalRegistryOutsideAsyncLifecycleError()
|
throw new AccessGlobalRegistryOutsideAsyncLifecycleError()
|
||||||
|
Loading…
Reference in New Issue
Block a user