cellplatform / platform-0.2.0

/sys (shared system modules)
Other
4 stars 2 forks source link

Cmd: Command/Invoke (Primitive) #192

Closed philcockfield closed 4 months ago

philcockfield commented 4 months ago

Automerge document wrapper that provides a Command type logic allowing functions to be invoked via changes to the document.

Example:

  type C = C1 | C2;
  type C1 = t.CmdType<'Foo', { foo: number }>;
  type C2 = t.CmdType<'Bar', { msg?: string }>;

  const cmd = Cmd.create<C>(doc);
  const events = cmd.events(dispose$);

  const sum = (params: P): R => ({ sum: params.a + params.b });
  const responses: t.CmdTx<C2>[] = [];
  events.tx.name('add').subscribe((e) => cmd.invoke('add:res', sum(e.params)));
  events.tx.name('add:res').subscribe((e) => responses.push(e));

  cmd.invoke('add', { a: 2, b: 3 });
  expect(responses[0].params.sum).to.eql(5);

  dispose();