##// END OF EJS Templates
bump dependencies, bump ts to 5.2...
bump dependencies, bump ts to 5.2 moved observables logic to ObservableImpl fixed while/until bug with complete handler multiple call

File last commit:

r146:af4f8424e83d v1.9.0 default
r155:6acbe6efbe20 v1.10.2 default
Show More
WatchRendition.ts
93 lines | 2.6 KiB | video/mp2t | TypeScriptLexer
/ djx / src / main / ts / tsx / WatchRendition.ts
import { argumentNotNull } from "@implab/core-amd/safe";
import { queueRenderTask, getItemDom, getPriority, getScope } from "./render";
import { RenditionBase } from "./RenditionBase";
import { Scope } from "./Scope";
import { Subscribable } from "../observable";
import { Cancellation } from "@implab/core-amd/Cancellation";
import { collectNodes, destroy, isDocumentFragmentNode, isMounted, placeAt, startupWidgets } from "./traits";
export class WatchRendition<T> extends RenditionBase<Node> {
private readonly _component: (arg: T) => unknown;
private readonly _node: Node;
private readonly _scope = new Scope();
private readonly _subject: Subscribable<T>;
private _renderJob?: { value: T };
private _ct = Cancellation.none;
private _priority = 0;
constructor(component: (arg: T) => unknown, subject: Subscribable<T>) {
super();
argumentNotNull(component, "component");
this._component = component;
this._subject = subject;
this._node = document.createComment("[Watch]");
}
protected _create() {
const scope = getScope();
this._priority = getPriority() + 1;
scope.own(() => {
this._scope.destroy();
destroy(this._node);
});
scope.own(this._subject.subscribe({ next: this._onValue }));
this._ct = new Cancellation(cancel => scope.own(cancel));
}
private readonly _onValue = (value: T) => {
if (!this._renderJob) {
// schedule a new job
this._renderJob = { value };
queueRenderTask(this._render, this._scope, this._priority);
} else {
// update existing job
this._renderJob = { value };
}
};
private readonly _render = () => {
// don't render destroyed rendition
if (this._ct.isRequested())
return;
// remove all previous content
this._scope.clean();
// render the new node
const node = getItemDom(this._renderJob ? this._component(this._renderJob.value) : undefined);
// get actual content
const pending = isDocumentFragmentNode(node) ?
collectNodes(node.childNodes) :
[node];
placeAt(node, this._node, "after");
if (isMounted(this._node))
pending.forEach(n => startupWidgets(n));
if (pending.length)
this._scope.own(() => pending.forEach(destroy));
this._renderJob = undefined;
};
protected _getDomNode() {
if (!this._node)
throw new Error("The instance of the widget isn't created");
return this._node;
}
}