UNPKG

@micro-frame/marko

Version:

A Marko tag for building SSR friendly micro frontends.

87 lines (86 loc) 2.84 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __reExport = (target, module2, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && key !== "default") __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); } return target; }; var __toModule = (module2) => { return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); }; var import_writable_dom = __toModule(require("writable-dom")); var import_stream = __toModule(require("../../../util/stream")); 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 = (0, import_stream.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 = (0, import_writable_dom.default)(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; } };