parent
b691dc9202
commit
58e5e96fa3
@ -0,0 +1,81 @@ |
|||||||
|
import { describe, it, expect } from "vitest"; |
||||||
|
import { signal, computed, isSignal, Effect, toValue } from "../core"; |
||||||
|
import { deepSignal } from "../deepSignal"; |
||||||
|
|
||||||
|
describe("core.ts coverage", () => { |
||||||
|
it("signal tagging helpers (.value/.peek/.get/.set)", () => { |
||||||
|
const s: any = signal(1); |
||||||
|
expect(isSignal(s)).toBe(true); |
||||||
|
expect(s.value).toBe(1); |
||||||
|
expect(s.peek()).toBe(1); |
||||||
|
expect(s.get()).toBe(1); |
||||||
|
s.set(2); |
||||||
|
expect(s.value).toBe(2); |
||||||
|
s.value = 3; |
||||||
|
expect(s.peek()).toBe(3); |
||||||
|
}); |
||||||
|
|
||||||
|
it("computed tagging helpers (.value/.peek/.get)", () => { |
||||||
|
const s: any = signal(2); |
||||||
|
const c: any = computed(() => s.value * 2); |
||||||
|
expect(isSignal(c)).toBe(true); |
||||||
|
expect(c.value).toBe(4); |
||||||
|
expect(c.peek()).toBe(4); |
||||||
|
expect(c.get()).toBe(4); |
||||||
|
s.value = 3; |
||||||
|
expect(c.value).toBe(6); |
||||||
|
}); |
||||||
|
|
||||||
|
it("toValue resolves function, signal and plain value", () => { |
||||||
|
const s: any = signal(5); |
||||||
|
const fn = () => 10; |
||||||
|
expect(toValue(fn)).toBe(10); |
||||||
|
expect(toValue(s)).toBe(5); |
||||||
|
expect(toValue(42)).toBe(42); |
||||||
|
}); |
||||||
|
|
||||||
|
it("Effect wrapper run/stop behavior", () => { |
||||||
|
let runs = 0; |
||||||
|
const eff = new Effect(() => { |
||||||
|
runs++; |
||||||
|
}); |
||||||
|
// Constructing Effect registers alienEffect and schedules first run immediately when dependency accessed (none here), run() executes getter
|
||||||
|
eff.run(); |
||||||
|
// Construction may trigger an initial scheduler pass; ensure at least 1
|
||||||
|
expect(runs).toBeGreaterThanOrEqual(1); |
||||||
|
// Add scheduler side effect and dependency in second effect
|
||||||
|
const dep = signal(0); |
||||||
|
const eff2 = new Effect(() => { |
||||||
|
dep(); |
||||||
|
runs++; |
||||||
|
}); |
||||||
|
const base = runs; |
||||||
|
dep.set(1); // triggers wrapped effect, increments runs again
|
||||||
|
expect(runs).toBeGreaterThan(base); |
||||||
|
eff2.stop(); |
||||||
|
const prev = runs; |
||||||
|
dep.set(2); // no further increment after stop
|
||||||
|
expect(runs).toBe(prev); |
||||||
|
// stopping already stopped effect has no effect
|
||||||
|
eff2.stop(); |
||||||
|
expect(runs).toBe(prev); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("deepSignal.ts extra branches", () => { |
||||||
|
it("access well-known symbol property returns raw value and not a signal", () => { |
||||||
|
const tag = Symbol.toStringTag; |
||||||
|
const ds = deepSignal({ [tag]: "Custom", x: 1 }) as any; |
||||||
|
const val = ds[tag]; |
||||||
|
expect(val).toBe("Custom"); |
||||||
|
}); |
||||||
|
|
||||||
|
it("access Set Symbol.iterator.toString() key path (skip branch)", () => { |
||||||
|
const ds = deepSignal({ set: new Set([1]) }) as any; |
||||||
|
const iterKey = Symbol.iterator.toString(); // 'Symbol(Symbol.iterator)'
|
||||||
|
// Accessing this string property triggers skip branch (no special handling needed)
|
||||||
|
const maybe = ds.set[iterKey]; |
||||||
|
// underlying Set likely has undefined for that string key
|
||||||
|
expect(maybe).toBeUndefined(); |
||||||
|
}); |
||||||
|
}); |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,76 @@ |
|||||||
|
import { describe, it, expect, beforeEach } from "vitest"; |
||||||
|
import { deepSignal } from "../deepSignal"; |
||||||
|
import { watch, __traverseCount, __resetTraverseCount } from "../watch"; |
||||||
|
|
||||||
|
// Goal: demonstrate that patchOptimized deep watch performs fewer traversals
|
||||||
|
// than standard deep watch for the same batch of nested mutations.
|
||||||
|
// We use the exported __traverseCount instrumentation to measure how many
|
||||||
|
// times traverse() executes under each strategy.
|
||||||
|
|
||||||
|
describe("watch patchOptimized performance", () => { |
||||||
|
let store: any; |
||||||
|
const build = (breadth = 3, depth = 3) => { |
||||||
|
const make = (d: number): any => { |
||||||
|
if (d === 0) return { v: 0 }; |
||||||
|
const obj: any = {}; |
||||||
|
for (let i = 0; i < breadth; i++) obj["k" + i] = make(d - 1); |
||||||
|
return obj; |
||||||
|
}; |
||||||
|
return make(depth); |
||||||
|
}; |
||||||
|
|
||||||
|
beforeEach(() => { |
||||||
|
store = deepSignal(build()); |
||||||
|
}); |
||||||
|
|
||||||
|
function mutateAll(breadth = 3, depth = 3) { |
||||||
|
const visit = (node: any, d: number) => { |
||||||
|
if (d === 0) { |
||||||
|
node.v++; |
||||||
|
return; |
||||||
|
} |
||||||
|
for (let i = 0; i < breadth; i++) visit(node["k" + i], d - 1); |
||||||
|
}; |
||||||
|
visit(store, depth); |
||||||
|
} |
||||||
|
|
||||||
|
it("reduces traverse calls for deep watchers", async () => { |
||||||
|
// Non-optimized deep watch
|
||||||
|
__resetTraverseCount(); |
||||||
|
const stop1 = watch( |
||||||
|
store, |
||||||
|
() => { |
||||||
|
/* no-op */ |
||||||
|
}, |
||||||
|
{ deep: true, patchOptimized: false } |
||||||
|
); |
||||||
|
mutateAll(); |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
const traversalsNormal = __traverseCount; |
||||||
|
stop1(); |
||||||
|
|
||||||
|
// Optimized deep watch
|
||||||
|
__resetTraverseCount(); |
||||||
|
const stop2 = watch( |
||||||
|
store, |
||||||
|
() => { |
||||||
|
/* no-op */ |
||||||
|
}, |
||||||
|
{ deep: true, patchOptimized: true } |
||||||
|
); |
||||||
|
mutateAll(); |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
const traversalsOptimized = __traverseCount; |
||||||
|
stop2(); |
||||||
|
|
||||||
|
console.log( |
||||||
|
`Traversals normal: ${traversalsNormal}, optimized: ${traversalsOptimized}` |
||||||
|
); |
||||||
|
// Optimized path should not perform more traversals than baseline and ideally fewer.
|
||||||
|
expect(traversalsOptimized <= traversalsNormal).toBe(true); |
||||||
|
// Ensure baseline actually did at least one traversal more than optimized (sanity check)
|
||||||
|
expect(traversalsNormal > traversalsOptimized).toBe(true); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,199 @@ |
|||||||
|
import { describe, it, expect } from "vitest"; |
||||||
|
import { deepSignal, getDeepSignalRootId, peek } from "../deepSignal"; |
||||||
|
import { |
||||||
|
watch, |
||||||
|
watchPatches, |
||||||
|
observe, |
||||||
|
__traverseCount, |
||||||
|
__resetTraverseCount, |
||||||
|
traverse, |
||||||
|
} from "../watch"; |
||||||
|
import { effect, signal } from "../core"; |
||||||
|
|
||||||
|
describe("watch advanced", () => { |
||||||
|
it("numeric deep depth limits traversal reactions", async () => { |
||||||
|
const st = deepSignal({ a: { b: { c: 1 } } }); |
||||||
|
let runs = 0; |
||||||
|
watch( |
||||||
|
st, |
||||||
|
() => { |
||||||
|
runs++; |
||||||
|
}, |
||||||
|
{ deep: 1 } |
||||||
|
); // depth 1: should collect a & a.b but not a.b.c
|
||||||
|
// initial run (immediate not set so first collection sets oldValue only)
|
||||||
|
st.a.b.c = 2; // depth 2 change (may trigger depending on traversal semantics)
|
||||||
|
st.a.b = { c: 3 }; // depth 1 mutation
|
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(runs).toBeGreaterThan(0); |
||||||
|
}); |
||||||
|
|
||||||
|
it("multi-source watch array triggers when one source changes", async () => { |
||||||
|
const a = signal(1); |
||||||
|
const b = deepSignal({ x: 1 }); |
||||||
|
let vals: any[] = []; |
||||||
|
watch( |
||||||
|
[a, b], |
||||||
|
(nv) => { |
||||||
|
vals = nv; |
||||||
|
}, |
||||||
|
{ deep: true } |
||||||
|
); |
||||||
|
b.x = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(vals[1].x).toBe(2); |
||||||
|
}); |
||||||
|
|
||||||
|
it("watch getter source (function) with callback", async () => { |
||||||
|
const st = deepSignal({ n: 1 }); |
||||||
|
let seen = 0; |
||||||
|
watch( |
||||||
|
() => st.n * 2, |
||||||
|
(val) => { |
||||||
|
seen = val; |
||||||
|
}, |
||||||
|
{ immediate: true } |
||||||
|
); |
||||||
|
expect(seen).toBe(2); |
||||||
|
st.n = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(seen).toBe(4); |
||||||
|
}); |
||||||
|
|
||||||
|
it("watch once with patchOptimized deep on deepSignal", async () => { |
||||||
|
const st = deepSignal({ a: 1 }); |
||||||
|
let count = 0; |
||||||
|
watch( |
||||||
|
st, |
||||||
|
() => { |
||||||
|
count++; |
||||||
|
}, |
||||||
|
{ deep: true, once: true, patchOptimized: true } |
||||||
|
); |
||||||
|
st.a = 2; |
||||||
|
st.a = 3; |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(count).toBe(1); |
||||||
|
}); |
||||||
|
|
||||||
|
it("observe value mode returns values and not patches", async () => { |
||||||
|
const st = deepSignal({ a: 1 }); |
||||||
|
let latest: any; |
||||||
|
const stop = observe( |
||||||
|
st, |
||||||
|
(v: any) => { |
||||||
|
latest = v; |
||||||
|
}, |
||||||
|
{ deep: true } |
||||||
|
); |
||||||
|
st.a = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(latest.a).toBe(2); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("patches & root ids", () => { |
||||||
|
it("root ids are unique", () => { |
||||||
|
const a = deepSignal({}); |
||||||
|
const b = deepSignal({}); |
||||||
|
expect(getDeepSignalRootId(a)).not.toBe(getDeepSignalRootId(b)); |
||||||
|
}); |
||||||
|
|
||||||
|
it("watchPatches throws on non-deepSignal input", () => { |
||||||
|
expect(() => watchPatches({}, () => {})).toThrow(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("Map unsupported does not emit patches", async () => { |
||||||
|
const m = new Map<string, number>(); |
||||||
|
const st = deepSignal({ m }); |
||||||
|
const patches: any[] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
m.set("a", 1); |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(patches.length).toBe(0); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("tier3: Set iteration variants", () => { |
||||||
|
it("entries() iteration proxies nested mutation", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>() }); |
||||||
|
st.s.add({ id: "eEnt", inner: { v: 1 } }); |
||||||
|
const paths: string[] = []; |
||||||
|
const stop = watchPatches(st, (p) => |
||||||
|
paths.push(...p.map((pp) => pp.path.join("."))) |
||||||
|
); |
||||||
|
for (const [val] of st.s.entries()) { |
||||||
|
(val as any).inner.v; |
||||||
|
} // ensure proxy
|
||||||
|
for (const [val] of st.s.entries()) { |
||||||
|
(val as any).inner.v = 2; |
||||||
|
} |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(paths.some((p) => p.endsWith("eEnt.inner.v"))).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("forEach iteration proxies nested mutation", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>() }); |
||||||
|
st.s.add({ id: "fe1", data: { n: 1 } }); |
||||||
|
const stop = watchPatches(st, () => {}); |
||||||
|
st.s.forEach((e) => (e as any).data.n); // access
|
||||||
|
st.s.forEach((e) => { |
||||||
|
(e as any).data.n = 2; |
||||||
|
}); |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("keys() iteration returns proxies", async () => { |
||||||
|
const st = deepSignal({ s: new Set<any>() }); |
||||||
|
st.s.add({ id: "k1", foo: { x: 1 } }); |
||||||
|
const stop = watchPatches(st, () => {}); |
||||||
|
for (const e of st.s.keys()) { |
||||||
|
(e as any).foo.x = 2; |
||||||
|
} |
||||||
|
await Promise.resolve(); |
||||||
|
await Promise.resolve(); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("tier3: peek behavior", () => { |
||||||
|
it("peek does not create reactive dependency on property", async () => { |
||||||
|
const st = deepSignal({ a: 1 }); |
||||||
|
let runs = 0; |
||||||
|
effect(() => { |
||||||
|
runs++; |
||||||
|
peek(st, "a"); |
||||||
|
}); |
||||||
|
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)", () => { |
||||||
|
it("traverse counts and respects depth param", () => { |
||||||
|
__resetTraverseCount(); |
||||||
|
const obj: any = { a: { b: { c: 1 } } }; |
||||||
|
traverse(obj, 1); |
||||||
|
const shallowCount = __traverseCount; |
||||||
|
__resetTraverseCount(); |
||||||
|
traverse(obj, 3); |
||||||
|
const deepCount = __traverseCount; |
||||||
|
expect(deepCount).toBeGreaterThan(shallowCount); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,339 @@ |
|||||||
|
import { describe, it, expect } from "vitest"; |
||||||
|
import { deepSignal, setSetEntrySyntheticId, addWithId } from "../deepSignal"; |
||||||
|
import { watchPatches, observe, Patch } from "../watch"; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for watchPatches / observe(..., {mode:'patch'}) ensuring: |
||||||
|
* 1. Only patches from the provided root are emitted. |
||||||
|
* 2. Batching groups multiple sync mutations in one array. |
||||||
|
* 3. Delete operations are reported without value. |
||||||
|
* 4. observe patch mode mirrors watchPatches output. |
||||||
|
*/ |
||||||
|
|
||||||
|
// NOTE about Set entry tests:
|
||||||
|
// deepSignal does NOT rewrite external references passed into Set.add().
|
||||||
|
// After an object is added, mutate ONLY the proxied version (obtained via iteration, values(), entries(), forEach, or addWithId) to get deep patches.
|
||||||
|
// Mutating the original variable captured before adding will NOT emit patches.
|
||||||
|
describe("watchPatches", () => { |
||||||
|
it("emits set patches with correct paths and batching", async () => { |
||||||
|
const state = deepSignal({ a: { b: 1 }, arr: [1, { x: 2 }] }); |
||||||
|
const received: any[] = []; |
||||||
|
const stop = watchPatches(state, (patches) => { |
||||||
|
received.push(patches); |
||||||
|
}); |
||||||
|
|
||||||
|
// multiple synchronous mutations => single microtask batch expected
|
||||||
|
state.a.b = 2; |
||||||
|
(state.arr[1] as any).x = 3; |
||||||
|
state.arr.push(5); |
||||||
|
|
||||||
|
await Promise.resolve(); // flush microtask
|
||||||
|
expect(received.length).toBe(1); |
||||||
|
const batch = received[0]; |
||||||
|
// Paths should reflect root-relative keys
|
||||||
|
const paths = (batch as Patch[]).map((p: Patch) => p.path.join(".")).sort(); |
||||||
|
expect(paths).toContain("a.b"); |
||||||
|
expect(paths).toContain("arr.1.x"); |
||||||
|
expect(paths).toContain("arr.2"); // new index push
|
||||||
|
// Types
|
||||||
|
const setTypes = (batch as Patch[]).filter( |
||||||
|
(p: Patch) => p.type === "set" |
||||||
|
).length; |
||||||
|
expect(setTypes).toBe(batch.length); |
||||||
|
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("emits delete patches without value", async () => { |
||||||
|
const state = deepSignal<{ a: { b?: number }; c?: number }>({ |
||||||
|
a: { b: 1 }, |
||||||
|
c: 2, |
||||||
|
}); |
||||||
|
const out: any[] = []; |
||||||
|
const stop = watchPatches(state, (p) => out.push(p)); |
||||||
|
|
||||||
|
delete state.a.b; |
||||||
|
delete state.c; |
||||||
|
|
||||||
|
await Promise.resolve(); |
||||||
|
expect(out.length).toBe(1); |
||||||
|
const [batch] = out; |
||||||
|
const deletePatches = (batch as Patch[]).filter( |
||||||
|
(p: Patch) => p.type === "delete" |
||||||
|
); |
||||||
|
const delPaths = deletePatches.map((p: Patch) => p.path.join(".")).sort(); |
||||||
|
expect(delPaths).toEqual(["a.b", "c"]); |
||||||
|
deletePatches.forEach((p: Patch) => expect(p.value).toBeUndefined()); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("observe patch mode mirrors watchPatches", async () => { |
||||||
|
const state = deepSignal({ a: 1 }); |
||||||
|
const wp: any[] = []; |
||||||
|
const ob: any[] = []; |
||||||
|
const stop1 = watchPatches(state, (p) => wp.push(p)); |
||||||
|
const stop2 = observe(state, (p: Patch[]) => ob.push(p), { mode: "patch" }); |
||||||
|
|
||||||
|
state.a = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
expect(wp.length).toBe(1); |
||||||
|
expect(ob.length).toBe(1); |
||||||
|
expect(wp[0].length).toBe(1); |
||||||
|
expect(ob[0][0].path.join(".")).toBe("a"); |
||||||
|
stop1(); |
||||||
|
stop2(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("filters out patches from other roots", async () => { |
||||||
|
const a = deepSignal({ x: 1 }); |
||||||
|
const b = deepSignal({ y: 2 }); |
||||||
|
const out: any[] = []; |
||||||
|
const stop = watchPatches(a, (p) => out.push(p)); |
||||||
|
b.y = 3; // unrelated root
|
||||||
|
a.x = 2; // related root
|
||||||
|
await Promise.resolve(); |
||||||
|
expect(out.length).toBe(1); |
||||||
|
expect(out[0][0].path.join(".")).toBe("x"); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("emits patches for Set structural mutations (add/delete)", async () => { |
||||||
|
const state = deepSignal<{ s: Set<number> }>({ s: new Set([1, 2]) }); |
||||||
|
const batches: Patch[][] = []; |
||||||
|
const stop = watchPatches(state, (p) => batches.push(p)); |
||||||
|
state.s.add(3); |
||||||
|
state.s.delete(1); |
||||||
|
await Promise.resolve(); |
||||||
|
expect(batches.length >= 1).toBe(true); |
||||||
|
const allPaths = batches.flatMap((b) => b.map((p) => p.path.join("."))); |
||||||
|
// Expect per-entry paths like s.3 (primitive) or s.<synthetic>
|
||||||
|
expect(allPaths.some((p) => p.startsWith("s."))).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("emits patches for nested objects added after initialization", async () => { |
||||||
|
const state = deepSignal<{ root: any }>({ root: {} }); |
||||||
|
const patches: Patch[][] = []; |
||||||
|
const stop = watchPatches(state, (p) => patches.push(p)); |
||||||
|
state.root.child = { level: { value: 1 } }; |
||||||
|
state.root.child.level.value = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
const flat = patches.flat(); |
||||||
|
const paths = flat.map((p) => p.path.join(".")); |
||||||
|
expect(paths).toContain("root.child"); // initial add
|
||||||
|
expect(paths).toContain("root.child.level.value"); // deep mutation
|
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("emits structural patches for sets of sets (no deep inner object mutation tracking)", async () => { |
||||||
|
const innerA = new Set<any>([{ id: "node1", x: 1 }]); |
||||||
|
const s = new Set<any>([innerA]); |
||||||
|
const state = deepSignal<{ graph: Set<any> }>({ graph: s }); |
||||||
|
const batches: Patch[][] = []; |
||||||
|
const stop = watchPatches(state, (p) => batches.push(p)); |
||||||
|
// Add a new inner set
|
||||||
|
const innerB = new Set<any>([{ id: "node2", x: 5 }]); |
||||||
|
state.graph.add(innerB); |
||||||
|
// Mutate object inside innerA
|
||||||
|
([...innerA][0] as any).x = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
const flat = batches.flat(); |
||||||
|
const pathStrings = flat.map((p) => p.path.join(".")); |
||||||
|
// Expect a patch for adding innerB (graph.<syntheticId>)
|
||||||
|
expect(pathStrings.some((p) => p.startsWith("graph."))).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("tracks deep nested object mutation inside a Set entry after iteration", async () => { |
||||||
|
const rawEntry = { id: "n1", data: { val: 1 } }; |
||||||
|
const st = deepSignal({ bag: new Set<any>([rawEntry]) }); |
||||||
|
const patches: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
// Obtain proxied entry via iteration
|
||||||
|
let proxied: any; |
||||||
|
for (const e of st.bag.values()) { |
||||||
|
proxied = e; // this is the proxied version
|
||||||
|
e.data.val; // access to ensure deep proxying of nested object
|
||||||
|
} |
||||||
|
// Mutate proxied (NOT rawEntry)
|
||||||
|
proxied.data.val = 2; |
||||||
|
await Promise.resolve(); |
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(flat.some((p) => p.endsWith("n1.data.val"))).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("allows custom synthetic id for Set entry", async () => { |
||||||
|
const node = { name: "x" }; |
||||||
|
const state = deepSignal({ s: new Set<any>() }); |
||||||
|
const patches: Patch[][] = []; |
||||||
|
const stop = watchPatches(state, (p) => patches.push(p)); |
||||||
|
// Preferred ergonomic helper
|
||||||
|
addWithId(state.s as any, node, "custom123"); |
||||||
|
await Promise.resolve(); |
||||||
|
const flat = patches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(flat.some((p) => p === "s.custom123")).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("Set", () => { |
||||||
|
it("emits one 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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => batches.push(p)); |
||||||
|
st.s.clear(); |
||||||
|
await Promise.resolve(); |
||||||
|
const all = batches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(all).toContain("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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
st.s.add(obj); |
||||||
|
st.s.delete(obj); |
||||||
|
await Promise.resolve(); |
||||||
|
const all = patches |
||||||
|
.flat() |
||||||
|
.filter((p) => p.type === "delete") |
||||||
|
.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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
st.s.add(1); // duplicate
|
||||||
|
await Promise.resolve(); |
||||||
|
// no new patches (size unchanged)
|
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
const ret = addWithId(st.s as any, 5, "ignored"); // primitives ignore id
|
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => batches.push(p)); |
||||||
|
// values()
|
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => batches.push(p)); |
||||||
|
raw.data.x = 2; // mutate raw (no patch expected for deep)
|
||||||
|
await Promise.resolve(); |
||||||
|
const afterRaw = batches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(afterRaw.some((p) => p.endsWith("id1.data.x"))).toBe(false); |
||||||
|
// Now mutate via proxied
|
||||||
|
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: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => patches.push(p)); |
||||||
|
st.s.add(a1); |
||||||
|
st.s.add(a2); |
||||||
|
await Promise.resolve(); |
||||||
|
const keys = patches |
||||||
|
.flat() |
||||||
|
.filter((p) => p.type === "set") |
||||||
|
.map((p) => p.path.slice(-1)[0]); |
||||||
|
// Expect two distinct keys
|
||||||
|
expect(new Set(keys).size).toBe(2); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
describe("Arrays & mixed batch", () => { |
||||||
|
it("emits patches for splice/unshift/shift in single batch", async () => { |
||||||
|
const st = deepSignal({ arr: [1, 2, 3] }); |
||||||
|
const batches: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => batches.push(p)); |
||||||
|
st.arr.splice(1, 1, 99, 100); // delete index1, add two
|
||||||
|
st.arr.unshift(0); |
||||||
|
st.arr.shift(); // undo
|
||||||
|
await Promise.resolve(); |
||||||
|
const paths = batches.flat().map((p) => p.path.join(".")); |
||||||
|
expect(paths.some((p) => p.startsWith("arr."))).toBe(true); |
||||||
|
stop(); |
||||||
|
}); |
||||||
|
|
||||||
|
it("mixed object/array/Set mutations batch together", async () => { |
||||||
|
const st = deepSignal({ o: { a: 1 }, arr: [1], s: new Set<any>() }); |
||||||
|
const batches: Patch[][] = []; |
||||||
|
const stop = watchPatches(st, (p) => batches.push(p)); |
||||||
|
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(); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
@ -1,5 +1,12 @@ |
|||||||
import { watch } from "./watch" |
import { watch } from "./watch"; |
||||||
|
|
||||||
export function watchEffect(effect: () => void) { |
/** |
||||||
return watch(effect, undefined) |
* Run a reactive effect function immediately and again whenever its accessed dependencies change. |
||||||
|
* Provides an optional `onCleanup` registration parameter for teardown logic between re-runs. |
||||||
|
* Equivalent to `watch(effectFn)` with no explicit callback. |
||||||
|
*/ |
||||||
|
export function watchEffect( |
||||||
|
effect: (onCleanup?: (fn: () => void) => void) => void |
||||||
|
) { |
||||||
|
return watch(effect as any, undefined); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue