Skip to content

Commit 08df86c

Browse files
committed
docs: add scheduler docs
1 parent 2e067b9 commit 08df86c

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ trace(count$);
123123

124124
</details>
125125

126+
<details>
127+
<summary>⏳ Scheduler mechanism for controlled updates.</summary>
128+
129+
`@embra/reactivity` includes a scheduler mechanism and built-in schedulers that lets you control when reactive updates are processed.
130+
This is useful for batching updates and deferring computations.
131+
132+
```ts
133+
import { writable, MicrotaskScheduler } from "@embra/reactivity";
134+
135+
const rapidChangeCount$ = writable(0);
136+
137+
rapidChangeCount$.reaction(console.log, MicrotaskScheduler);
138+
139+
count$.set(1);
140+
count$.set(2);
141+
142+
await Promise.resolve();
143+
// Logs "2" once after a microtask tick, reducing unnecessary computations.
144+
```
145+
146+
You can also provide your owned custom scheduler function easily.
147+
148+
```ts
149+
import { writable, asyncScheduler } from "@embra/reactivity";
150+
151+
const MicrotaskScheduler = asyncScheduler(flush => Promise.resolve().then(flush));
152+
const AnimationFrameScheduler = asyncScheduler(requestAnimationFrame);
153+
```
154+
155+
</details>
156+
126157
<details>
127158
<summary>🏗️ Framework agnostic. First-class support for React.</summary>
128159

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"endregion",
1010
"hyrious",
1111
"lcov",
12+
"microtask",
1213
"Parens",
1314
"pnpm",
1415
"preinstall",

src/react/useValue.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const defaultArgs = [returnsNoop, returnsNoop as () => any] as const;
4141
* It only triggers re-rendering when new value emitted from $ (base on {@link Readable.version} instead of React's `Object.is` comparison).
4242
*
4343
* @param $ A {@link ReadableLike}.
44+
* @param scheduler - An optional {@link Scheduler} to control the update frequency. If not provided, updates are applied synchronously.
4445
* @returns the value of the {@link ReadableLike}, or $ itself if $ is not a {@link ReadableLike}
4546
*
4647
* @example
@@ -52,6 +53,16 @@ const defaultArgs = [returnsNoop, returnsNoop as () => any] as const;
5253
* return <div>{count}</div>;
5354
* }
5455
* ```
56+
*
57+
* @example
58+
* ```tsx
59+
* import { useValue, MicrotaskScheduler } from "@embra/reactivity/react";
60+
*
61+
* function App({ rapidChangeCount$ }) {
62+
* const count = useValue(rapidChangeCount$, MicrotaskScheduler); // update at most once per microtask
63+
* return <div>{count}</div>;
64+
* }
65+
* ```
5566
*/
5667
export const useValue: UseValue = <T, U>($: ReadableLike<T> | U, scheduler?: Scheduler): Unwrap<T> | U => {
5768
const args = useMemo(() => {

src/schedulers/MicrotaskScheduler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { asyncScheduler } from "./AsyncScheduler";
22
import { type Scheduler, type SchedulerFlush } from "./interface";
33

44
/**
5-
* A {@link Scheduler} that runs updates in microtasks (Promise).
5+
* A {@link Scheduler} that runs updates at most once per microtask (Promise).
66
*/
77
export const MicrotaskScheduler: Scheduler = /* @__PURE__ */ asyncScheduler(
88
/* @__PURE__ */ ((tick: Promise<void>, flush: SchedulerFlush): Promise<void> => tick.then(flush)).bind(

0 commit comments

Comments
 (0)