-
Notifications
You must be signed in to change notification settings - Fork 2
/
als-manager.ts
70 lines (53 loc) · 1.7 KB
/
als-manager.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { AsyncLocalStorage } from 'async_hooks';
import { AsyncStorageManagerException } from '../exceptions';
export class AsyncStorageManager<T> implements Map<string, T> {
constructor(private readonly asyncLocalStorage: AsyncLocalStorage<Map<string, T>>) { }
private getStore(): Map<string, T> {
const store = this.asyncLocalStorage.getStore();
if (!store) {
throw new AsyncStorageManagerException('No active store found');
}
return store;
}
register(): void {
this.asyncLocalStorage.enterWith(new Map());
}
runWithNewContext<R, TArgs extends any[]>(fn: (...args: TArgs) => R, ...args: TArgs): R {
return this.asyncLocalStorage.run<R, TArgs>(new Map<string, T>(), fn, ...args);
}
set(key: string, value: T): this {
this.getStore().set(key, value);
return this;
}
get(key: string): T | undefined {
return this.getStore().get(key);
}
clear(): void {
return this.getStore().clear();
}
delete(key: string): boolean {
return this.getStore().delete(key);
}
forEach(callbackfn: (value: T, key: string, map: Map<string, T>) => void, thisArg?: any): void {
return this.getStore().forEach(callbackfn, thisArg);
}
has(key: string): boolean {
return this.getStore().has(key);
}
get size(): number {
return this.getStore().size;
}
entries(): IterableIterator<[string, T]> {
return this.getStore().entries();
}
keys(): IterableIterator<string> {
return this.getStore().keys();
}
values(): IterableIterator<T> {
return this.getStore().values();
}
[Symbol.iterator](): IterableIterator<[string, T]> {
return this.getStore()[Symbol.iterator]()
}
[Symbol.toStringTag]: string = '[object AsyncContext]';
}