@nustackdev/ui-core 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,12 @@
1
1
  {
2
2
  "name": "@nustackdev/ui-core",
3
3
  "description": "Wire protocol for the Nu.",
4
- "version": "0.2.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/nustackdev/nu.git",
7
+ "directory": "pkgs/ts/ui-core"
8
+ },
9
+ "version": "0.2.1",
5
10
  "type": "module",
6
11
  "main": "./src/index.ts",
7
12
  "types": "./src/index.ts",
package/src/tree.test.ts CHANGED
@@ -304,6 +304,38 @@ describe("dispatch", () => {
304
304
  expect(store.getState().getIn(["t"])?.props.rows).toEqual([{ id: 1 }, { id: 2 }]);
305
305
  });
306
306
 
307
+ it("creates the node from a custom op's chain, then runs its handler", () => {
308
+ const behaviours: Record<string, Behaviour> = {
309
+ Card: {
310
+ handlers: {
311
+ set_title: (ctx, payload) =>
312
+ ctx.update((props) => {
313
+ props.title = String(payload);
314
+ }),
315
+ },
316
+ },
317
+ };
318
+ const store = createTreeStore({ resolve: (t) => behaviours[t] });
319
+ store
320
+ .getState()
321
+ .dispatch(
322
+ frame("set_title", ["col", "card"], "Hi", [
323
+ step("col", "Column"),
324
+ step("card", "Card", { title: "", footer: "" }),
325
+ ]),
326
+ );
327
+ expect(store.getState().getIn(["col"])?.type).toBe("Column");
328
+ expect(store.getState().getIn(["col", "card"])?.props).toEqual({ title: "Hi", footer: "" });
329
+ });
330
+
331
+ it("still reports a custom op with no chain on a missing node", () => {
332
+ const onError = vi.fn();
333
+ const store = createTreeStore({ onError });
334
+ store.getState().dispatch(frame("set_title", ["nope"], "Hi"));
335
+ expect(onError).toHaveBeenCalledOnce();
336
+ expect(store.getState().getIn(["nope"])).toBeNull();
337
+ });
338
+
307
339
  it("gives a type's own write handler the raw payload", () => {
308
340
  const behaviours: Record<string, Behaviour> = {
309
341
  InputRef: {
@@ -348,14 +380,14 @@ describe("dispatch", () => {
348
380
  it("lets a type answer a read itself", () => {
349
381
  const sent: TreeFrame[] = [];
350
382
  const behaviours: Record<string, Behaviour> = {
351
- ProseRef: {
383
+ MarkdownRef: {
352
384
  handlers: {
353
385
  read: (ctx) => ctx.send(OPS.read, `md:${ctx.node.props.value}`, ctx.frame.id),
354
386
  },
355
387
  },
356
388
  };
357
389
  const store = createTreeStore({ send: (f) => sent.push(f), resolve: (t) => behaviours[t] });
358
- store.getState().write([step("p", "ProseRef", { value: "hi" })]);
390
+ store.getState().write([step("p", "MarkdownRef", { value: "hi" })]);
359
391
  store.getState().dispatch({ op: OPS.read, ref: ["p"], payload: null, id: "f2" });
360
392
  expect(sent).toEqual([{ op: OPS.read, ref: ["p"], payload: "md:hi", id: "f2" }]);
361
393
  });
package/src/tree.ts CHANGED
@@ -312,6 +312,11 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
312
312
  }
313
313
  return;
314
314
  }
315
+ // Any other op that carries a chain brings its node into being
316
+ // first, the same way a write does: a Card's `set_title` may be
317
+ // the first frame that ever names that Card. A frame with no
318
+ // chain is addressed at a node that should already be there.
319
+ if (frame.chain) api.write(chain);
315
320
  const node = api.getIn(frame.ref);
316
321
  if (!node) {
317
322
  onError(`no node for op "${frame.op}"`, frame.ref);