Compare commits
No commits in common. 'e979f0233afcfcab60e54392104e62f0160c1129' and 'd78b7dabd514a629d14a46191ee5337c5644f240' have entirely different histories.
e979f0233a
...
d78b7dabd5
@ -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,217 +0,0 @@ |
|||||||
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