UNPKG

lib0

Version:

isomorphic utility functions

96 lines • 4.06 kB
/** * Schema describing the deltas produced/consumed by a {@link DomRDT}: a recursive node with a string * name, string→string attributes, and text content. */ export const $domDelta: s.Schema<delta.Delta<{ name: string; attrs: { [x: string]: string; }; text: true; recursiveChildren: true; }>>; export function domRDT(dom: Element): DomRDT; export type Schema<T> = import("../../schema.js").Schema<T>; export type RDT<Conf extends delta.DeltaConf> = import("../rdt.js").RDT<Conf>; /** * The {@link delta.DeltaConf} of the deltas a {@link DomRDT} produces/consumes (see {@link $domDelta}). */ export type DomConf = { name: string; attrs: { [key: string]: string; }; text: true; recursiveChildren: true; }; export type DomDelta = delta.Delta<DomConf>; import * as delta from '../delta.js'; import * as s from '../../schema.js'; /** * An RDT backed by a live DOM subtree. DOM mutations observed via `MutationObserver` reconcile the * mirror incrementally (re-reading only changed paths, reusing unchanged subtrees by reference) and the * resulting minimal diff is emitted as a delta; incoming deltas are applied back onto the DOM. * * @implements {RDT<DomConf>} * @extends {ObservableV2<{ delta: (delta: delta.Delta<DomConf>, origin: any) => void, destroy: (rdt: DomRDT) => void }>} */ declare class DomRDT extends ObservableV2<{ delta: (delta: delta.Delta<DomConf>, origin: any) => void; destroy: (rdt: DomRDT) => void; }> implements RDT<DomConf> { /** * @param {Element} observedNode */ constructor(observedNode: Element); /** * @type {Schema<DomDelta>} */ $delta: Schema<DomDelta>; observedNode: Element; /** * Maps each observed DOM node to its mirror delta node in {@link DomRDT#_state}. A {@link reconcile} * reuses an unchanged subtree by reference (skipping its DOM read) via this map; entries for removed * nodes are reclaimed automatically. * * @type {WeakMap<Node, DomDelta>} */ _nodes: WeakMap<Node, DomDelta>; /** * The live delta mirror of the observed subtree, maintained incrementally. A reconcile rebuilds only * the changed paths and swaps this whole root (see {@link pull}); it is never mutated in place, so a * mirror handed out by {@link DomRDT#delta} stays a valid immutable snapshot. * * @type {DomDelta} */ _state: DomDelta; observer: MutationObserver; /** * Apply a foreign delta onto the DOM. * * The DOM may have been edited locally (`b`) since the last sync, concurrently with the incoming * change (`d`). We pull `b` first and rebase the two against each other (OT) so neither is lost: * `d` rebased onto `b` is applied to the DOM, and `b` rebased onto `d` is returned as the fix so the * binding pipes it back through the transformer to the other side. * * @param {delta.Delta<DomConf>} d * @param {any} [origin] who produced `d`; forwarded verbatim on the emitted `'delta'` event so * listeners can recognise (and skip) their own changes — see {@link RDT} “Origins”. Defaults to `null` * (an anonymous/local change). * @return {delta.DeltaBuilder<DomConf> | null} the rebased local change (`b`), or `null` when there * were no concurrent edits. A binding maps a returned fix back onto the other side with the * `correction` origin (see `../rdt.js`) */ applyDelta(d: delta.Delta<DomConf>, origin?: any): delta.DeltaBuilder<DomConf> | null; /** * The current state as a delta: the live mirror ({@link DomRDT#_state}), an "insert everything" delta * describing the observed subtree. A shared read snapshot — consumers clone before mutating (see * {@link RDT}); the mirror is never mutated in place, so a returned snapshot stays valid. * * @return {delta.Delta<DomConf>} */ get delta(): delta.Delta<DomConf>; } import { ObservableV2 } from '../../observable.js'; export {}; //# sourceMappingURL=dom.d.ts.map