##// END OF EJS Templates
linting
linting

File last commit:

r109:4a375b9c654a default
r109:4a375b9c654a default
Show More
render.ts
112 lines | 3.0 KiB | video/mp2t | TypeScriptLexer
cin
Testing nested watch, release candidate
r101 import { TraceSource } from "@implab/core-amd/log/TraceSource";
import { isPromise } from "@implab/core-amd/safe";
import { id as mid } from "module";
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 import { IScope, Scope } from "./Scope";
import { isNode, isRendition, isWidget } from "./traits";
cin
Testing nested watch, release candidate
r101
const trace = TraceSource.get(mid);
cin
file rename
r98
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 interface Context {
scope: IScope;
cin
file rename
r98
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 hooks?: (() => void)[];
}
cin
Testing nested watch, release candidate
r101
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 let _context: Context = {
scope: Scope.dummy
cin
linting
r109 };
cin
Testing nested watch, release candidate
r101
const guard = (cb: () => unknown) => {
try {
cin
linting
r109 const result = cb();
cin
Testing nested watch, release candidate
r101 if (isPromise(result)) {
const warn = (ret: unknown) => trace.error("The callback {0} competed asynchronously. result = {1}", cb, ret);
result.then(warn, warn);
}
} catch (e) {
trace.error(e);
}
cin
linting
r109 };
cin
file rename
r98
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 export const beginRender = (scope: IScope = getScope()) => {
const prev = _context;
_context = {
scope,
hooks: []
};
return endRender(prev);
cin
linting
r109 };
cin
Testing nested watch, release candidate
r101
/**
* Completes render operation
*/
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 const endRender = (prev: Context) => () => {
const { hooks } = _context;
if (hooks)
cin
Testing nested watch, release candidate
r101 hooks.forEach(guard);
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102
_context = prev;
cin
linting
r109 };
cin
Testing nested watch, release candidate
r101
export const renderHook = (hook: () => void) => {
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 const { hooks } = _context;
if (hooks)
cin
Testing nested watch, release candidate
r101 hooks.push(hook);
else
guard(hook);
cin
linting
r109 };
cin
file rename
r98
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 export const refHook = <T>(value: T, ref: JSX.Ref<T>) => {
const { hooks, scope } = _context;
if (hooks)
hooks.push(() => ref(value));
else
guard(() => ref(value));
scope.own(() => ref(undefined));
cin
linting
r109 };
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102
cin
file rename
r98 /** Returns the current scope */
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 export const getScope = () => _context.scope;
cin
file rename
r98
/** Schedules the rendition to be rendered to the DOM Node
* @param rendition The rendition to be rendered
* @param scope The scope
*/
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 export const render = (rendition: unknown, scope = Scope.dummy) => {
const complete = beginRender(scope);
cin
file rename
r98 try {
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 return getItemDom(rendition);
cin
file rename
r98 } finally {
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 complete();
cin
file rename
r98 }
cin
linting
r109 };
cin
file rename
r98
/** Renders DOM element for different types of the argument. */
cin
Testing nested watch, release candidate
r101 export const getItemDom = (v: unknown) => {
cin
file rename
r98 if (typeof v === "string" || typeof v === "number" || v instanceof RegExp || v instanceof Date) {
// primitive types converted to the text nodes
return document.createTextNode(v.toString());
} else if (isNode(v)) {
// nodes are kept as is
return v;
} else if (isRendition(v)) {
// renditions are instantiated
cin
Testing nested watch, release candidate
r101 return v.getDomNode();
cin
file rename
r98 } else if (isWidget(v)) {
// widgets are converted to it's markup
return v.domNode;
} else if (typeof v === "boolean" || v === null || v === undefined) {
cin
`Subscribable` is made compatible with rxjs, added map, filter and scan...
r102 // null | undefined | boolean are removed
return document.createDocumentFragment();
cin
file rename
r98 } else if (v instanceof Array) {
// arrays will be translated to document fragments
const fragment = document.createDocumentFragment();
cin
Testing nested watch, release candidate
r101 v.map(item => getItemDom(item))
cin
file rename
r98 .forEach(node => fragment.appendChild(node));
return fragment;
} else {
// bug: explicit error otherwise
cin
linting
r109 throw new Error(`Invalid parameter: ${String(v)}`);
cin
file rename
r98 }
cin
linting
r109 };