-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Explicit create() and run() Signed-off-by: Matteo Collina <[email protected]> * add node 22 Signed-off-by: Matteo Collina <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
8 changed files
with
159 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
declare namespace asyncforge { | ||
export function start(): void; | ||
interface Store { | ||
run<T>(fn: () => T) : T; | ||
} | ||
|
||
export function create () : Store; | ||
|
||
export function memo<T extends unknown>( | ||
name?: string | ||
): { | ||
(): T; | ||
key: symbol; | ||
set: (value: T) => void; | ||
}; | ||
export function setAll(memos: Record<symbol, unknown>): void; | ||
} | ||
|
||
export = asyncforge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { expectError, expectType } from "tsd"; | ||
import { start, memo, setAll } from "."; | ||
import { create, memo } from "."; | ||
import type { Store } from "."; | ||
|
||
expectType<() => void>(start); | ||
expectType<() => Store>(create); | ||
|
||
const store = create(); | ||
|
||
expectType<number>(store.run(() => 42)); | ||
|
||
// memo | ||
const memoNum = memo<number>(); | ||
expectType<symbol>(memoNum.key); | ||
expectType<void>(memoNum.set(123)); | ||
expectType<number>(memoNum()); | ||
expectError<void>(memoNum.set("wrong")); | ||
|
||
// setAll | ||
const test = memo<string>(); | ||
expectType<void>(setAll({ [test.key]: 42 })); | ||
expectError<void>(setAll({ wrong: 42 })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,38 @@ | ||
import { memo } from './asyncforge.js' | ||
import { create, memo } from './asyncforge.js' | ||
|
||
const a = memo() | ||
const b = memo() | ||
|
||
a.set(42) | ||
b.set(123) | ||
const store = create() | ||
|
||
// simulate an event loop turn | ||
setImmediate(() => { | ||
console.log('-- first event loop turn --') | ||
console.log('a', a()) | ||
console.log('b', b()) | ||
store.run(() => { | ||
a.set(42) | ||
b.set(123) | ||
|
||
b.set(456) | ||
// simulate an event loop turn | ||
setImmediate(() => { | ||
console.log('-- third event loop turn --') | ||
console.log('-- first event loop turn --') | ||
console.log('a', a()) | ||
console.log('b', b()) | ||
}) | ||
}) | ||
|
||
a.set(43) | ||
b.set(321) | ||
create(() => { | ||
a.set(43) | ||
b.set(321) | ||
|
||
// simulate an event loop turn | ||
setImmediate(() => { | ||
console.log('-- second event loop turn --') | ||
console.log('a', a()) | ||
console.log('b', b()) | ||
// simulate an event loop turn | ||
setImmediate(() => { | ||
console.log('-- second event loop turn --') | ||
console.log('a', a()) | ||
console.log('b', b()) | ||
|
||
store.run(() => { | ||
setImmediate(() => { | ||
console.log('-- third event loop turn --') | ||
console.log('a', a()) | ||
console.log('b', b()) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.