-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
39 lines (29 loc) · 958 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface AsyncLocalStorageInterface {
getStore: () => any | undefined;
run: <T>(store: any, callback: () => T) => T;
}
export class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
getStore(): any {
return undefined;
}
run<T>(_store: any, callback: () => T): T {
return callback();
}
}
class AsyncLocalStorageProvider {
private asyncLocalStorage: AsyncLocalStorageInterface =
new MockAsyncLocalStorage();
private hasBeenInitialized = false;
getInstance(): AsyncLocalStorageInterface {
return this.asyncLocalStorage;
}
initializeGlobalInstance(instance: AsyncLocalStorageInterface) {
if (!this.hasBeenInitialized) {
this.hasBeenInitialized = true;
this.asyncLocalStorage = instance;
}
}
}
const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
export { AsyncLocalStorageProviderSingleton };