-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
82 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from "./event"; | ||
export * from "./effect"; | ||
export * from "./event"; | ||
export * from "./methods"; | ||
export * from "./store"; | ||
export * from "./utils"; | ||
export * from "./types.h"; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createStore } from "./store"; | ||
import { combine, merge } from "./methods"; | ||
import { createEvent } from "./event"; | ||
|
||
describe("methods", () => { | ||
it("should be create combine store", () => { | ||
const $balance = createStore(0); | ||
const $username = createStore("zerobias"); | ||
const watcherMock = jest.fn(); | ||
|
||
const $greeting = combine( | ||
$balance, | ||
$username, | ||
(balance, username) => `Hello, ${username}. Your balance is ${balance}`, | ||
); | ||
|
||
$greeting.watch(watcherMock); | ||
expect(watcherMock).toHaveBeenCalledWith("Hello, zerobias. Your balance is 0"); | ||
}); | ||
|
||
it("should be create new event that triggers upon any of the given units being triggered", () => { | ||
const watcherMock = jest.fn(); | ||
const foo = createEvent<number>(); | ||
const bar = createEvent<number>(); | ||
const baz = merge(foo, bar); | ||
|
||
baz.watch(watcherMock); | ||
|
||
foo(10); | ||
expect(watcherMock).toHaveBeenCalledWith(10); | ||
|
||
baz(42); | ||
expect(watcherMock).toHaveBeenCalledWith(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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createEvent, type Event } from "./event"; | ||
import { createStore, type Store } from "./store"; | ||
import type { Unit } from "./types.h"; | ||
|
||
type States<S extends Store<unknown>[]> = { | ||
[Key in keyof S]: S[Key] extends Store<infer U> ? U : never; | ||
}; | ||
|
||
type CombineFn<S extends Store<unknown>[], NewStore> = (...states: States<S>) => NewStore; | ||
|
||
export function combine<S extends Store<unknown>[], NewStore>( | ||
...args: [...S, CombineFn<S, NewStore>] | ||
): Store<NewStore> { | ||
const stores = args.slice(0, -1) as S; | ||
const fn = args[args.length - 1] as CombineFn<S, NewStore>; | ||
|
||
const getStoreState = () => fn(...(stores.map((store) => store.getState()) as States<S>)); | ||
|
||
const updateStore = createEvent(); | ||
const newStore = createStore<NewStore>(getStoreState()).on(updateStore, () => getStoreState()); | ||
|
||
stores.forEach((store) => store.watch(() => updateStore())); | ||
|
||
return newStore; | ||
} | ||
|
||
export function merge<T>(...units: Unit<T>[]): Event<T> { | ||
const event = createEvent<T>(); | ||
|
||
units.forEach((unit) => unit.watch(event)); | ||
|
||
return event; | ||
} |
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 +1,7 @@ | ||
import type { Effect } from "./effect"; | ||
import type { Event } from "./event"; | ||
import type { Store } from "./store"; | ||
|
||
export type Unsubscribe = () => void; | ||
|
||
export type Unit<T> = Effect<T, unknown> | Event<T> | Store<T>; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.