-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.js
More file actions
63 lines (56 loc) · 1.97 KB
/
Copy pathelements.js
File metadata and controls
63 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Async } from "./app.js";
export function defineAsyncContainerElement(options = {}) {
const tagName = options.tagName ?? "async-container";
const registry = options.customElements ?? globalThis.customElements;
if (!registry) {
throw new Error("defineAsyncContainerElement(...) requires customElements.");
}
const existing = registry.get(tagName);
if (existing) {
return existing;
}
const app = options.app ?? options.Async ?? Async;
const HTMLElementBase = options.HTMLElement ?? options.window?.HTMLElement ?? globalThis.HTMLElement;
if (!HTMLElementBase) {
throw new Error("defineAsyncContainerElement(...) requires HTMLElement.");
}
class AsyncContainerElement extends HTMLElementBase {
connectedCallback() {
if (this.__asyncAttached) {
return;
}
const runtime = app._runtime ?? app.start?.();
runtime?.attachRoot?.(this);
this.__asyncRuntime = runtime;
this.__asyncAttached = true;
}
disconnectedCallback() {
if (!this.__asyncAttached) {
return;
}
this.__asyncRuntime?.detachRoot?.(this);
this.__asyncRuntime = undefined;
this.__asyncAttached = false;
}
}
registry.define(tagName, AsyncContainerElement);
return AsyncContainerElement;
}
export function defineAsyncSuspenseElement(options = {}) {
const tagName = options.tagName ?? "async-suspense";
const registry = options.customElements ?? globalThis.customElements;
if (!registry) {
throw new Error("defineAsyncSuspenseElement(...) requires customElements.");
}
const existing = registry.get(tagName);
if (existing) {
return existing;
}
const HTMLElementBase = options.HTMLElement ?? options.window?.HTMLElement ?? globalThis.HTMLElement;
if (!HTMLElementBase) {
throw new Error("defineAsyncSuspenseElement(...) requires HTMLElement.");
}
class AsyncSuspenseElement extends HTMLElementBase {}
registry.define(tagName, AsyncSuspenseElement);
return AsyncSuspenseElement;
}