-
Notifications
You must be signed in to change notification settings - Fork 4
Add recho.state(value) #114
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e7af60
Add mutable
pearmini 7c1148f
Expose __Mutator__
pearmini dcd1c0b
Add Observable Notebook Kit attribution
pearmini 0adef11
Merge branch 'main' into mutable
pearmini 366a24c
Rename to recho.state
pearmini f98860a
Add docs
pearmini 6ac0c2d
Merge branch 'main' into mutable
pearmini 5ce6bda
Update state order
pearmini 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
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,72 @@ | ||
| /** | ||
| * @title recho.state(value) | ||
| */ | ||
|
|
||
| /** | ||
| * ============================================================================ | ||
| * = recho.state(value) = | ||
| * ============================================================================ | ||
| * | ||
| * Creates a reactive state variable that can be mutated over time. This is | ||
| * similar to React's useState hook and enables mutable reactive values that | ||
| * automatically trigger re-evaluation of dependent blocks when changed. | ||
| * | ||
| * @param {any} value - The initial state value. | ||
| * @returns {[any, Function, Function]} A tuple containing: | ||
| * - state: The reactive state value that can be read directly | ||
| * - setState: Function to update the state (accepts value or updater function) | ||
| * - getState: Function to get the current state value | ||
| */ | ||
|
|
||
| // Basic counter that increments after 1 second | ||
| const [count1, setCount1] = recho.state(0); | ||
|
|
||
| setTimeout(() => { | ||
| setCount1(count1 => count1 + 1); | ||
| }, 1000); | ||
|
|
||
| //➜ 1 | ||
| echo(count1); | ||
|
|
||
| // Timer that counts down from 10 | ||
| const [timer, setTimer] = recho.state(10); | ||
|
|
||
| { | ||
| const interval = setInterval(() => { | ||
| setTimer(t => { | ||
| if (t <= 0) { | ||
| clearInterval(interval); | ||
| return 0; | ||
| } | ||
| return t - 1; | ||
| }); | ||
| }, 1000); | ||
|
|
||
| invalidation.then(() => clearInterval(interval)); | ||
| } | ||
|
|
||
| //➜ 8 | ||
| echo(`Time remaining: ${timer}s`); | ||
|
|
||
| // State can be updated with a direct value | ||
| const [message, setMessage] = recho.state("Hello"); | ||
|
|
||
| setTimeout(() => { | ||
| setMessage("Hello, World!"); | ||
| }, 2000); | ||
|
|
||
| //➜ "Hello, World!" | ||
| echo(message); | ||
|
|
||
| // Multiple states can be used together | ||
| const [firstName, setFirstName] = recho.state("John"); | ||
| const [lastName, setLastName] = recho.state("Doe"); | ||
|
|
||
| setTimeout(() => { | ||
| setFirstName("Jane"); | ||
| setLastName("Smith"); | ||
| }, 1500); | ||
|
|
||
| //➜ "Jane Smith" | ||
| echo(`${firstName} ${lastName}`); | ||
|
|
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
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,37 @@ | ||
| // Derived from Observable Notebook Kit's observe. | ||
| // https://github.com/observablehq/notebook-kit/blob/main/src/runtime/stdlib/generators/observe.ts | ||
|
|
||
| export async function* observe(initialize) { | ||
| let resolve = undefined; | ||
| let value = undefined; | ||
| let stale = false; | ||
|
|
||
| const dispose = initialize((x) => { | ||
| value = x; | ||
| if (resolve) { | ||
| resolve(x); | ||
| resolve = undefined; | ||
| } else { | ||
| stale = true; | ||
| } | ||
| return x; | ||
| }); | ||
|
|
||
| if (dispose != null && typeof dispose !== "function") { | ||
| throw new Error( | ||
| typeof dispose === "object" && "then" in dispose && typeof dispose.then === "function" | ||
| ? "async initializers are not supported" | ||
| : "initializer returned something, but not a dispose function", | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| while (true) { | ||
| yield stale ? ((stale = false), value) : new Promise((_) => (resolve = _)); | ||
| } | ||
| } finally { | ||
| if (dispose != null) { | ||
| dispose(); | ||
| } | ||
| } | ||
| } |
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,26 @@ | ||
| // Derived from Observable Notebook Kit's mutable and mutator. | ||
| // https://github.com/observablehq/notebook-kit/blob/main/src/runtime/stdlib/mutable.ts | ||
| import {observe} from "./observe.js"; | ||
|
|
||
| // Mutable returns a generator with a value getter/setting that allows the | ||
| // generated value to be mutated. Therefore, direct mutation is only allowed | ||
| // within the defining cell, but the cell can also export functions that allows | ||
| // other cells to mutate the value as desired. | ||
| function Mutable(value) { | ||
| let change = undefined; | ||
| const mutable = observe((_) => { | ||
| change = _; | ||
| if (value !== undefined) change(value); | ||
| }); | ||
| return Object.defineProperty(mutable, "value", { | ||
| get: () => value, | ||
| set: (x) => ((value = x), void change?.(value)), | ||
| }); | ||
| } | ||
|
|
||
| export function state(value) { | ||
| const state = Mutable(value); | ||
| const setState = (x) => (typeof x === "function" ? (state.value = x(state.value)) : (state.value = x)); | ||
| const getState = () => state.value; | ||
| return [state, setState, getState]; | ||
pearmini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,8 @@ | ||
| export const mutable = `const [a, setA] = recho.state(0); | ||
|
|
||
| setTimeout(() => { | ||
| setA((a) => a + 1); | ||
| }, 1000); | ||
|
|
||
| echo(a); | ||
| `; |
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
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.