export function isObject(v) {
return typeof v === "object" && v !== null && !Array.isArray(v);
}
export function toJson(v) {
if (v === undefined)
return null;
return JSON.parse(JSON.stringify(v));
}
export function deepEqual(a, b) {
if (a === b)
return true;
if (typeof a !== typeof b || a === null || b === null || typeof a !== "object")
return false;
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length)
return false;
for (let i = 0; i < a.length; i++)
if (!deepEqual(a[i], b[i]))
return false;
return true;
}
if (Array.isArray(b))
return false;
const ak = Object.keys(a);
const bo = b;
if (ak.length !== Object.keys(bo).length)
return false;
for (const k of ak) {
if (!Object.hasOwn(bo, k) || !deepEqual(a[k], bo[k]))
return false;
}
return true;
}
export function diff(a, b) {
const set = {};
const del = [];
walk(a, b, [], set, del);
const out = {};
if (Object.keys(set).length > 0)
out.set = set;
if (del.length > 0)
out.del = del;
return out.set || out.del ? out : undefined;
}
export function diffAt(a, b, path) {
const set = {};
const del = [];
walk(a, b, path, set, del);
return { set, del };
}
function walk(a, b, path, set, del) {
for (const k of Object.keys(a)) {
if (!Object.hasOwn(b, k))
del.push([...path, k]);
}
for (const k of Object.keys(b)) {
const nv = b[k];
if (!Object.hasOwn(a, k)) {
set[k] = nv;
continue;
}
const ov = a[k];
if (isObject(ov) && isObject(nv)) {
const sub = {};
walk(ov, nv, [...path, k], sub, del);
if (Object.keys(sub).length > 0)
set[k] = sub;
}
else if (!deepEqual(ov, nv)) {
set[k] = nv;
}
}
}
export function applyDiff(view, d) {
const out = toJson(view);
if (d.set)
merge(out, d.set);
for (const path of d.del ?? []) {
let o = out;
for (let i = 0; i < path.length - 1 && isObject(o); i++)
o = o[path[i]];
if (isObject(o))
delete o[path[path.length - 1]];
}
return out;
}
function merge(target, patch) {
for (const k of Object.keys(patch)) {
const pv = patch[k];
const tv = target[k];
if (isObject(pv) && isObject(tv))
merge(tv, pv);
else
target[k] = toJson(pv);
}
}
export class StateBox {
#value = {};
#version = 0;
#snapshot;
#changed;
constructor(changed) {
this.#changed = changed;
}
get value() {
return toJson(this.#value);
}
peek() {
return this.#value;
}
get version() {
return this.#version;
}
snapshot() {
if (this.#snapshot?.version !== this.#version)
this.#snapshot = { version: this.#version, value: toJson(this.#value) };
return this.#snapshot.value;
}
set(partial) {
const j = toJson(partial);
if (!isObject(j))
throw new TypeError("state must be an object");
Object.assign(this.#value, j);
for (const [k, v] of Object.entries(partial))
if (v === undefined)
delete this.#value[k];
this.#version++;
this.#changed();
}
replace(value) {
const j = toJson(value);
if (!isObject(j))
throw new TypeError("state must be an object");
this.#value = j;
this.#version++;
this.#changed();
}
update(fn) {
const draft = toJson(this.#value);
fn(draft);
this.replace(draft);
}
}