@micro-frame/marko
Version:
A Marko tag for building SSR friendly micro frontends.
72 lines (71 loc) • 2.18 kB
JavaScript
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
import { getSource } from "../../../util/stream";
var require_web_component = __commonJS({
"src/node_modules/@internal/stream-source-component/web.component.ts"(exports, module) {
function readableToAsyncIterator(readable) {
const reader = readable.getReader();
const decoder = new TextDecoder();
return {
async next() {
const { value, done } = await reader.read();
if (done) {
return {
value: void 0,
done
};
}
return {
value: decoder.decode(value),
done
};
}
};
}
module.exports = {
onCreate() {
const ssrEl = document.getElementById(this.id);
if (ssrEl) {
this.src = ssrEl.dataset.src;
}
},
onInput(input) {
this.streamSource = getSource(input.name);
},
onMount() {
this.onUpdate();
},
onDestroy() {
var _a;
(_a = this.controller) == null ? void 0 : _a.abort();
},
async onUpdate() {
var _a;
if (this.src === this.input.src)
return;
this.src = this.input.src;
this.streamSource.invalidate(this.input.src);
(_a = this.controller) == null ? void 0 : _a.abort();
const controller = this.controller = new AbortController();
let err;
try {
const res = await (this.input.fetch || fetch)(this.input.src, {
method: this.input.method,
body: JSON.stringify(this.input.body),
headers: this.input.headers,
cache: this.input.cache,
signal: controller.signal
});
if (!res.ok)
throw new Error(res.statusText);
await this.streamSource.run(this.input.parser(readableToAsyncIterator(res.body)));
} catch (_err) {
err = _err;
this.streamSource.close(err);
}
}
};
}
});
export default require_web_component();