|
| 1 | +import type { Metadata } from "next"; |
| 2 | + |
| 3 | +export const metadata: Metadata = { title: "@muix/core" }; |
| 4 | + |
| 5 | +export default function CorePage() { |
| 6 | + return ( |
| 7 | + <> |
| 8 | + <h1>@muix/core</h1> |
| 9 | + <p style={{ color: "var(--muted)", marginTop: "0.5rem" }}> |
| 10 | + The foundation — Signal, Observable, Channel, Session, Action, EventBus. |
| 11 | + Zero runtime dependencies. |
| 12 | + </p> |
| 13 | + |
| 14 | + <h2>Signal</h2> |
| 15 | + <p> |
| 16 | + A reactive value container. Naming is TC39 Signals proposal-compatible; |
| 17 | + the implementation is a lightweight own build (~150 lines). |
| 18 | + </p> |
| 19 | + <pre>{`import { createSignal, createComputed } from "@muix/core"; |
| 20 | +
|
| 21 | +const count = createSignal(0); |
| 22 | +count.set(1); |
| 23 | +count.update((n) => n + 1); // → 2 |
| 24 | +count.peek(); // read without subscribing |
| 25 | +
|
| 26 | +const doubled = createComputed( |
| 27 | + () => count.value * 2, |
| 28 | + [count], |
| 29 | +); |
| 30 | +
|
| 31 | +const sub = count.observe().subscribe({ next: (v) => console.log(v) }); |
| 32 | +sub.unsubscribe();`}</pre> |
| 33 | + <table> |
| 34 | + <thead><tr><th>Member</th><th>Description</th></tr></thead> |
| 35 | + <tbody> |
| 36 | + <tr><td><code>value</code></td><td>Current value (tracked read)</td></tr> |
| 37 | + <tr><td><code>peek()</code></td><td>Read without creating a subscription side-effect</td></tr> |
| 38 | + <tr><td><code>set(v)</code></td><td>Set new value; notifies listeners only if <code>!equals(prev, next)</code></td></tr> |
| 39 | + <tr><td><code>update(fn)</code></td><td>Derive next value from current</td></tr> |
| 40 | + <tr><td><code>observe()</code></td><td>Returns an <code>Observable<T></code>; emits current value immediately on subscribe</td></tr> |
| 41 | + </tbody> |
| 42 | + </table> |
| 43 | + |
| 44 | + <h2>Observable</h2> |
| 45 | + <p> |
| 46 | + TC39-compatible push stream (~200 lines). Implements{" "} |
| 47 | + <code>[Symbol.observable]()</code> for RxJS interop. |
| 48 | + </p> |
| 49 | + <pre>{`import { Observable } from "@muix/core"; |
| 50 | +
|
| 51 | +const obs = new Observable<number>((observer) => { |
| 52 | + observer.next(1); |
| 53 | + observer.next(2); |
| 54 | + observer.complete(); |
| 55 | + return () => { /* cleanup */ }; |
| 56 | +}); |
| 57 | +
|
| 58 | +const sub = obs.subscribe({ |
| 59 | + next: (v) => console.log(v), |
| 60 | + error: (e) => console.error(e), |
| 61 | + complete: () => console.log("done"), |
| 62 | +}); |
| 63 | +
|
| 64 | +sub.unsubscribe();`}</pre> |
| 65 | + |
| 66 | + <h2>Channel</h2> |
| 67 | + <p> |
| 68 | + Duplex streaming primitive built on{" "} |
| 69 | + <code>ReadableStream</code> / <code>WritableStream</code> (WHATWG Streams). |
| 70 | + Provides native backpressure, pause/resume, and composable piping. |
| 71 | + </p> |
| 72 | + <pre>{`import { createChannel } from "@muix/core"; |
| 73 | +
|
| 74 | +const ch = createChannel<string>({ highWaterMark: 16 }); |
| 75 | +await ch.open(); |
| 76 | +
|
| 77 | +// Write |
| 78 | +await ch.send("hello"); |
| 79 | +
|
| 80 | +// Observe (non-locking) |
| 81 | +ch.observe().subscribe({ next: (frame) => console.log(frame.data) }); |
| 82 | +
|
| 83 | +// Pipe through a TransformStream |
| 84 | +const upper = ch.pipe(new TransformStream({ |
| 85 | + transform: (chunk, ctrl) => ctrl.enqueue(chunk.toUpperCase()), |
| 86 | +})); |
| 87 | +
|
| 88 | +ch.pause(); // backpressure |
| 89 | +ch.resume(); |
| 90 | +await ch.close();`}</pre> |
| 91 | + <table> |
| 92 | + <thead><tr><th>Member</th><th>Description</th></tr></thead> |
| 93 | + <tbody> |
| 94 | + <tr><td><code>status</code></td><td><code>ReadonlySignal<ChannelStatus></code> — <code>idle | open | paused | closed | errored</code></td></tr> |
| 95 | + <tr><td><code>source.readable</code></td><td>WHATWG <code>ReadableStream<ChannelFrame<Out>></code></td></tr> |
| 96 | + <tr><td><code>sink.writable</code></td><td>WHATWG <code>WritableStream<ChannelFrame<In>></code></td></tr> |
| 97 | + <tr><td><code>send(data)</code></td><td>Enqueue a frame; awaits if paused</td></tr> |
| 98 | + <tr><td><code>observe()</code></td><td>Non-locking Observable over outbound frames</td></tr> |
| 99 | + <tr><td><code>pipe(transform)</code></td><td>Returns a new downstream Channel</td></tr> |
| 100 | + </tbody> |
| 101 | + </table> |
| 102 | + |
| 103 | + <h2>Session</h2> |
| 104 | + <p> |
| 105 | + A lifecycle container that owns channels and dispatches actions. |
| 106 | + </p> |
| 107 | + <pre>{`import { createSession } from "@muix/core"; |
| 108 | +
|
| 109 | +const session = createSession({ id: "chat" }); |
| 110 | +await session.start(); |
| 111 | +
|
| 112 | +const ch = session.addChannel<string>("messages"); |
| 113 | +await ch.open(); |
| 114 | +
|
| 115 | +const action = session.dispatch({ |
| 116 | + id: "fetch-summary", |
| 117 | + execute: async function* (signal) { |
| 118 | + yield { type: "progress", percent: 50 }; |
| 119 | + return "done"; |
| 120 | + }, |
| 121 | +}); |
| 122 | +
|
| 123 | +await session.suspend(); |
| 124 | +await session.resume(); |
| 125 | +await session.terminate();`}</pre> |
| 126 | + |
| 127 | + <h2>Action</h2> |
| 128 | + <p> |
| 129 | + Cancellable async unit of work. Uses <code>AbortSignal</code> for |
| 130 | + cancellation. Named <code>toPromise()</code> (not <code>.then()</code>) |
| 131 | + to avoid the JavaScript thenable trap. |
| 132 | + </p> |
| 133 | + <pre>{`const action = session.dispatch({ |
| 134 | + id: "my-action", |
| 135 | + execute: async function* (signal) { |
| 136 | + for (let i = 0; i < 10; i++) { |
| 137 | + if (signal.aborted) return; |
| 138 | + yield { type: "progress", percent: i * 10 }; |
| 139 | + } |
| 140 | + return "result"; |
| 141 | + }, |
| 142 | +}); |
| 143 | +
|
| 144 | +// Reactive status |
| 145 | +action.status.observe().subscribe({ next: console.log }); |
| 146 | +
|
| 147 | +action.cancel("user cancelled"); |
| 148 | +const result = await action.toPromise();`}</pre> |
| 149 | + </> |
| 150 | + ); |
| 151 | +} |
0 commit comments