deep signals: emit nested patches when adding new object too

refactor
Laurin Weger 1 day ago
parent fa87233fc9
commit 177e2f2739
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 1297
      sdk/js/alien-deepsignals/src/deepSignal.ts
  2. 729
      sdk/js/alien-deepsignals/src/test/watchPatches.test.ts

File diff suppressed because it is too large Load Diff

@ -1,357 +1,424 @@
import { describe, it, expect } from "vitest"; import { describe, it, expect } from "vitest";
import { import {
deepSignal, deepSignal,
setSetEntrySyntheticId, setSetEntrySyntheticId,
addWithId, addWithId,
DeepPatch, DeepPatch,
} from "../deepSignal"; } from "../deepSignal";
import { watch, observe } from "../watch"; import { watch, observe } from "../watch";
describe("watch (patch mode)", () => { describe("watch (patch mode)", () => {
it("emits set patches with correct paths and batching", async () => { it("emits set patches with correct paths and batching", async () => {
const state = deepSignal({ a: { b: 1 }, arr: [1, { x: 2 }] }); const state = deepSignal({ a: { b: 1 }, arr: [1, { x: 2 }] });
const received: DeepPatch[][] = []; const received: DeepPatch[][] = [];
const { stopListening: stop } = watch(state, ({ patches }) => { const { stopListening: stop } = watch(state, ({ patches }) => {
received.push(patches); received.push(patches);
});
state.a.b = 2;
(state.arr[1] as any).x = 3;
state.arr.push(5);
await Promise.resolve();
expect(received.length).toBe(1);
const batch = received[0];
const paths = batch.map((p) => p.path.join(".")).sort();
expect(paths).toContain("a.b");
expect(paths).toContain("arr.1.x");
expect(paths).toContain("arr.2");
const addOps = batch.filter((p) => p.op === "add").length;
expect(addOps).toBe(batch.length);
stop();
}); });
state.a.b = 2;
(state.arr[1] as any).x = 3;
state.arr.push(5);
await Promise.resolve();
expect(received.length).toBe(1);
const batch = received[0];
const paths = batch.map((p) => p.path.join(".")).sort();
expect(paths).toContain("a.b");
expect(paths).toContain("arr.1.x");
expect(paths).toContain("arr.2");
const addOps = batch.filter((p) => p.op === "add").length;
expect(addOps).toBe(batch.length);
stop();
});
it("emits delete patches without value", async () => { it("emits delete patches without value", async () => {
const state = deepSignal<{ a: { b?: number }; c?: number }>({ const state = deepSignal<{ a: { b?: number }; c?: number }>({
a: { b: 1 }, a: { b: 1 },
c: 2, c: 2,
});
const out: DeepPatch[][] = [];
const { stopListening: stop } = watch(state, ({ patches }) =>
out.push(patches)
);
delete state.a.b;
delete state.c;
await Promise.resolve();
expect(out.length).toBe(1);
const [batch] = out;
const deletePatches = batch.filter((p) => p.op === "remove");
const delPaths = deletePatches.map((p) => p.path.join(".")).sort();
expect(delPaths).toEqual(["a.b", "c"]);
deletePatches.forEach((p: any) => expect(p.value).toBeUndefined());
stop();
}); });
const out: DeepPatch[][] = [];
const { stopListening: stop } = watch(state, ({ patches }) =>
out.push(patches)
);
delete state.a.b;
delete state.c;
await Promise.resolve();
expect(out.length).toBe(1);
const [batch] = out;
const deletePatches = batch.filter((p) => p.op === "remove");
const delPaths = deletePatches.map((p) => p.path.join(".")).sort();
expect(delPaths).toEqual(["a.b", "c"]);
deletePatches.forEach((p: any) => expect(p.value).toBeUndefined());
stop();
});
it("observe patch mode mirrors watch patch mode", async () => { it("observe patch mode mirrors watch patch mode", async () => {
const state = deepSignal({ a: 1 }); const state = deepSignal({ a: 1 });
const wp: DeepPatch[][] = []; const wp: DeepPatch[][] = [];
const ob: DeepPatch[][] = []; const ob: DeepPatch[][] = [];
const { stopListening: stop1 } = watch(state, ({ patches }) => const { stopListening: stop1 } = watch(state, ({ patches }) =>
wp.push(patches) wp.push(patches)
); );
const { stopListening: stop2 } = observe(state, ({ patches }) => const { stopListening: stop2 } = observe(state, ({ patches }) =>
ob.push(patches) ob.push(patches)
); );
state.a = 2; state.a = 2;
await Promise.resolve(); await Promise.resolve();
expect(wp.length).toBe(1); expect(wp.length).toBe(1);
expect(ob.length).toBe(1); expect(ob.length).toBe(1);
expect(wp[0][0].path.join(".")).toBe("a"); expect(wp[0][0].path.join(".")).toBe("a");
stop1(); stop1();
stop2(); stop2();
}); });
it("filters out patches from other roots", async () => { it("filters out patches from other roots", async () => {
const a = deepSignal({ x: 1 }); const a = deepSignal({ x: 1 });
const b = deepSignal({ y: 2 }); const b = deepSignal({ y: 2 });
const out: DeepPatch[][] = []; const out: DeepPatch[][] = [];
const { stopListening: stop } = watch(a, ({ patches }) => const { stopListening: stop } = watch(a, ({ patches }) =>
out.push(patches) out.push(patches)
); );
b.y = 3; b.y = 3;
a.x = 2; a.x = 2;
await Promise.resolve(); await Promise.resolve();
expect(out.length).toBe(1); expect(out.length).toBe(1);
expect(out[0][0].path.join(".")).toBe("x"); expect(out[0][0].path.join(".")).toBe("x");
stop(); stop();
}); });
it("emits patches for Set structural mutations (add/delete)", async () => { it("emits patches for Set structural mutations (add/delete)", async () => {
const state = deepSignal<{ s: Set<number> }>({ s: new Set([1, 2]) }); const state = deepSignal<{ s: Set<number> }>({ s: new Set([1, 2]) });
const batches: DeepPatch[][] = []; const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(state, ({ patches }) => const { stopListening: stop } = watch(state, ({ patches }) =>
batches.push(patches) batches.push(patches)
); );
state.s.add(3); state.s.add(3);
state.s.delete(1); state.s.delete(1);
await Promise.resolve(); await Promise.resolve();
expect(batches.length >= 1).toBe(true); expect(batches.length >= 1).toBe(true);
const allPaths = batches.flatMap((b) => b.map((p) => p.path.join("."))); const allPaths = batches.flatMap((b) => b.map((p) => p.path.join(".")));
expect(allPaths.some((p) => p.startsWith("s."))).toBe(true); expect(allPaths.some((p) => p.startsWith("s."))).toBe(true);
stop(); stop();
}); });
it("emits patches for nested objects added after initialization", async () => { it("emits patches for nested objects added after initialization", async () => {
const state = deepSignal<{ root: any }>({ root: {} }); const state = deepSignal<{ root: any }>({ root: {} });
const patches: DeepPatch[][] = []; const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(state, ({ patches: batch }) => const { stopListening: stop } = watch(state, ({ patches: batch }) =>
patches.push(batch) patches.push(batch)
); );
state.root.child = { level: { value: 1 } }; state.root.child = { level: { value: 1 }, l1: "val" };
state.root.child.level.value = 2; await Promise.resolve();
await Promise.resolve(); const flat = patches.flat().map((p) => p.path.join("."));
const flat = patches.flat().map((p) => p.path.join(".")); expect(flat).toContain("root.child");
expect(flat).toContain("root.child"); expect(flat).toContain("root.child.level.value");
expect(flat).toContain("root.child.level.value"); stop();
stop(); });
});
it("emits structural patches for sets of sets", async () => { it("emits patches for deeply nested arrays and objects", async () => {
const innerA = new Set<any>([{ id: "node1", x: 1 }]); const state = deepSignal<{ data: any }>({ data: null });
const s = new Set<any>([innerA]); const patches: DeepPatch[][] = [];
const state = deepSignal<{ graph: Set<any> }>({ graph: s }); const { stopListening: stop } = watch(state, ({ patches: batch }) =>
const batches: DeepPatch[][] = []; patches.push(batch)
const { stopListening: stop } = watch(state, ({ patches }) => );
batches.push(patches) state.data = {
); users: [
const innerB = new Set<any>([{ id: "node2", x: 5 }]); {
state.graph.add(innerB); id: 1,
([...innerA][0] as any).x = 2; profile: { name: "Alice", settings: { theme: "dark" } },
await Promise.resolve(); },
const pathStrings = batches.flat().map((p) => p.path.join(".")); {
expect(pathStrings.some((p) => p.startsWith("graph."))).toBe(true); id: 2,
stop(); profile: { name: "Bob", settings: { theme: "light" } },
}); },
],
meta: { count: 2, active: true },
};
await Promise.resolve();
it("tracks deep nested object mutation inside a Set entry after iteration", async () => { const flat = patches.flat().map((p) => p.path.join("."));
const rawEntry = { id: "n1", data: { val: 1 } }; // Check for root object
const st = deepSignal({ bag: new Set<any>([rawEntry]) }); expect(flat).toContain("data");
const collected: DeepPatch[][] = []; // Check for nested array
const { stopListening: stop } = watch(st, ({ patches }) => expect(flat).toContain("data.users");
collected.push(patches) // Check for array elements
); expect(flat).toContain("data.users.0");
let proxied: any; expect(flat).toContain("data.users.1");
for (const e of st.bag.values()) { // Check for deeply nested properties
proxied = e; expect(flat).toContain("data.users.0.profile.settings.theme");
e.data.val; expect(flat).toContain("data.users.1.profile.settings.theme");
} expect(flat).toContain("data.meta.count");
proxied.data.val = 2; expect(flat).toContain("data.meta.active");
await Promise.resolve(); stop();
const flat = collected.flat().map((p: DeepPatch) => p.path.join(".")); });
expect(flat.some((p: string) => p.endsWith("n1.data.val"))).toBe(true);
stop();
});
it("allows custom synthetic id for Set entry", async () => { it("emits patches for Set with nested objects added as one operation", async () => {
const node = { name: "x" }; const state = deepSignal<{ container: any }>({ container: {} });
const state = deepSignal({ s: new Set<any>() }); const patches: DeepPatch[][] = [];
const collected2: DeepPatch[][] = []; const { stopListening: stop } = watch(state, ({ patches: batch }) =>
const { stopListening: stop } = watch(state, ({ patches }) => patches.push(batch)
collected2.push(patches) );
); state.container.items = new Set([
addWithId(state.s as any, node, "custom123"); { id: "a", data: { nested: { value: 1 } } },
await Promise.resolve(); { id: "b", data: { nested: { value: 2 } } },
const flat = collected2.flat().map((p: DeepPatch) => p.path.join(".")); ]);
expect(flat.some((p: string) => p === "s.custom123")).toBe(true); await Promise.resolve();
stop();
});
describe("Set", () => { const flat = patches.flat().map((p) => p.path.join("."));
it("emits single structural patch on Set.clear()", async () => {
const st = deepSignal({ s: new Set<any>() }); // Check for the Set itself
addWithId(st.s as any, { id: "a", x: 1 }, "a"); expect(flat).toContain("container.items");
addWithId(st.s as any, { id: "b", x: 2 }, "b"); // Check for Set entries (using their id as synthetic key)
const batches: DeepPatch[][] = []; expect(flat.some((p) => p.startsWith("container.items.a"))).toBe(true);
const { stopListening: stop } = watch(st, ({ patches }) => expect(flat.some((p) => p.startsWith("container.items.b"))).toBe(true);
batches.push(patches) // Check for deeply nested properties within Set entries
); expect(flat).toContain("container.items.a.data.nested.value");
st.s.clear(); expect(flat).toContain("container.items.b.data.nested.value");
await Promise.resolve(); stop();
const all = batches.flat().map((p) => p.path.join("."));
expect(all).toEqual(["s"]);
stop();
});
it("emits delete patch for object entry", async () => {
const st = deepSignal({ s: new Set<any>() });
const obj = { id: "n1", x: 1 };
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(obj);
st.s.delete(obj);
await Promise.resolve();
const all = patches
.flat()
.filter((p) => p.op === "remove")
.map((p) => p.path.join("."));
expect(all).toContain("s.n1");
stop();
});
it("does not emit patch for duplicate add", async () => {
const st = deepSignal({ s: new Set<number>([1]) });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(1);
await Promise.resolve();
expect(patches.length).toBe(0);
stop();
});
it("does not emit patch deleting non-existent entry", async () => {
const st = deepSignal({ s: new Set<number>([1]) });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.delete(2);
await Promise.resolve();
expect(patches.length).toBe(0);
stop();
});
it("addWithId primitive returns primitive and emits patch with primitive key", async () => {
const st = deepSignal({ s: new Set<any>() });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
const ret = addWithId(st.s as any, 5, "ignored");
expect(ret).toBe(5);
await Promise.resolve();
const paths = patches.flat().map((p) => p.path.join("."));
expect(paths).toContain("s.5");
stop();
});
it("setSetEntrySyntheticId applies custom id without helper", async () => {
const st = deepSignal({ s: new Set<any>() });
const obj = { name: "x" };
setSetEntrySyntheticId(obj, "customX");
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(obj);
await Promise.resolve();
const paths = patches.flat().map((p) => p.path.join("."));
expect(paths).toContain("s.customX");
stop();
});
it("values/entries/forEach proxy nested mutation", async () => {
const st = deepSignal({ s: new Set<any>() });
const entry = addWithId(st.s as any, { id: "e1", inner: { v: 1 } }, "e1");
const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches }) =>
batches.push(patches)
);
for (const e of st.s.values()) {
e.inner.v;
}
entry.inner.v = 2;
await Promise.resolve();
const vPaths = batches.flat().map((p) => p.path.join("."));
expect(vPaths.some((p) => p.endsWith("e1.inner.v"))).toBe(true);
stop();
}); });
it("raw reference mutation produces no deep patch while proxied does", async () => {
const raw = { id: "id1", data: { x: 1 } }; it("emits structural patches for sets of sets", async () => {
const st = deepSignal({ s: new Set<any>([raw]) }); const innerA = new Set<any>([{ id: "node1", x: 1 }]);
const batches: DeepPatch[][] = []; const s = new Set<any>([innerA]);
const { stopListening: stop } = watch(st, ({ patches }) => const state = deepSignal<{ graph: Set<any> }>({ graph: s });
batches.push(patches) const batches: DeepPatch[][] = [];
); const { stopListening: stop } = watch(state, ({ patches }) =>
raw.data.x = 2; batches.push(patches)
await Promise.resolve(); );
const afterRaw = batches.flat().map((p) => p.path.join(".")); const innerB = new Set<any>([{ id: "node2", x: 5 }]);
expect(afterRaw.some((p) => p.endsWith("id1.data.x"))).toBe(false); state.graph.add(innerB);
let proxied: any; ([...innerA][0] as any).x = 2;
for (const e of st.s.values()) proxied = e; await Promise.resolve();
proxied.data.x = 3; const pathStrings = batches.flat().map((p) => p.path.join("."));
await Promise.resolve(); expect(pathStrings.some((p) => p.startsWith("graph."))).toBe(true);
const afterProxied = batches.flat().map((p) => p.path.join(".")); stop();
expect(afterProxied.some((p) => p.endsWith("id1.data.x"))).toBe(true);
stop();
}); });
it("synthetic id collision assigns unique blank node id", async () => {
const st = deepSignal({ s: new Set<any>() }); it("tracks deep nested object mutation inside a Set entry after iteration", async () => {
const a1 = { id: "dup", v: 1 }; const rawEntry = { id: "n1", data: { val: 1 } };
const a2 = { id: "dup", v: 2 }; const st = deepSignal({ bag: new Set<any>([rawEntry]) });
const patches: DeepPatch[][] = []; const collected: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) => const { stopListening: stop } = watch(st, ({ patches }) =>
patches.push(batch) collected.push(patches)
); );
st.s.add(a1); let proxied: any;
st.s.add(a2); for (const e of st.bag.values()) {
await Promise.resolve(); proxied = e;
const keys = patches e.data.val;
.flat() }
.filter((p) => p.op === "add") proxied.data.val = 2;
.map((p) => p.path.slice(-1)[0]); await Promise.resolve();
expect(new Set(keys).size).toBe(2); const flat = collected.flat().map((p: DeepPatch) => p.path.join("."));
stop(); expect(flat.some((p: string) => p.endsWith("n1.data.val"))).toBe(true);
stop();
}); });
it("allows Array.from() and spread on Set without brand errors and tracks nested mutation", async () => { it("allows custom synthetic id for Set entry", async () => {
const st = deepSignal({ const node = { name: "x" };
s: new Set<any>([{ id: "eIter", inner: { v: 1 } }]), const state = deepSignal({ s: new Set<any>() });
}); const collected2: DeepPatch[][] = [];
// Regression: previously 'values method called on incompatible Proxy' was thrown here. const { stopListening: stop } = watch(state, ({ patches }) =>
const arr = Array.from(st.s); collected2.push(patches)
expect(arr.length).toBe(1); );
expect(arr[0].inner.v).toBe(1); addWithId(state.s as any, node, "custom123");
const spread = [...st.s]; await Promise.resolve();
expect(spread[0].inner.v).toBe(1); const flat = collected2.flat().map((p: DeepPatch) => p.path.join("."));
const batches: DeepPatch[][] = []; expect(flat.some((p: string) => p === "s.custom123")).toBe(true);
const { stopListening: stop } = watch(st, ({ patches }) => stop();
batches.push(patches)
);
spread[0].inner.v = 2; // mutate nested field of iterated (proxied) entry
await Promise.resolve();
const flat = batches.flat().map((p) => p.path.join("."));
expect(flat.some((p) => p.endsWith("eIter.inner.v"))).toBe(true);
stop();
}); });
});
describe("Arrays & mixed batch", () => { describe("Set", () => {
it("emits patches for splice/unshift/shift in single batch", async () => { it("emits single structural patch on Set.clear()", async () => {
const st = deepSignal({ arr: [1, 2, 3] }); const st = deepSignal({ s: new Set<any>() });
const batches: DeepPatch[][] = []; addWithId(st.s as any, { id: "a", x: 1 }, "a");
const { stopListening: stop } = watch(st, ({ patches }) => addWithId(st.s as any, { id: "b", x: 2 }, "b");
batches.push(patches) const batches: DeepPatch[][] = [];
); const { stopListening: stop } = watch(st, ({ patches }) =>
st.arr.splice(1, 1, 99, 100); batches.push(patches)
st.arr.unshift(0); );
st.arr.shift(); st.s.clear();
await Promise.resolve(); await Promise.resolve();
const paths = batches.flat().map((p) => p.path.join(".")); const all = batches.flat().map((p) => p.path.join("."));
expect(paths.some((p) => p.startsWith("arr."))).toBe(true); expect(all).toEqual(["s"]);
stop(); stop();
});
it("emits delete patch for object entry", async () => {
const st = deepSignal({ s: new Set<any>() });
const obj = { id: "n1", x: 1 };
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(obj);
st.s.delete(obj);
await Promise.resolve();
const all = patches
.flat()
.filter((p) => p.op === "remove")
.map((p) => p.path.join("."));
expect(all).toContain("s.n1");
stop();
});
it("does not emit patch for duplicate add", async () => {
const st = deepSignal({ s: new Set<number>([1]) });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(1);
await Promise.resolve();
expect(patches.length).toBe(0);
stop();
});
it("does not emit patch deleting non-existent entry", async () => {
const st = deepSignal({ s: new Set<number>([1]) });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.delete(2);
await Promise.resolve();
expect(patches.length).toBe(0);
stop();
});
it("addWithId primitive returns primitive and emits patch with primitive key", async () => {
const st = deepSignal({ s: new Set<any>() });
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
const ret = addWithId(st.s as any, 5, "ignored");
expect(ret).toBe(5);
await Promise.resolve();
const paths = patches.flat().map((p) => p.path.join("."));
expect(paths).toContain("s.5");
stop();
});
it("setSetEntrySyntheticId applies custom id without helper", async () => {
const st = deepSignal({ s: new Set<any>() });
const obj = { name: "x" };
setSetEntrySyntheticId(obj, "customX");
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(obj);
await Promise.resolve();
const paths = patches.flat().map((p) => p.path.join("."));
expect(paths).toContain("s.customX");
stop();
});
it("values/entries/forEach proxy nested mutation", async () => {
const st = deepSignal({ s: new Set<any>() });
const entry = addWithId(
st.s as any,
{ id: "e1", inner: { v: 1 } },
"e1"
);
const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches }) =>
batches.push(patches)
);
for (const e of st.s.values()) {
e.inner.v;
}
entry.inner.v = 2;
await Promise.resolve();
const vPaths = batches.flat().map((p) => p.path.join("."));
expect(vPaths.some((p) => p.endsWith("e1.inner.v"))).toBe(true);
stop();
});
it("raw reference mutation produces no deep patch while proxied does", async () => {
const raw = { id: "id1", data: { x: 1 } };
const st = deepSignal({ s: new Set<any>([raw]) });
const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches }) =>
batches.push(patches)
);
raw.data.x = 2;
await Promise.resolve();
const afterRaw = batches.flat().map((p) => p.path.join("."));
expect(afterRaw.some((p) => p.endsWith("id1.data.x"))).toBe(false);
let proxied: any;
for (const e of st.s.values()) proxied = e;
proxied.data.x = 3;
await Promise.resolve();
const afterProxied = batches.flat().map((p) => p.path.join("."));
expect(afterProxied.some((p) => p.endsWith("id1.data.x"))).toBe(
true
);
stop();
});
it("synthetic id collision assigns unique blank node id", async () => {
const st = deepSignal({ s: new Set<any>() });
const a1 = { id: "dup", v: 1 };
const a2 = { id: "dup", v: 2 };
const patches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches: batch }) =>
patches.push(batch)
);
st.s.add(a1);
st.s.add(a2);
await Promise.resolve();
const keys = patches
.flat()
.filter((p) => p.op === "add")
.map((p) => p.path.slice(-1)[0]);
expect(new Set(keys).size).toBe(2);
stop();
});
it("allows Array.from() and spread on Set without brand errors and tracks nested mutation", async () => {
const st = deepSignal({
s: new Set<any>([{ id: "eIter", inner: { v: 1 } }]),
});
// Regression: previously 'values method called on incompatible Proxy' was thrown here.
const arr = Array.from(st.s);
expect(arr.length).toBe(1);
expect(arr[0].inner.v).toBe(1);
const spread = [...st.s];
expect(spread[0].inner.v).toBe(1);
const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches }) =>
batches.push(patches)
);
spread[0].inner.v = 2; // mutate nested field of iterated (proxied) entry
await Promise.resolve();
const flat = batches.flat().map((p) => p.path.join("."));
expect(flat.some((p) => p.endsWith("eIter.inner.v"))).toBe(true);
stop();
});
}); });
it("mixed object/array/Set mutations batch together", async () => {
const st = deepSignal({ o: { a: 1 }, arr: [1], s: new Set<any>() }); describe("Arrays & mixed batch", () => {
const batches: DeepPatch[][] = []; it("emits patches for splice/unshift/shift in single batch", async () => {
const { stopListening: stop } = watch(st, ({ patches }) => const st = deepSignal({ arr: [1, 2, 3] });
batches.push(patches) const batches: DeepPatch[][] = [];
); const { stopListening: stop } = watch(st, ({ patches }) =>
st.o.a = 2; batches.push(patches)
st.arr.push(2); );
addWithId(st.s as any, { id: "z", v: 1 }, "z"); st.arr.splice(1, 1, 99, 100);
await Promise.resolve(); st.arr.unshift(0);
expect(batches.length).toBe(1); st.arr.shift();
const paths = batches[0].map((p) => p.path.join(".")); await Promise.resolve();
expect(paths).toContain("o.a"); const paths = batches.flat().map((p) => p.path.join("."));
expect(paths).toContain("arr.1"); expect(paths.some((p) => p.startsWith("arr."))).toBe(true);
expect(paths.some((p) => p.startsWith("s."))).toBe(true); stop();
stop(); });
it("mixed object/array/Set mutations batch together", async () => {
const st = deepSignal({ o: { a: 1 }, arr: [1], s: new Set<any>() });
const batches: DeepPatch[][] = [];
const { stopListening: stop } = watch(st, ({ patches }) =>
batches.push(patches)
);
st.o.a = 2;
st.arr.push(2);
addWithId(st.s as any, { id: "z", v: 1 }, "z");
await Promise.resolve();
expect(batches.length).toBe(1);
const paths = batches[0].map((p) => p.path.join("."));
expect(paths).toContain("o.a");
expect(paths).toContain("arr.1");
expect(paths.some((p) => p.startsWith("s."))).toBe(true);
stop();
});
}); });
});
}); });

Loading…
Cancel
Save