UNPKG

@micro-frame/marko

Version:

A Marko tag for building SSR friendly micro frontends.

79 lines (78 loc) 2.32 kB
var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; import getWritableDOM from "writable-dom"; import { getSource } from "../../../util/stream"; var require_web_component = __commonJS({ "src/node_modules/@internal/micro-frame-slot-component/web.component.ts"(exports, module) { module.exports = { onCreate() { const ssrEl = document.getElementById(this.id); let loading = true; if (ssrEl) { this.slotId = ssrEl.dataset.slot; loading = false; } this.state = { loading, err: void 0 }; this.handleSrcChange = this.handleSrcChange.bind(this); }, onInput(input) { if (this.from !== input.from) { this.from = input.from; this.streamSource = getSource(input.from); if (!input.noRefresh) { this.streamSource.onInvalidate(this.handleSrcChange); } } }, onMount() { this.onUpdate(); }, handleSrcChange(src) { this.curSrc = src; this.forceUpdate(); }, onDestroy() { var _a; (_a = this.slot) == null ? void 0 : _a.end(); this.streamSource.offInvalidate(this.handleSrcChange); }, async onUpdate() { if (this.slotId === this.input.slot && this.prevSrc === this.curSrc) return; this.state.loading = true; this.state.err = void 0; this.slotId = this.input.slot; this.prevSrc = this.curSrc; let writable; let err; try { this.slot = this.streamSource.slot(this.slotId); if (!this.slot) { return; } writable = getWritableDOM(this.el, this.el.lastChild.previousSibling); while (true) { const { value, done } = await this.slot.next(); if (done) break; writable.write(value); } await writable.close(); } catch (_err) { err = _err; } if (err && !this.input.catch) throw err; this.state.loading = false; this.state.err = err; } }; } }); export default require_web_component();