Skip to content

Commit 5ece0a8

Browse files
committed
feat: typing
1 parent b4e3163 commit 5ece0a8

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ npm install --save node-simple-context
1414

1515
### Simple
1616

17-
1. Create a new file `my-context.ts` in which you define your context.
17+
- 1. Create a new file `my-context.ts` in which you define your context.
1818

1919
```ts
2020
import { createSimpleContext } from 'node-simple-context';
@@ -23,21 +23,26 @@ export const contextA = createSimpleContext();
2323
export const contextB = createSimpleContext();
2424
```
2525

26-
2. You now can set the context
26+
- 2. You now can set the context in your code wherever you want.
2727

2828
```ts
2929
import { contextA, contextB } from './my-context';
30+
3031
contextA.set('foo', 'bar');
3132
contextB.set('foo', 'baz');
3233
```
3334

34-
3. In an other file, you can get your context value
35+
- 3. In an other file, you can get your context value
3536

3637
```ts
3738
import { contextA, contextB } from './my-context';
39+
3840
console.log(contextA.get('foo')); // bar
3941
console.log(contextB.get('foo')); // baz
4042
console.log(contextA.get('xxx')); // undefined
43+
44+
// in typescript
45+
console.log(contextA.get<string>('foo')); // bar
4146
```
4247

4348
### Complex
@@ -48,26 +53,26 @@ console.log(contextA.get('xxx')); // undefined
4853
const context = createSimpleContext();
4954

5055
const func = (): string => {
51-
const foo = context.getForkProperty('foo');
56+
const foo = context.get('foo');
5257
return `foo=${foo}`;
5358
};
5459

5560
context.fork();
56-
context.setForkProperty('foo', 'bar');
61+
context.set('foo', 'bar');
5762

5863
const res = await Promise.all([
5964
new Promise((resolve) => {
6065
setTimeout(() => {
6166
context.fork();
62-
context.setForkProperty('foo', 'tata');
67+
context.set('foo', 'tata');
6368
resolve(func());
6469
}, 400);
6570
}),
6671
func(),
6772
new Promise((resolve) => {
6873
setTimeout(() => {
6974
context.fork();
70-
context.setForkProperty('foo', 'toto');
75+
context.set('foo', 'toto');
7176
resolve(func());
7277
}, 200);
7378
}),
@@ -86,23 +91,23 @@ const contextB = createSimpleContext();
8691
const contextC = createSimpleContext();
8792

8893
const func = (context: SimpleContext): string => {
89-
const foo = context.getProperty('foo');
94+
const foo = context.get('foo');
9095
return `foo=${foo}`;
9196
};
9297

93-
contextC.setProperty('foo', 'bar');
98+
contextC.set('foo', 'bar');
9499

95100
const res = await Promise.all([
96101
new Promise((resolve) => {
97102
setTimeout(() => {
98-
contextA.setProperty('foo', 'tata');
103+
contextA.set('foo', 'tata');
99104
resolve(func(contextA));
100105
}, 400);
101106
}),
102107
func(contextC),
103108
new Promise((resolve) => {
104109
setTimeout(() => {
105-
contextB.setProperty('foo', 'toto');
110+
contextB.set('foo', 'toto');
106111
resolve(func(contextB));
107112
}, 200);
108113
}),

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
"node",
2222
"node-simple-context",
2323
"minimalist",
24-
"react-context"
24+
"react-context",
25+
"async_hooks",
26+
"async-hooks",
27+
"typescript"
2528
],
2629
"scripts": {
2730
"start": "node build/index.js",

src/__tests__/context.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,37 @@ describe('SimpleContext', () => {
66
expect(context).toBeInstanceOf(SimpleContext);
77
});
88

9+
it('should get a value from a context', () => {
10+
const context = createSimpleContext();
11+
context.set('A', 10);
12+
const res = context.get<number>('A');
13+
expect(res).toBe(10);
14+
});
15+
916
it('should display value in order with fork', async () => {
1017
const context = createSimpleContext();
1118

1219
const func = (): string => {
13-
const foo = context.getForkProperty('foo');
20+
const foo = context.get('foo');
1421
return `foo=${foo}`;
1522
};
1623

1724
context.fork();
18-
context.setForkProperty('foo', 'bar');
25+
context.set('foo', 'bar');
1926

2027
const res = await Promise.all([
2128
new Promise((resolve) => {
2229
setTimeout(() => {
2330
context.fork();
24-
context.setForkProperty('foo', 'tata');
31+
context.set('foo', 'tata');
2532
resolve(func());
2633
}, 400);
2734
}),
2835
func(),
2936
new Promise((resolve) => {
3037
setTimeout(() => {
3138
context.fork();
32-
context.setForkProperty('foo', 'toto');
39+
context.set('foo', 'toto');
3340
resolve(func());
3441
}, 200);
3542
}),
@@ -43,23 +50,23 @@ describe('SimpleContext', () => {
4350
const contextC = createSimpleContext();
4451

4552
const func = (context: SimpleContext): string => {
46-
const foo = context.getProperty('foo');
53+
const foo = context.get('foo');
4754
return `foo=${foo}`;
4855
};
4956

50-
contextC.setProperty('foo', 'bar');
57+
contextC.set('foo', 'bar');
5158

5259
const res = await Promise.all([
5360
new Promise((resolve) => {
5461
setTimeout(() => {
55-
contextA.setProperty('foo', 'tata');
62+
contextA.set('foo', 'tata');
5663
resolve(func(contextA));
5764
}, 400);
5865
}),
5966
func(contextC),
6067
new Promise((resolve) => {
6168
setTimeout(() => {
62-
contextB.setProperty('foo', 'toto');
69+
contextB.set('foo', 'toto');
6370
resolve(func(contextB));
6471
}, 200);
6572
}),

src/context.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ import async_hooks from 'async_hooks';
33

44
export class SimpleContext {
55
private contextId: string;
6-
private properties: { [key: string]: any } = {};
6+
private properties: Record<string, unknown> = {};
77
private forks: Array<SimpleContext> = [];
8+
private isFork: boolean;
89

9-
constructor(private id = randomUUID()) {
10-
this.contextId = this.id;
10+
constructor(id = randomUUID(), isFork = false) {
11+
this.contextId = id;
12+
this.isFork = isFork;
1113
}
1214

13-
public getProperty(key: string): any {
14-
return this.properties[key];
15+
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;
1520
}
1621

17-
public setProperty(key: string, value: any): void {
22+
public set<T>(key: string, value: T): void {
23+
if (this.isFork) {
24+
return this.setForkProperty(key, value);
25+
}
1826
this.properties[key] = value;
1927
}
2028

@@ -23,19 +31,19 @@ export class SimpleContext {
2331
this.forks = [...this.forks, new SimpleContext(id)];
2432
}
2533

26-
public setForkProperty(key: string, value: any): void {
34+
private setForkProperty<T>(key: string, value: T): void {
2735
const id = async_hooks.executionAsyncId().toString();
2836
const fork = this.forks.find((fork) => fork.contextId === id);
2937
if (fork) {
30-
fork.setProperty(key, value);
38+
fork.set(key, value);
3139
}
3240
}
3341

34-
public getForkProperty(key: string): any {
42+
private getForkProperty<T>(key: string): T | undefined {
3543
const id = async_hooks.executionAsyncId().toString();
3644
const fork = this.forks.find((fork) => fork.contextId === id);
3745
if (fork) {
38-
return fork.getProperty(key);
46+
return fork.get<T>(key);
3947
}
4048
return undefined;
4149
}

0 commit comments

Comments
 (0)