UNPKG

@thi.ng/rdom

Version:

Lightweight, reactive, VDOM-less UI/DOM components with async lifecycle and @thi.ng/hiccup compatible

54 lines (53 loc) 1.34 kB
import { isString } from "@thi.ng/checks/is-string"; import { defSetterUnsafe } from "@thi.ng/paths/setter"; import { __nextID } from "@thi.ng/rstream/idgen"; import { Subscription } from "@thi.ng/rstream/subscription"; import { $attribs } from "./dom.js"; import { $wrapText } from "./wrap.js"; function $sub(src, tag, attribs) { return src.subscribe(new $Sub(isString(tag) ? $wrapText(tag, attribs) : tag)); } const $subWithID = (src, inner, id) => src.subscribe(new $Sub(inner, id)); class $Sub extends Subscription { constructor(inner, id) { super(void 0, { id: id || `rdom$sub-${__nextID()}` }); this.inner = inner; } el; async mount(parent, index = -1) { return this.el = await this.inner.mount( parent, index, this.parent.deref() ); } async unmount() { this.unsubscribe(); this.el = void 0; await this.inner.unmount(); } update(x) { this.next(x); } next(x) { if (this.el) this.inner.update(x); } } class $SubA extends Subscription { constructor(comp, path, id) { super(void 0, { id: id || `rdom$attr-${__nextID()}` }); this.comp = comp; this.setter = defSetterUnsafe(path); } setter; attr = {}; next(a) { if (this.comp.el) $attribs(this.comp.el, this.setter(this.attr, a)); } } export { $Sub, $SubA, $sub, $subWithID };