json-joy
Version:
Collection of libraries for building collaborative editing apps.
42 lines (41 loc) • 1.46 kB
JavaScript
import { MapFanOut, OnNewFanOut } from './fanout';
export class NodeEvents {
api;
/**
* Fired on any model change, even if the node's value has not changed. The
* changes are fired once per microtask, so multiple changes in the same
* microtask are batched into a single event.
*/
onChanges;
/**
* Similar to `.onChanges`, but fired when the node's view has changed,
* checked using triple equality `===`.
*
* The strict equality identity is preserved deeply equal values, even for
* objects and arrays. So, this event will not fire if there was a change
* to the node's value, but the value is still deeply equal to the previous
* value.
*
* This event depends on overall Model's `onChanges` event, which is
* batched using `queueMicrotask`.
*/
onViewChanges;
constructor(api) {
this.api = api;
this.onChanges = new MapFanOut(this.api.api.onChanges, this.getSnapshot);
this.onViewChanges = new OnNewFanOut(this.onChanges, this.api.view());
}
/**
* Called when this node is deleted.
*
* @internal
* @ignore
*/
handleDelete() {
this.onViewChanges.clear();
this.onChanges.clear();
}
// ---------------------------------------------------------------- SyncStore
subscribe = (callback) => this.onViewChanges.listen(() => callback());
getSnapshot = () => this.api.view();
}