ajo
Version:
ajo is a JavaScript view library for building user interfaces
25 lines (24 loc) • 1.11 kB
JavaScript
import { render as l, h as m } from "./index.js";
import { html as u } from "./html.js";
const y = async function* (i, n = "") {
const s = /* @__PURE__ */ new Map([[n, 0]]), e = /* @__PURE__ */ new Set(), o = [], c = (t = n) => (s.set(t, (s.get(t) ?? 0) + 1), t ? `${t}:${s.get(t) - 1}` : String(s.get(t) - 1)), d = (t, r) => ({ nodeName: "div", "data-ssr": t, children: r }), f = (t) => {
const r = Promise.resolve(`<script>window.$stream?.push(${JSON.stringify(t)})<\/script>`);
e.add(r), r.then((h) => o.push(h)).finally(() => e.delete(r));
};
for (const t of u(i, { alloc: c, placeholder: d, push: f })) yield t;
for (; e.size || o.length; ) {
for (; o.length; ) yield o.shift();
e.size && await Promise.race(e);
}
}, a = /* @__PURE__ */ new Set();
async function p({ id: i, src: n, h: s }) {
const e = document.querySelector(`[data-ssr="${i}"]`);
if (!e) return a.add({ id: i, src: n, h: s });
n ? l(m((await import(n)).default, s), e) : l(s, e);
const o = i + ":";
for (const c of a) c.id.startsWith(o) && (a.delete(c), p(c));
}
export {
p as hydrate,
y as stream
};