Skip to content

Commit

Permalink
added store.enterWith()
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed May 21, 2024
1 parent 3e869e0 commit 3ee8b97
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ create(() => {
console.log('b', b())
})
})

setImmediate(() => {
store.enterWith()
console.log('-- fourth event loop turn --')
console.log('a', a())
console.log('b', b())
})
})
})
```
Expand All @@ -59,25 +66,25 @@ create(() => {
You can call the `asyncforge` functions in a type-safe way:

```ts
import { start, memo, setAll } from "asyncforge";
import { create, memo } from "asyncforge";
const memoNum = memo<number>();
const test = memo<string>();

// This is okay for TypeScript, since you're passing a number
memoNum.set(123);

// This will not build
memoNum.set('wrong');
const store = create()

// The `result` var will be of type `number`
const result = memoNum()
store.run(() => {
// This is okay for TypeScript, since you're passing a number
memoNum.set(123);

const test = memo<string>();

// This is correct, since `test.key` is a `symbol`
setAll({ [test.key]: 42 });
// This will not build
memoNum.set('wrong');
})

// This will fail
setAll({ 'wrong': 42 });
store.run(() => {
// The `result` var will be of type `number`
const result = memoNum()
})
```

## License
Expand Down
1 change: 1 addition & 0 deletions asyncforge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare namespace asyncforge {
interface Store {
run<T>(fn: () => T) : T;
enterWith() : void;
}

export function create () : Store;
Expand Down
4 changes: 4 additions & 0 deletions asyncforge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Store {
run (fn) {
return asyncLocalStorage.run(this.#internal, fn)
}

enterWith () {
asyncLocalStorage.enterWith(this.#internal)
}
}

function create (fn) {
Expand Down
1 change: 1 addition & 0 deletions asyncforge.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expectType<() => Store>(create);
const store = create();

expectType<number>(store.run(() => 42));
expectType<void>(store.enterWith());

// memo
const memoNum = memo<number>();
Expand Down
7 changes: 7 additions & 0 deletions example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ create(() => {
console.log('b', b())
})
})

setImmediate(() => {
store.enterWith()
console.log('-- fourth event loop turn --')
console.log('a', a())
console.log('b', b())
})
})
})
24 changes: 24 additions & 0 deletions test/memo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,27 @@ test('run multiple times', async (t) => {

await p.completed
})

test('enterWith', async (t) => {
const p = tspl(t, { plan: 5 })
const a = memo()

p.throws(a, /asyncforge store has not been created/)
p.throws(() => a.set('foo'), /asyncforge store has not been created/)

create().enterWith()

p.deepStrictEqual(a(), undefined)
a.set({ value: 'bar' })
p.deepStrictEqual(a(), { value: 'bar' })

setImmediate(() => {
p.deepStrictEqual(a(), { value: 'bar' })
})

queueMicrotask(() => {
p.deepStrictEqual(a(), { value: 'bar' })
})

await p.completed
})

0 comments on commit 3ee8b97

Please sign in to comment.