Sync / foldkit-sync/metakit

SyncDeclared

namespace · packages/sync/src/metakit.ts:120

const SyncDeclared = { holds, transition, fromMachine }

Laws and transitions of a Sync document declared as metakit expressions. Named apart from foldkit-form/metakit's Declared, so one file can import both.

In the guides

Members

SyncDeclared.holds

function · packages/sync/src/metakit.ts:36

holds<Shared>(
  expression: Expr.Expr<Model<Shared>, boolean>,
): Law<Shared, string>['holds']

A law's holds from an expression over the snapshot an operation leaves, seen as model. It holds only where the expression is true: a snapshot it is null over, or that metakit refuses to evaluate it over (an integer result past ±(2^53−1)), breaks the law.

SyncDeclared.transition

function · packages/sync/src/metakit.ts:52

transition<Shared, Message>(
  declared: Transition.Transition<Model<Shared>, Message>,
): DeclaredTransition<Shared, Message>

A durable Message's replay from a metakit Transition over { model }. A Message whose guard is false is refused. make rejects a guard proved unreachable under the laws written with holds, or a transition proved to break one. The check, its words and the replay by tag all live here, so a document that declares no transition carries none of them.

SyncDeclared.fromMachine

function · packages/sync/src/machine.ts:47

fromMachine<
  Root,
  Shared,
  Transitions extends Readonly<Record<string, unknown>>,
  const Tags extends ReadonlyArray<keyof Transitions & string>,
>(
  machine: Machine<Root, Transitions>,
  tags: Tags,
  shared: {
    readonly dependencies: ReadonlyArray<ReadonlyArray<string>>
    readonly get: (root: Root) => Shared
    readonly set: (root: Root, shared: Shared) => Root
  },
  initial: Root,
): {
  readonly [K in Tags[number]]: DeclaredTransition<
    Shared,
    InputOf<Transitions[K]>
  >
}

Extracts the durable subset of an application's metakit Machine for Sync.forApplication(App).make({ transitions }), throwing at definition for a tag whose transition:

  • is not a transition of the Machine;
  • emits a Command (durable transitions are state-only);
  • declares supplied values (a replay passes none, so every replay would be refused);
  • has a read it does not bound (an opaque term may read outside the shared slice);
  • reads or writes a path that is not inside the shared slice (the shared slice's path must be a prefix of it, so a write to a parent of the slice is refused).

It does not check the document's laws, or for an unreachable guard: a replay runs on the shared slice over initial, as forApplication does. Use SyncDeclared.transition for those checks.