Learn / Events, folds and gates

Add a server gate to a fold

Wrap App with forApplication(App) from foldkit-sync to declare which slice syncs (shared), which Messages travel (durable), who may send each one (authorize), and what must always remain true (laws):

ts
type Principal = { readonly actorId: string; readonly canWrite: boolean }

const TodoSync = forApplication(App)
  .withPrincipal<Principal>()
  .make({
    documentId: DocumentId.make('todos'),
    shared: Projection.pick(App.model.todos),
    durable: MessageSet.make(App, [
      Message.CreatedTodo,
      Message.CompletedTodo,
      Message.ClearedTodos,
    ]),
    authorize: {
      CompletedTodo: ({ message, shared }) => shared.todos.some(todo => todo.id === message.id),
    },
    laws: {
      atMost100Todos: {
        holds: ({ after }) => after.todos.length <= 100,
        message: 'A list holds 100 todos',
        path: ['todos'],
      },
    },
  })

Each option plays a specific role:

  • shared: Names the Model slice kept in sync across replicas. Because filter is omitted, it stays local to each device.
  • durable: Lists the MessageSet allowed in the outbox and server journal. ChoseFilter is omitted, so changing the filter updates the local screen without syncing.
  • authorize: Checks permission on the server inside the commit against the authoritative snapshot before the operation.
  • laws: Checks invariants against the snapshot after every durable operation.

TodoSync describes the sync contract as data; Replicated state shows how clients and servers execute it.