|
|
|
@ -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()
|
|
|
|
|