Skip to content

Commit 4d92ad7

Browse files
committed
fix: fork
1 parent fdfa5fd commit 4d92ad7

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/context.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,44 @@ export class SimpleContext {
55
private contextId: string;
66
private properties: Record<string, unknown> = {};
77
private forks: Array<SimpleContext> = [];
8-
private isFork: boolean;
98

10-
constructor(id = randomUUID(), isFork = false) {
9+
constructor(id = randomUUID()) {
1110
this.contextId = id;
12-
this.isFork = isFork;
1311
}
1412

1513
public get<T>(key: string): T | undefined {
16-
if (this.isFork) {
17-
return this.getForkProperty(key);
18-
}
19-
return this.properties[key] as T | undefined;
14+
return this.getForkProperty<T>(key);
2015
}
2116

2217
public set<T>(key: string, value: T): void {
23-
if (this.isFork) {
24-
return this.setForkProperty(key, value);
25-
}
26-
this.properties[key] = value;
18+
return this.setForkProperty<T>(key, value);
2719
}
2820

2921
public fork(): void {
30-
const id = async_hooks.executionAsyncId().toString();
31-
this.forks = [...this.forks, new SimpleContext(id)];
22+
const id = async_hooks.executionAsyncId();
23+
this.forks = [...this.forks, new SimpleContext(id.toString())];
3224
}
3325

3426
private setForkProperty<T>(key: string, value: T): void {
3527
const id = async_hooks.executionAsyncId().toString();
3628
const fork = this.forks.find((fork) => fork.contextId === id);
37-
if (fork) {
38-
fork.set(key, value);
39-
}
29+
return fork
30+
? fork.setProperty<T>(key, value)
31+
: this.setProperty<T>(key, value);
4032
}
4133

4234
private getForkProperty<T>(key: string): T | undefined {
4335
const id = async_hooks.executionAsyncId().toString();
4436
const fork = this.forks.find((fork) => fork.contextId === id);
45-
if (fork) {
46-
return fork.get<T>(key);
47-
}
48-
return undefined;
37+
return fork ? fork.getProperty<T>(key) : this.getProperty<T>(key);
38+
}
39+
40+
private setProperty<T>(key: string, value: T): void {
41+
this.properties[key] = value;
42+
}
43+
44+
private getProperty<T>(key: string): T | undefined {
45+
return this.properties[key] as T | undefined;
4946
}
5047
}
5148

0 commit comments

Comments
 (0)