Live shared state
Clients receive the current state, follow changes, and catch up after a disconnect.
Yielded Sync
Keep clients in step with server state through live updates and typed actions.
Live demo
Live shared state
Clients receive the current state, follow changes, and catch up after a disconnect.
Typed actions
Define schemas for state, actions, and events. Your server decides which changes to accept.
Responsive edits
Show changes immediately while the server confirms them, and retry interrupted commands safely.
Define the source once. The server decides each reservation; clients predict it.
import { Action, Source } from "@yielded/sync";import { Schema } from "effect";
export class SoldOut extends Schema.TaggedError<SoldOut>()("SoldOut", {}) {}
export const Seats = Source.make({ kind: "seats", id: Schema.NonEmptyString.pipe(Schema.brand("ShowId")), schemaVersion: 1, snapshot: Schema.Struct({ seats: Schema.Array(Schema.NullOr(Schema.String)) }), event: Schema.Struct({ seat: Schema.Int, guest: Schema.String }), actions: [ Action.make("reserve", { payload: Schema.Struct({ guest: Schema.String }), success: Schema.Struct({ seat: Schema.Int }), error: SoldOut, }), ],});import { Server } from "@yielded/sync/server";import { Effect } from "effect";
import { authorize, Principal } from "./auth";import { Seats, SoldOut } from "./seats";
export const SeatsServer = Server.make(Seats, { principal: Principal, state: Seats.spec.snapshot, initialize: Effect.succeed({ seats: [null, null, null] }), snapshot: (state) => state, authorize, actions: { reserve: Effect.fn(function* ({ state, payload }) { const seat = state.seats.indexOf(null);
if (seat === -1) return yield* SoldOut.make({}); const seats = state.seats.map((guest, index) => (index === seat ? payload.guest : guest));
return { state: { seats }, events: [{ seat, guest: payload.guest }], result: { seat } }; }), },});import { Client } from "@yielded/sync/client";
import { Seats } from "./seats";
export const SeatsClient = Client.definition(Seats, { applyEvent: ({ seats }, event) => ({ seats: seats.map((guest, index) => (index === event.seat ? event.guest : guest)), }), optimistic: { // Predict the next free seat. The server's event confirms or replaces it. reserve: ({ seats }, payload) => { const seat = seats.indexOf(null);
return { seats: seats.map((guest, index) => (index === seat ? payload.guest : guest)), }; }, },});Your app supplies Principal and authorize in auth.ts. Step through
each scenario to see how Sync
handles conflicts, lost responses, and offline edits.
For a complete application, run the React board.