@pogly/spacetimedb-predict
v0.1.1
Published
Optimistic updates / client-side prediction for SpacetimeDB clients: predictions layered over the client cache, rebased on every server change, rolled back when the server says no. React hooks, and a ctx.db shim to run a TS module's own reducer logic as t
Maintainers
Readme
@pogly/spacetimedb-predict
Optimistic updates and client-side prediction for SpacetimeDB clients (experimental). Call a reducer and the UI changes at once; the prediction is re-run on every server change until the reply comes back, then the real rows take over without a flicker. Refused or timed-out calls roll back.
npm install @pogly/spacetimedb-predictimport { predict } from '@pogly/spacetimedb-predict';
const p = predict({ task: { table: conn.db.task, key: (t) => t.id, autoInc: 'id' } });
await p.call(
() => conn.reducers.toggleTask({ id }),
(d) => {
const t = d.task.find(id);
if (t) d.task.update({ ...t, done: !t.done });
}
);
p.subscribe(() => render(p.table('task').rows()));- React:
usePredicted(p, 'task')andusePredictedCall(p, send, prediction)from@pogly/spacetimedb-predict/react. - Shared reducer logic (TypeScript modules):
shimDb(draft, { task: { unique: ['id'] } })from@pogly/spacetimedb-predict/shimgives the logic actx.dbover the prediction.
The full README, a demo and the measurements are in the repository.
