commit
e9a3b2fe88
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,340 @@ |
|||||||
|
import { describe, it, expect } from "vitest"; |
||||||
|
import { deepSignal, DeepPatch, DeepSignalOptions } from "../deepSignal"; |
||||||
|
import { watch } from "../watch"; |
||||||
|
|
||||||
|
describe("deepSignal options", () => { |
||||||
|
describe("custom ID generator", () => { |
||||||
|
it("uses custom ID generator for objects without @id", async () => { |
||||||
|
let counter = 1000; |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => `custom-${counter++}`, |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ data: {} as any }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.data.user = { name: "Alice" }; |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
// Check that @id was assigned
|
||||||
|
expect((state.data.user as any)["@id"]).toBe("custom-1000"); |
||||||
|
|
||||||
|
// Check that patch was emitted for @id
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(flat).toContain("data.user.@id"); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("respects existing @id on objects", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => "should-not-be-used", |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ items: [] as any[] }, options); |
||||||
|
|
||||||
|
state.items.push({ "@id": "existing-123", value: 42 }); |
||||||
|
|
||||||
|
// Should use the existing @id
|
||||||
|
expect((state.items[0] as any)["@id"]).toBe("existing-123"); |
||||||
|
}); |
||||||
|
|
||||||
|
it("uses @id property from objects added to Sets", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => "fallback-id", |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ s: new Set<any>() }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
const obj = { "@id": "set-entry-1", data: "test" }; |
||||||
|
state.s.add(obj); |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
// Path should use the @id as synthetic key
|
||||||
|
expect(flat.some((p) => p.startsWith("s.set-entry-1"))).toBe(true); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("addIdToObjects option", () => { |
||||||
|
it("adds @id to all nested objects when enabled", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ root: {} as any }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.root.level1 = { |
||||||
|
level2: { |
||||||
|
level3: { value: "deep" }, |
||||||
|
}, |
||||||
|
}; |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
// Check all levels have @id
|
||||||
|
expect((state.root.level1 as any)["@id"]).toBeDefined(); |
||||||
|
expect((state.root.level1.level2 as any)["@id"]).toBeDefined(); |
||||||
|
expect( |
||||||
|
(state.root.level1.level2.level3 as any)["@id"] |
||||||
|
).toBeDefined(); |
||||||
|
|
||||||
|
// Check patches were emitted for all @id fields
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(flat).toContain("root.level1.@id"); |
||||||
|
expect(flat).toContain("root.level1.level2.@id"); |
||||||
|
expect(flat).toContain("root.level1.level2.level3.@id"); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("does not add @id when option is false", () => { |
||||||
|
const state = deepSignal({ data: { nested: {} } }); |
||||||
|
|
||||||
|
// Should not have @id
|
||||||
|
expect("@id" in (state.data as any)).toBe(false); |
||||||
|
expect("@id" in (state.data.nested as any)).toBe(false); |
||||||
|
}); |
||||||
|
|
||||||
|
it("adds @id to objects in arrays", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ items: [] as any[] }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.items.push({ name: "Item 1" }, { name: "Item 2" }); |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
// Both items should have @id
|
||||||
|
expect((state.items[0] as any)["@id"]).toBeDefined(); |
||||||
|
expect((state.items[1] as any)["@id"]).toBeDefined(); |
||||||
|
|
||||||
|
// Check patches
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(flat).toContain("items.0.@id"); |
||||||
|
expect(flat).toContain("items.1.@id"); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("adds @id to objects in Sets", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => |
||||||
|
`gen-${Math.random().toString(36).substr(2, 9)}`, |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ s: new Set<any>() }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
const obj1 = { value: 1 }; |
||||||
|
const obj2 = { value: 2 }; |
||||||
|
state.s.add(obj1); |
||||||
|
state.s.add(obj2); |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
// Get proxied objects from Set
|
||||||
|
const proxiedObjs = Array.from(state.s); |
||||||
|
expect((proxiedObjs[0] as any)["@id"]).toBeDefined(); |
||||||
|
expect((proxiedObjs[1] as any)["@id"]).toBeDefined(); |
||||||
|
|
||||||
|
// @id should be used as synthetic key in paths
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
const obj1Id = (proxiedObjs[0] as any)["@id"]; |
||||||
|
const obj2Id = (proxiedObjs[1] as any)["@id"]; |
||||||
|
expect(flat.some((p) => p.startsWith(`s.${obj1Id}`))).toBe(true); |
||||||
|
expect(flat.some((p) => p.startsWith(`s.${obj2Id}`))).toBe(true); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("@id property behavior", () => { |
||||||
|
it("makes @id readonly", () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ obj: {} as any }, options); |
||||||
|
state.obj.data = { value: 1 }; |
||||||
|
|
||||||
|
// Attempting to modify @id should throw
|
||||||
|
expect(() => { |
||||||
|
(state.obj.data as any)["@id"] = "new-id"; |
||||||
|
}).toThrow("Cannot modify readonly property '@id'"); |
||||||
|
}); |
||||||
|
|
||||||
|
it("makes @id enumerable", () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ obj: {} as any }, options); |
||||||
|
state.obj.data = { value: 1 }; |
||||||
|
|
||||||
|
// @id should show up in Object.keys()
|
||||||
|
const keys = Object.keys(state.obj.data); |
||||||
|
expect(keys).toContain("@id"); |
||||||
|
}); |
||||||
|
|
||||||
|
it("emits patches for @id even on objects with existing @id", async () => { |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ container: {} as any }, options); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
// Object already has @id before being added
|
||||||
|
const objWithId = { "@id": "pre-existing", data: "test" }; |
||||||
|
state.container.item = objWithId; |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
// Patch should still be emitted for @id
|
||||||
|
expect(flat).toContain("container.item.@id"); |
||||||
|
|
||||||
|
// Verify the value in the patch
|
||||||
|
const idPatch = patches |
||||||
|
.flat() |
||||||
|
.find((p) => p.path.join(".") === "container.item.@id"); |
||||||
|
expect((idPatch as any).value).toBe("pre-existing"); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("options inheritance", () => { |
||||||
|
it("child objects inherit options from root", async () => { |
||||||
|
let idCounter = 5000; |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => `inherited-${idCounter++}`, |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ root: {} as any }, options); |
||||||
|
|
||||||
|
// Add nested structure
|
||||||
|
state.root.child = { |
||||||
|
grandchild: { |
||||||
|
value: "nested", |
||||||
|
}, |
||||||
|
}; |
||||||
|
|
||||||
|
// All should have IDs generated by the custom generator
|
||||||
|
expect((state.root.child as any)["@id"]).toMatch(/^inherited-/); |
||||||
|
expect((state.root.child.grandchild as any)["@id"]).toMatch( |
||||||
|
/^inherited-/ |
||||||
|
); |
||||||
|
}); |
||||||
|
|
||||||
|
it("objects added to Sets inherit options", async () => { |
||||||
|
let counter = 9000; |
||||||
|
const options: DeepSignalOptions = { |
||||||
|
idGenerator: () => `set-child-${counter++}`, |
||||||
|
addIdToObjects: true, |
||||||
|
}; |
||||||
|
|
||||||
|
const state = deepSignal({ s: new Set<any>() }, options); |
||||||
|
|
||||||
|
const obj = { nested: { value: 1 } }; |
||||||
|
state.s.add(obj); |
||||||
|
|
||||||
|
// Iterate to get proxied object
|
||||||
|
const proxied = Array.from(state.s)[0]; |
||||||
|
|
||||||
|
// Object and nested object should have custom IDs
|
||||||
|
expect((proxied as any)["@id"]).toMatch(/^set-child-/); |
||||||
|
expect((proxied.nested as any)["@id"]).toMatch(/^set-child-/); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("backward compatibility", () => { |
||||||
|
it("still works without options", async () => { |
||||||
|
const state = deepSignal({ data: { value: 1 } }); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.data.value = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
expect(patches.flat().length).toBeGreaterThan(0); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
// TODO: Delete duplicate logic for `id`. Only accept @id.
|
||||||
|
it("objects with id property still work for Sets", async () => { |
||||||
|
const state = deepSignal({ s: new Set<any>() }); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.s.add({ id: "legacy-id", value: 1 }); |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
// Should use id as synthetic key
|
||||||
|
expect(flat.some((p) => p.startsWith("s.legacy-id"))).toBe(true); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("@id takes precedence over id property", async () => { |
||||||
|
const state = deepSignal({ s: new Set<any>() }); |
||||||
|
const patches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(state, ({ patches: batch }) => |
||||||
|
patches.push(batch) |
||||||
|
); |
||||||
|
|
||||||
|
state.s.add({ |
||||||
|
id: "should-not-use", |
||||||
|
"@id": "should-use", |
||||||
|
value: 1, |
||||||
|
}); |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
// Should use @id, not id
|
||||||
|
expect(flat.some((p) => p.startsWith("s.should-use"))).toBe(true); |
||||||
|
expect(flat.some((p) => p.startsWith("s.should-not-use"))).toBe( |
||||||
|
false |
||||||
|
); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
@ -1,148 +1,148 @@ |
|||||||
import { describe, it, expect } from "vitest"; |
import { describe, it, expect } from "vitest"; |
||||||
import { deepSignal, getDeepSignalRootId, peek } from "../deepSignal"; |
import { deepSignal, getDeepSignalRootId, peek } from "../deepSignal"; |
||||||
import { |
import { |
||||||
watch, |
watch, |
||||||
__traverseCount, |
__traverseCount, |
||||||
__resetTraverseCount, |
__resetTraverseCount, |
||||||
traverse, |
traverse, |
||||||
} from "../watch"; |
} from "../watch"; |
||||||
import { effect } from "../core"; |
import { effect } from "../core"; |
||||||
|
|
||||||
describe("watch advanced", () => { |
describe("watch advanced", () => { |
||||||
it("basic patch watcher fires on deep mutations", async () => { |
it("basic patch watcher fires on deep mutations", async () => { |
||||||
const st = deepSignal({ a: { b: { c: 1 } } }); |
const st = deepSignal({ a: { b: { c: 1 } } }); |
||||||
let batches: number = 0; |
let batches: number = 0; |
||||||
watch(st, ({ patches }) => { |
watch(st, ({ patches }) => { |
||||||
if (patches.length) batches++; |
if (patches.length) batches++; |
||||||
|
}); |
||||||
|
st.a.b.c = 2; |
||||||
|
st.a.b = { c: 3 } as any; |
||||||
|
await Promise.resolve(); |
||||||
|
expect(batches).toBeGreaterThan(0); |
||||||
}); |
}); |
||||||
st.a.b.c = 2; |
|
||||||
st.a.b = { c: 3 } as any; |
|
||||||
await Promise.resolve(); |
|
||||||
expect(batches).toBeGreaterThan(0); |
|
||||||
}); |
|
||||||
|
|
||||||
// multi-source value mode removed; patch-only now - skip equivalent
|
// multi-source value mode removed; patch-only now - skip equivalent
|
||||||
|
|
||||||
// getter source value mode removed in patch-only watcher
|
// getter source value mode removed in patch-only watcher
|
||||||
|
|
||||||
it("watch once option still stops after first batch", async () => { |
it("watch once option still stops after first batch", async () => { |
||||||
const st = deepSignal({ a: 1 }); |
const st = deepSignal({ a: 1 }); |
||||||
let count = 0; |
let count = 0; |
||||||
watch( |
watch( |
||||||
st, |
st, |
||||||
() => { |
() => { |
||||||
count++; |
count++; |
||||||
}, |
}, |
||||||
{ once: true, immediate: true } |
{ once: true, immediate: true } |
||||||
); |
); |
||||||
st.a = 2; |
st.a = 2; |
||||||
st.a = 3; |
st.a = 3; |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
expect(count).toBe(1); |
expect(count).toBe(1); |
||||||
}); |
}); |
||||||
|
|
||||||
// observe value mode removed; observe is alias of watch
|
// observe value mode removed; observe is alias of watch
|
||||||
}); |
}); |
||||||
|
|
||||||
describe("patches & root ids", () => { |
describe("patches & root ids", () => { |
||||||
it("root ids are unique", () => { |
it("root ids are unique", () => { |
||||||
const a = deepSignal({}); |
const a = deepSignal({}); |
||||||
const b = deepSignal({}); |
const b = deepSignal({}); |
||||||
expect(getDeepSignalRootId(a)).not.toBe(getDeepSignalRootId(b)); |
expect(getDeepSignalRootId(a)).not.toBe(getDeepSignalRootId(b)); |
||||||
}); |
}); |
||||||
|
|
||||||
// legacy watchPatches API removed; patch mode only valid for deepSignal roots
|
// legacy watchPatches API removed; patch mode only valid for deepSignal roots
|
||||||
it("watch throws on non-deepSignal input", () => { |
it("watch throws on non-deepSignal input", () => { |
||||||
expect(() => watch({} as any, () => {})).toThrow(); |
expect(() => watch({} as any, () => {})).toThrow(); |
||||||
}); |
}); |
||||||
|
|
||||||
it("Map unsupported does not emit patches", async () => { |
it("Map unsupported does not emit patches", async () => { |
||||||
const m = new Map<string, number>(); |
const m = new Map<string, number>(); |
||||||
const st = deepSignal({ m }); |
const st = deepSignal({ m }); |
||||||
const patches: any[] = []; |
const patches: any[] = []; |
||||||
const { stopListening: stop } = watch(st, ({ patches: batch }) => |
const { stopListening: stop } = watch(st, ({ patches: batch }) => |
||||||
patches.push(batch) |
patches.push(batch) |
||||||
); |
); |
||||||
m.set("a", 1); |
m.set("a", 1); |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
expect(patches.length).toBe(0); |
expect(patches.length).toBe(0); |
||||||
stop(); |
stop(); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
|
|
||||||
describe("tier3: Set iteration variants", () => { |
describe("tier3: Set iteration variants", () => { |
||||||
it("entries() iteration proxies nested mutation", async () => { |
it("entries() iteration proxies nested mutation", async () => { |
||||||
const st = deepSignal({ s: new Set<any>() }); |
const st = deepSignal({ s: new Set<any>() }); |
||||||
st.s.add({ id: "eEnt", inner: { v: 1 } }); |
st.s.add({ id: "eEnt", inner: { v: 1 } }); |
||||||
const paths: string[] = []; |
const paths: string[] = []; |
||||||
const { stopListening: stop } = watch(st, ({ patches }) => |
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
paths.push(...patches.map((pp: any) => pp.path.join("."))) |
paths.push(...patches.map((pp: any) => pp.path.join("."))) |
||||||
); |
); |
||||||
for (const [val] of st.s.entries()) { |
for (const [val] of st.s.entries()) { |
||||||
(val as any).inner.v; |
(val as any).inner.v; |
||||||
} // ensure proxy
|
} // ensure proxy
|
||||||
for (const [val] of st.s.entries()) { |
for (const [val] of st.s.entries()) { |
||||||
(val as any).inner.v = 2; |
(val as any).inner.v = 2; |
||||||
} |
} |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
expect(paths.some((p) => p.endsWith("eEnt.inner.v"))).toBe(true); |
expect(paths.some((p) => p.endsWith("eEnt.inner.v"))).toBe(true); |
||||||
stop(); |
stop(); |
||||||
}); |
}); |
||||||
|
|
||||||
it("forEach iteration proxies nested mutation", async () => { |
it("forEach iteration proxies nested mutation", async () => { |
||||||
const st = deepSignal({ s: new Set<any>() }); |
const st = deepSignal({ s: new Set<any>() }); |
||||||
st.s.add({ id: "fe1", data: { n: 1 } }); |
st.s.add({ id: "fe1", data: { n: 1 } }); |
||||||
const { stopListening: stop } = watch(st, () => {}); |
const { stopListening: stop } = watch(st, () => {}); |
||||||
st.s.forEach((e) => (e as any).data.n); // access
|
st.s.forEach((e) => (e as any).data.n); // access
|
||||||
st.s.forEach((e) => { |
st.s.forEach((e) => { |
||||||
(e as any).data.n = 2; |
(e as any).data.n = 2; |
||||||
|
}); |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
stop(); |
||||||
}); |
}); |
||||||
await Promise.resolve(); |
|
||||||
await Promise.resolve(); |
|
||||||
stop(); |
|
||||||
}); |
|
||||||
|
|
||||||
it("keys() iteration returns proxies", async () => { |
it("keys() iteration returns proxies", async () => { |
||||||
const st = deepSignal({ s: new Set<any>() }); |
const st = deepSignal({ s: new Set<any>() }); |
||||||
st.s.add({ id: "k1", foo: { x: 1 } }); |
st.s.add({ id: "k1", foo: { x: 1 } }); |
||||||
const { stopListening: stop } = watch(st, () => {}); |
const { stopListening: stop } = watch(st, () => {}); |
||||||
for (const e of st.s.keys()) { |
for (const e of st.s.keys()) { |
||||||
(e as any).foo.x = 2; |
(e as any).foo.x = 2; |
||||||
} |
} |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
stop(); |
stop(); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
|
|
||||||
describe("tier3: peek behavior", () => { |
describe("tier3: peek behavior", () => { |
||||||
it("peek does not create reactive dependency on property", async () => { |
it("peek does not create reactive dependency on property", async () => { |
||||||
const st = deepSignal({ a: 1 }); |
const st = deepSignal({ a: 1 }); |
||||||
let runs = 0; |
let runs = 0; |
||||||
effect(() => { |
effect(() => { |
||||||
runs++; |
runs++; |
||||||
peek(st, "a"); |
peek(st, "a"); |
||||||
|
}); |
||||||
|
expect(runs).toBe(1); |
||||||
|
st.a = 2; |
||||||
|
// Flush microtasks
|
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(runs).toBe(1); // no rerun
|
||||||
}); |
}); |
||||||
expect(runs).toBe(1); |
|
||||||
st.a = 2; |
|
||||||
// Flush microtasks
|
|
||||||
await Promise.resolve(); |
|
||||||
await Promise.resolve(); |
|
||||||
expect(runs).toBe(1); // no rerun
|
|
||||||
}); |
|
||||||
}); |
}); |
||||||
|
|
||||||
describe("tier3: traverse helper direct calls (symbols & sets)", () => { |
describe("tier3: traverse helper direct calls (symbols & sets)", () => { |
||||||
it("traverse counts and respects depth param", () => { |
it("traverse counts and respects depth param", () => { |
||||||
__resetTraverseCount(); |
__resetTraverseCount(); |
||||||
const obj: any = { a: { b: { c: 1 } } }; |
const obj: any = { a: { b: { c: 1 } } }; |
||||||
traverse(obj, 1); |
traverse(obj, 1); |
||||||
const shallowCount = __traverseCount; |
const shallowCount = __traverseCount; |
||||||
__resetTraverseCount(); |
__resetTraverseCount(); |
||||||
traverse(obj, 3); |
traverse(obj, 3); |
||||||
const deepCount = __traverseCount; |
const deepCount = __traverseCount; |
||||||
expect(deepCount).toBeGreaterThan(shallowCount); |
expect(deepCount).toBeGreaterThan(shallowCount); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
|
@ -1,357 +1,531 @@ |
|||||||
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); |
// For primitives, the path should be just "s" (the Set itself)
|
||||||
stop(); |
expect(allPaths.every((p) => p === "s")).toBe(true); |
||||||
}); |
// Check the values
|
||||||
|
const patches = batches.flat(); |
||||||
|
const addPatches = patches.filter((p) => p.op === "add"); |
||||||
|
const deletePatches = patches.filter((p) => p.op === "remove"); |
||||||
|
expect(addPatches.length).toBe(1); |
||||||
|
expect(deletePatches.length).toBe(1); |
||||||
|
expect((addPatches[0] as any).value[0]).toBe(3); |
||||||
|
expect((deletePatches[0] as any).value).toBe(1); |
||||||
|
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 patches for primitive adds", async () => { |
||||||
const st = deepSignal({ arr: [1, 2, 3] }); |
const st = deepSignal({ s: new Set<any>() }); |
||||||
const batches: DeepPatch[][] = []; |
const batches: DeepPatch[][] = []; |
||||||
const { stopListening: stop } = watch(st, ({ patches }) => |
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
batches.push(patches) |
batches.push(patches) |
||||||
); |
); |
||||||
st.arr.splice(1, 1, 99, 100); |
st.s.add(true); |
||||||
st.arr.unshift(0); |
st.s.add(2); |
||||||
st.arr.shift(); |
st.s.add("3"); |
||||||
await Promise.resolve(); |
await Promise.resolve(); |
||||||
const paths = batches.flat().map((p) => p.path.join(".")); |
|
||||||
expect(paths.some((p) => p.startsWith("arr."))).toBe(true); |
expect(batches.length).toBe(1); |
||||||
stop(); |
const patches = batches[0]; |
||||||
|
expect(patches.length).toBe(3); |
||||||
|
|
||||||
|
// All patches should have the same path (the Set itself)
|
||||||
|
patches.forEach((p) => { |
||||||
|
expect(p.path.join(".")).toBe("s"); |
||||||
|
expect(p.op).toBe("add"); |
||||||
|
expect((p as any).type).toBe("set"); |
||||||
|
}); |
||||||
|
|
||||||
|
// Check that values are in the value field, not in path
|
||||||
|
const values = patches.map((p: any) => p.value[0]); |
||||||
|
expect(values).toContain(true); |
||||||
|
expect(values).toContain(2); |
||||||
|
expect(values).toContain("3"); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
it("emits patches for primitive deletes", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>([true, 2, "3"]) }); |
||||||
|
const batches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
|
batches.push(patches) |
||||||
|
); |
||||||
|
st.s.delete(true); |
||||||
|
st.s.delete(2); |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
expect(batches.length).toBe(1); |
||||||
|
const patches = batches[0]; |
||||||
|
expect(patches.length).toBe(2); |
||||||
|
|
||||||
|
// All patches should have the same path (the Set itself)
|
||||||
|
patches.forEach((p) => { |
||||||
|
expect(p.path.join(".")).toBe("s"); |
||||||
|
expect(p.op).toBe("remove"); |
||||||
|
expect((p as any).type).toBe("set"); |
||||||
|
}); |
||||||
|
|
||||||
|
// Check that values are in the value field
|
||||||
|
const values = patches.map((p: any) => p.value); |
||||||
|
expect(values).toContain(true); |
||||||
|
expect(values).toContain(2); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
it("does not emit patches for non-existent primitives", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>([1, 2]) }); |
||||||
|
const batches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
|
batches.push(patches) |
||||||
|
); |
||||||
|
st.s.delete("nonexistent"); |
||||||
|
st.s.delete(999); |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
expect(batches.length).toBe(0); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
it("does not emit patches for already added primitive", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>([1, "test", true]) }); |
||||||
|
const batches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
|
batches.push(patches) |
||||||
|
); |
||||||
|
st.s.add(1); |
||||||
|
st.s.add("test"); |
||||||
|
st.s.add(true); |
||||||
|
await Promise.resolve(); |
||||||
|
|
||||||
|
expect(batches.length).toBe(0); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
it("emits single structural patch on Set.clear()", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>() }); |
||||||
|
addWithId(st.s as any, { id: "a", x: 1 }, "a"); |
||||||
|
addWithId(st.s as any, { id: "b", x: 2 }, "b"); |
||||||
|
const batches: DeepPatch[][] = []; |
||||||
|
const { stopListening: stop } = watch(st, ({ patches }) => |
||||||
|
batches.push(patches) |
||||||
|
); |
||||||
|
st.s.clear(); |
||||||
|
await Promise.resolve(); |
||||||
|
// clear() emits a single structural patch for the Set itself (op: "add", value: [])
|
||||||
|
const structuralPatches = batches |
||||||
|
.flat() |
||||||
|
.filter((p) => p.path.length === 1 && p.path[0] === "s"); |
||||||
|
expect(structuralPatches.length).toBe(1); |
||||||
|
expect(structuralPatches[0].op).toBe("add"); |
||||||
|
expect((structuralPatches[0] as any).value).toEqual([]); |
||||||
|
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(); |
||||||
|
// For primitives, path should be just "s" and value should be in the value field
|
||||||
|
const paths = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(paths).toContain("s"); |
||||||
|
const values = patches.flat().map((p: any) => p.value?.[0]); |
||||||
|
expect(values).toContain(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(); |
||||||
|
// Filter for Set structural patches only (path length 2: ['s', syntheticId])
|
||||||
|
const setAddPatches = patches |
||||||
|
.flat() |
||||||
|
.filter( |
||||||
|
(p) => |
||||||
|
p.op === "add" && |
||||||
|
p.path.length === 2 && |
||||||
|
p.path[0] === "s" |
||||||
|
); |
||||||
|
const keys = setAddPatches.map((p) => p.path.slice(-1)[0]); |
||||||
|
// Both objects should have unique synthetic IDs despite id collision
|
||||||
|
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(); |
||||||
|
}); |
||||||
}); |
}); |
||||||
}); |
|
||||||
}); |
}); |
||||||
|
@ -0,0 +1,217 @@ |
|||||||
|
import type { Diff as Patches, Scope } from "../types.ts"; |
||||||
|
import { applyDiff } from "./applyDiff.ts"; |
||||||
|
|
||||||
|
import * as NG from "@ng-org/lib-wasm"; |
||||||
|
|
||||||
|
import { |
||||||
|
deepSignal, |
||||||
|
watch as watchDeepSignal, |
||||||
|
batch, |
||||||
|
} from "@ng-org/alien-deepsignals"; |
||||||
|
import type { |
||||||
|
DeepPatch, |
||||||
|
DeepSignalObject, |
||||||
|
WatchPatchCallback, |
||||||
|
WatchPatchEvent, |
||||||
|
} from "@ng-org/alien-deepsignals"; |
||||||
|
import type { ShapeType, BaseType } from "@ng-org/shex-orm"; |
||||||
|
|
||||||
|
export class OrmConnection<T extends BaseType> { |
||||||
|
// TODO: WeakMaps?
|
||||||
|
private static idToEntry = new Map<string, OrmConnection<any>>(); |
||||||
|
|
||||||
|
private ng: typeof NG; |
||||||
|
readonly shapeType: ShapeType<T>; |
||||||
|
readonly scope: Scope; |
||||||
|
readonly signalObject: DeepSignalObject<T | {}>; |
||||||
|
private refCount: number; |
||||||
|
/*** Identifier as a combination of shape type and scope. Prevents duplications. */ |
||||||
|
private identifier: string; |
||||||
|
ready: boolean; |
||||||
|
sessionId: number; |
||||||
|
suspendDeepWatcher: boolean; |
||||||
|
readyPromise: Promise<void>; |
||||||
|
// Promise that resolves once initial data has been applied.
|
||||||
|
resolveReady!: () => void; |
||||||
|
|
||||||
|
// FinalizationRegistry to clean up connections when signal objects are GC'd.
|
||||||
|
private static cleanupSignalRegistry = |
||||||
|
typeof FinalizationRegistry === "function" |
||||||
|
? new FinalizationRegistry<string>((connectionId) => { |
||||||
|
// Best-effort fallback; look up by id and clean
|
||||||
|
const entry = this.idToEntry.get(connectionId); |
||||||
|
if (!entry) return; |
||||||
|
entry.release(); |
||||||
|
}) |
||||||
|
: null; |
||||||
|
|
||||||
|
private constructor(shapeType: ShapeType<T>, scope: Scope, ng: typeof NG) { |
||||||
|
this.shapeType = shapeType; |
||||||
|
this.scope = scope; |
||||||
|
this.ng = ng; |
||||||
|
this.refCount = 0; |
||||||
|
this.ready = false; |
||||||
|
this.suspendDeepWatcher = false; |
||||||
|
this.identifier = `${shapeType.shape}::${canonicalScope(scope)}`; |
||||||
|
this.signalObject = deepSignal<T | {}>(new Set(), { |
||||||
|
addIdToObjects: true, |
||||||
|
idGenerator: this.generateSubjectIri, |
||||||
|
}); |
||||||
|
|
||||||
|
// TODO:
|
||||||
|
this.sessionId = 1; |
||||||
|
|
||||||
|
// Schedule cleanup of the connection when the signal object is GC'd.
|
||||||
|
OrmConnection.cleanupSignalRegistry?.register( |
||||||
|
this.signalObject, |
||||||
|
this.identifier, |
||||||
|
this.signalObject |
||||||
|
); |
||||||
|
|
||||||
|
// Add listener to deep signal object to report changes back to wasm land.
|
||||||
|
watchDeepSignal(this.signalObject as T, this.onSignalObjectUpdate); |
||||||
|
|
||||||
|
// Initialize per-entry readiness promise that resolves in setUpConnection
|
||||||
|
this.readyPromise = new Promise<void>((resolve) => { |
||||||
|
this.resolveReady = resolve; |
||||||
|
}); |
||||||
|
|
||||||
|
// Establish connection to wasm land.
|
||||||
|
ng.orm_start(scope, shapeType, this.sessionId, this.onBackendMessage); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get a connection which contains the ORM and lifecycle methods. |
||||||
|
* @param shapeType |
||||||
|
* @param scope |
||||||
|
* @param ng |
||||||
|
* @returns |
||||||
|
*/ |
||||||
|
public static getConnection<T extends BaseType>( |
||||||
|
shapeType: ShapeType<T>, |
||||||
|
scope: Scope, |
||||||
|
ng: typeof NG |
||||||
|
): OrmConnection<T> { |
||||||
|
const scopeKey = canonicalScope(scope); |
||||||
|
|
||||||
|
// Unique identifier for a given shape type and scope.
|
||||||
|
const identifier = `${shapeType.shape}::${scopeKey}`; |
||||||
|
|
||||||
|
// If we already have an object for this shape+scope,
|
||||||
|
// return it and just increase the reference count.
|
||||||
|
// Otherwise, create new one.
|
||||||
|
const connection = |
||||||
|
OrmConnection.idToEntry.get(identifier) ?? |
||||||
|
new OrmConnection(shapeType, scope, ng); |
||||||
|
|
||||||
|
connection.refCount += 1; |
||||||
|
|
||||||
|
return connection; |
||||||
|
} |
||||||
|
|
||||||
|
public release() { |
||||||
|
if (this.refCount > 0) this.refCount--; |
||||||
|
if (this.refCount === 0) { |
||||||
|
OrmConnection.idToEntry.delete(this.identifier); |
||||||
|
|
||||||
|
OrmConnection.cleanupSignalRegistry?.unregister(this.signalObject); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private onSignalObjectUpdate({ patches }: WatchPatchEvent<T>) { |
||||||
|
if (this.suspendDeepWatcher || !this.ready || !patches.length) return; |
||||||
|
|
||||||
|
const ormPatches = deepPatchesToDiff(patches); |
||||||
|
|
||||||
|
this.ng.orm_update( |
||||||
|
this.scope, |
||||||
|
this.shapeType.shape, |
||||||
|
ormPatches, |
||||||
|
this.sessionId |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
private onBackendMessage(...message: any) { |
||||||
|
this.handleInitialResponse(message); |
||||||
|
} |
||||||
|
|
||||||
|
private handleInitialResponse(...param: any) { |
||||||
|
console.log("RESPONSE FROM BACKEND", param); |
||||||
|
|
||||||
|
// TODO: This will break, just provisionary.
|
||||||
|
const wasmMessage: WasmMessage = param; |
||||||
|
const { initialData } = wasmMessage; |
||||||
|
|
||||||
|
// Assign initial data to empty signal object without triggering watcher at first.
|
||||||
|
this.suspendDeepWatcher = true; |
||||||
|
batch(() => { |
||||||
|
// Convert arrays to sets and apply to signalObject (we only have sets but can only transport arrays).
|
||||||
|
Object.assign(this.signalObject, recurseArrayToSet(initialData)!); |
||||||
|
}); |
||||||
|
|
||||||
|
queueMicrotask(() => { |
||||||
|
this.suspendDeepWatcher = false; |
||||||
|
// Resolve readiness after initial data is committed and watcher armed.
|
||||||
|
this.resolveReady?.(); |
||||||
|
}); |
||||||
|
|
||||||
|
this.ready = true; |
||||||
|
} |
||||||
|
private onBackendUpdate(...params: any) { |
||||||
|
// Apply diff
|
||||||
|
} |
||||||
|
|
||||||
|
/** Function to create random subject IRIs for newly created nested objects. */ |
||||||
|
private generateSubjectIri(path: (string | number)[]): string { |
||||||
|
// Generate random string.
|
||||||
|
let b = Buffer.alloc(33); |
||||||
|
crypto.getRandomValues(b); |
||||||
|
const randomString = b.toString("base64url"); |
||||||
|
|
||||||
|
if (path.length > 0 && path[0].toString().startsWith("did:ng:o:")) { |
||||||
|
// If the root is a nuri, use that as a base IRI.
|
||||||
|
let rootNuri = path[0] as string; |
||||||
|
|
||||||
|
return rootNuri.substring(0, 9 + 44) + ":q:" + randomString; |
||||||
|
} else { |
||||||
|
// Else, just generate a random IRI.
|
||||||
|
return "did:ng:q:" + randomString; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
function escapePathSegment(segment: string): string { |
||||||
|
return segment.replace("~", "~0").replace("/", "~1"); |
||||||
|
} |
||||||
|
|
||||||
|
export function deepPatchesToDiff(patches: DeepPatch[]): Patches { |
||||||
|
return patches.map((patch) => { |
||||||
|
const path = |
||||||
|
"/" + |
||||||
|
patch.path.map((el) => escapePathSegment(el.toString())).join("/"); |
||||||
|
return { ...patch, path }; |
||||||
|
}) as Patches; |
||||||
|
} |
||||||
|
|
||||||
|
const recurseArrayToSet = (obj: any): any => { |
||||||
|
if (Array.isArray(obj)) { |
||||||
|
return new Set(obj.map(recurseArrayToSet)); |
||||||
|
} else if (obj && typeof obj === "object") { |
||||||
|
for (const key of Object.keys(obj)) { |
||||||
|
obj[key] = recurseArrayToSet(obj[key]); |
||||||
|
} |
||||||
|
return obj; |
||||||
|
} else { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
function canonicalScope(scope: Scope | undefined): string { |
||||||
|
if (scope == null) return ""; |
||||||
|
return Array.isArray(scope) |
||||||
|
? scope.slice().sort().join(",") |
||||||
|
: String(scope); |
||||||
|
} |
Loading…
Reference in new issue