pulsify-react
v20.0.0
Published
Fine-grained React rendering for Pulsify documents.
Readme
pulsify-react
Fine-grained React rendering for Pulsify documents, with no dependency besides
React and pulsify-core.
import { usePulse } from "pulsify-react";
import { memo } from "react";
function Note({ note }: { note: typeof doc }) {
const snap = usePulse(note);
return (
<>
<input value={snap.title} onChange={(e) => (snap.title = e.target.value)} />
<ul>{snap.items.map((item) => <Item key={item.label} item={item} />)}</ul>
</>
);
}
const Item = memo(({ item }: { item: (typeof doc)["items"][number] }) => (
<li onClick={() => (item.done = !item.done)}>{item.label}</li>
));useLoadPulse(definition, id, options?) loads a pulse under <Suspense>:
function NotePage({ id }: { id: string }) {
const note = useLoadPulse(Note, id); // suspends until loaded
return <h1>{note.title}</h1>;
}It returns a pulse loaded or created already at once, loads again when the ID
changes, and throws a failed load to the nearest error boundary, loading again
on a later render (React's immediate retry sees the same error). Loads are
kept per scope: components loading one ID in different scopes (options.scope)
never wait for, or fail with, each other's load. Then it works as usePulse.
usePulse(pulse) returns a proxy of the live pulse that records what the
component reads. Local and remote changes re-render it only when one of those
values changed. Writes go straight to the pulse.
- Objects, arrays, maps, counters, rich/plain text and trees keep their
identity while nothing read below them changes, including after an array
move, so
memochildren skip. A child reading a value it received records the read for the component that calledusePulse. - Counters, plain/rich text and trees are compared by node revision in O(1):
their readers re-render when their content changes. Maps track per key
(
get,has),size, and key iteration (keys,entries,values,forEach). Bind editors withpulsify-prosemirror,pulsify-lexicalorpulsify-codemirror. - The pulse itself is renewed on every render, so a
memochild given the whole pulse renders with its parent. Pass it the part it shows, or let it callusePulseon what it received. - A nested object passed to a skipped
memochild keeps that child's reads until something below it changes, which costs at most one extra render. - Pulses live on the client. During server rendering, both hooks throw and
the nearest
<Suspense>boundary renders its fallback, then the client renders the content. - Do not write during render. Reads made in event handlers or effects are tracked too and can cause extra renders.
Presence
import { usePulsePresence } from "pulsify-react";
function Cursors({ project }: { project: ProjectPulse }) {
const cursors = usePulsePresence(project, Cursor); // a definePresence channel
return (
<div onPointerMove={(e) => cursors.set({ x: e.clientX, y: e.clientY })}>
{cursors.others.map(({ peer, state }) => (
<i key={peer} style={{ left: state.x, top: state.y }} />
))}
</div>
);
}usePulsePresence(pulse, channel) joins the channel while the component is
mounted and returns { others, mine, set }. The component re-renders when
the others or its own state change, not when the document does. A peer whose
state did not change keeps the same object, so memo children skip.
setkeeps its identity between renders, while the pulse and the channel stay the same. Call it from handlers and effects, never during render. A state set by a child's effect before the channel is joined is kept and sent.- Joins are counted: several components can use the same channel, and the state is removed when the last one unmounts.
- Like
usePulse, it renders on the client only.
