-
Notifications
You must be signed in to change notification settings - Fork 3
feat(state-api): add @effectionx/state-api with useState #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
184e8f4
feat(state-api): add @effectionx/state-api with useState
taras 4c7625f
address CodeRabbit review feedback
taras 215949e
♻️ Switch state-api to context-api on stable Effection
taras ff1036d
♻️ Tighten state-api typing and remove broad any casts
taras 367799b
Migrate state-api tests from bdd to vitest
taras b8a4266
Sync pnpm-lock.yaml with effection 4.0.2
taras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,116 @@ | ||
| # State API | ||
|
|
||
| Reactive state container with typed reducers and middleware for Effection. | ||
|
|
||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic state | ||
|
|
||
| ```ts | ||
| import { run, each, spawn } from "effection"; | ||
| import { useState } from "@effectionx/state-api"; | ||
|
|
||
| await run(function* () { | ||
| const counter = yield* useState(0); | ||
|
|
||
| yield* counter.set(42); | ||
| yield* counter.update((n) => n + 1); | ||
| const value = yield* counter.get(); // 43 | ||
| }); | ||
| ``` | ||
|
|
||
| ### Typed reducers | ||
|
|
||
| Define named state transitions that are type-safe and interceptable. | ||
|
|
||
| ```ts | ||
| import { run } from "effection"; | ||
| import { useState } from "@effectionx/state-api"; | ||
|
|
||
| interface Todo { | ||
| id: number; | ||
| text: string; | ||
| done: boolean; | ||
| } | ||
|
|
||
| await run(function* () { | ||
| const todos = yield* useState([] as Todo[], { | ||
| add: (state, text: string) => [ | ||
| ...state, | ||
| { id: state.length, text, done: false }, | ||
| ], | ||
| toggle: (state, id: number) => | ||
| state.map((t) => (t.id === id ? { ...t, done: !t.done } : t)), | ||
| remove: (state, id: number) => state.filter((t) => t.id !== id), | ||
| }); | ||
|
|
||
| yield* todos.add("buy milk"); // returns the new state | ||
| yield* todos.toggle(0); | ||
| yield* todos.remove(0); | ||
|
|
||
| // built-in operations still available | ||
| yield* todos.set([]); | ||
| yield* todos.update((s) => [...s]); | ||
| const snapshot = yield* todos.get(); | ||
| }); | ||
| ``` | ||
|
|
||
| Each reducer is a function `(state, ...args) => newState`. The `state` | ||
| argument is injected automatically; callers pass only the remaining | ||
| arguments. | ||
|
|
||
| ### Stream subscription | ||
|
|
||
| `State<T>` implements `Stream<T, void>`, so you can subscribe to changes. | ||
| The following continues from the `todos` example above: | ||
|
|
||
| ```ts | ||
| // inside run(function* () { ... }) | ||
| yield* spawn(function* () { | ||
| for (const snapshot of yield* each(todos)) { | ||
| console.log("todos changed:", snapshot); | ||
| yield* each.next(); | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ### Middleware | ||
|
|
||
| Every operation (`set`, `update`, `get`, and all reducer actions) can be | ||
| intercepted with middleware via `around()`: | ||
|
|
||
| ```ts | ||
| // inside run(function* () { ... }) | ||
|
|
||
| // log every state change | ||
| yield* todos.around({ | ||
| *set([value], next) { | ||
| console.log("replacing state:", value); | ||
| return yield* next(value); | ||
| }, | ||
| *add([text], next) { | ||
| console.log("adding todo:", text); | ||
| return yield* next(text); | ||
| }, | ||
| }); | ||
|
|
||
| // validate state transitions | ||
| yield* counter.around({ | ||
| *set([value], next) { | ||
| if (value < 0) throw new Error("counter cannot be negative"); | ||
| return yield* next(value); | ||
| }, | ||
| }); | ||
|
|
||
| // modify arguments | ||
| yield* counter.around({ | ||
| *update([fn], next) { | ||
| return yield* next((n) => Math.max(0, fn(n))); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Middleware is scoped: it applies only within the current Effection scope | ||
| and is automatically removed when the scope exits. | ||
This file contains hidden or 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,2 @@ | ||
| export { useState } from "./state.ts"; | ||
| export type { State, ReducerMap } from "./state.ts"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.