@glance-networks/agent-plugin
Version:
Glance Networks Agent Plugin
144 lines (143 loc) • 5.19 kB
JavaScript
var S = (s) => {
throw TypeError(s);
};
var v = (s, t, o) => t.has(s) || S("Cannot " + o);
var w = (s, t, o) => (v(s, t, "read from private field"), o ? o.call(s) : t.get(s)), A = (s, t, o) => t.has(s) ? S("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(s) : t.set(s, o), E = (s, t, o, l) => (v(s, t, "write to private field"), l ? l.call(s, o) : t.set(s, o), o);
const x = /* @__PURE__ */ new Set(["string", "number", "boolean"]), M = (s) => {
let t;
return {
// Create
c() {
t = document.createElement("slot"), s && t.setAttribute("name", s);
},
// Mount
m(o, l) {
o.insertBefore(t, l || null);
},
// Props changed
p() {
},
// Detach
d(o) {
o && t.parentNode && t.parentNode.removeChild(t);
}
};
};
function P(s, { name: t, mode: o, styles: l }) {
var p;
if (!globalThis.customElements) {
console.log(
`Component ${t} not registered as there is no customElements in this environment. Perhaps this is an SSR compile, which is not supported for Leo components yet.`
);
return;
}
if (customElements.get(t)) {
console.log(`Attempted to register ${t} component multiple times.`);
return;
}
const u = new s({ target: document.createElement("div") }), d = Object.keys(u.$$.props), b = d.reduce((h, f) => (h.set(f.toLowerCase(), f), h), /* @__PURE__ */ new Map()), y = Array.from(b.keys()), g = new Set(
d.filter((h) => typeof u.$$.ctx[u.$$.props[h]] == "boolean")
);
class R extends HTMLElement {
constructor() {
super();
// @ts-ignore
A(this, p);
this.listenerRemovers = /* @__PURE__ */ new Map();
const r = this.shadowRoot ?? this.attachShadow({ mode: o });
if (l) {
const e = new CSSStyleSheet();
e.replaceSync((l == null ? void 0 : l.default) || l), r.adoptedStyleSheets && (r.adoptedStyleSheets = [e]);
}
r.replaceChildren();
let n = /* @__PURE__ */ new Set();
const i = () => {
var $;
const e = Array.from(this.children).map(
(a) => a.getAttribute("slot")
);
if (this.childNodes.length && e.push(void 0), this.component && // If the size is the same, and every one of our last slots
// is present, then nothing has changed, and we don't need
// to do anything here.
n.size === e.length && e.every((a) => n.has(a)))
return;
n = new Set(e);
const c = e.reduce(
(a, m) => ({
...a,
// @ts-ignore
[m ?? "default"]: [() => M(m)]
}),
{}
), C = Object.keys((($ = this.component) == null ? void 0 : $.$$.props) ?? {}).map((a) => [a, this[a]]).reduce((a, [m, L]) => ({ ...a, [m]: L }), {});
this.component && this.component.$destroy(), this.component = new s({
// Target this shadowDOM, so we get nicely encapsulated
// styles
target: r,
props: {
// Copy over existing props (there might be none, if
// this is our first render).
...C,
// Create WebComponent slots for each Svelte slot we
// have content for. This has to be done at render or
// Svelte won't support fallback content.
$$slots: c,
// Not sure what this is needed for but Svelte crashes
// without it. I think this might be related to slot
// props:
// https://svelte.dev/tutorial/slot-props
$$scope: { ctx: [] }
}
});
};
new MutationObserver(i).observe(this, {
childList: !0,
attributes: !1,
attributeOldValue: !1,
subtree: !1,
characterData: !1,
characterDataOldValue: !1
}), i();
for (const e of d)
Object.defineProperty(this, e, {
enumerable: !0,
get() {
const c = this.component.$$.props[e];
return this.component.$$.ctx[c];
},
set(c) {
x.has(typeof c) && (g.has(e) ? c ? this.setAttribute(e, "") : this.removeAttribute(e) : this.setAttribute(e, c)), this.component.$set({ [e]: c });
}
});
}
get component() {
return w(this, p);
}
set component(r) {
E(this, p, r);
for (const [n, i] of this.listenerRemovers.entries())
for (const [e, c] of i.entries())
c(), this.addEventListener(n, e);
}
static get observedAttributes() {
return y;
}
attributeChangedCallback(r, n, i) {
const e = b.get(r);
e && n !== i && (this[e] = g.has(e) ? i !== null : i);
}
addEventListener(r, n) {
this.listenerRemovers.has(r) || this.listenerRemovers.set(r, /* @__PURE__ */ new Map());
const i = this.component.$on(r, n);
this.listenerRemovers.get(r).set(n, i);
}
removeEventListener(r, n) {
var i, e, c;
(e = (i = this.listenerRemovers.get(r)) == null ? void 0 : i.get(n)) == null || e(), (c = this.listenerRemovers.get(r)) == null || c.delete(n);
}
}
p = new WeakMap(), customElements.define(t, R);
}
export {
P as default
};