##// END OF EJS Templates
Added playground project
Added playground project

File last commit:

r97:8b413dc7fc42 v1.3
r97:8b413dc7fc42 v1.3
Show More
WatchRendition.ts
56 lines | 1.5 KiB | video/mp2t | TypeScriptLexer
/ djx / src / main / ts / tsx / WatchRendition.ts
cin
Added Renderer, WatchRendition
r94 import { id as mid } from "module";
import { TraceSource } from "@implab/core-amd/log/TraceSource";
import { argumentNotNull } from "@implab/core-amd/safe";
cin
refactoring, adding scope to rendering methods
r96 import { render } from "./Renderer";
cin
Added Renderer, WatchRendition
r94 import { RenditionBase } from "./RenditionBase";
cin
refactoring, adding scope to rendering methods
r96 import { IScope, Scope } from "./Scope";
import { Observable } from "../observable";
cin
Added Renderer, WatchRendition
r94
const trace = TraceSource.get(mid);
cin
Added playground project
r97 const noop = () => {};
cin
Added Renderer, WatchRendition
r94 export class WatchRendition<T> extends RenditionBase<Node> {
private readonly _factory: (arg: T) => any;
private _node: Node;
private readonly _scope = new Scope();
cin
refactoring, adding scope to rendering methods
r96 private readonly _subject: Observable<T>;
constructor(component: (arg: T) => any, subject: Observable<T>) {
cin
Added Renderer, WatchRendition
r94 super();
argumentNotNull(component, "component");
this._factory = component;
cin
refactoring, adding scope to rendering methods
r96 this._subject = subject;
cin
Added Renderer, WatchRendition
r94 this._node = document.createComment("WatchRendition placeholder");
}
cin
refactoring, adding scope to rendering methods
r96 protected _create(attrs: object, children: any[], scope: IScope) {
cin
Added Renderer, WatchRendition
r94 scope.own(this._scope);
cin
Added playground project
r97 scope.own(this._subject.on({ next: this._onValue }));
cin
Added Renderer, WatchRendition
r94 }
cin
refactoring, adding scope to rendering methods
r96 private _onValue = (value: T) => void this._render(value).catch( e => trace.error(e));
cin
Added Renderer, WatchRendition
r94 private async _render(value: T) {
cin
refactoring, adding scope to rendering methods
r96 const prevNode = this._node;
cin
Added Renderer, WatchRendition
r94 this._scope.clean();
this._node = await render(() => this._factory(value), this._scope);
cin
refactoring, adding scope to rendering methods
r96 this.placeAt(prevNode, "replace");
cin
Added Renderer, WatchRendition
r94 }
protected _getDomNode() {
if (!this._node)
throw new Error("The instance of the widget isn't created");
return this._node;
}
}