@tokamaq/sdk 0.0.0-stage → 0.1.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1403 -2
- package/build-internals.ts +25 -0
- package/caller/agent-launch.ts +60 -0
- package/client.tsx +498 -0
- package/component-actions.ts +327 -0
- package/component-alerts.ts +136 -0
- package/component-attributes.ts +340 -0
- package/component-parts.tsx +1177 -0
- package/core-contracts/auth.ts +40 -0
- package/package.json +98 -4
- package/quit.ts +58 -0
- package/regions.tsx +94 -0
- package/screen-search.ts +211 -0
- package/send-file.ts +175 -0
- package/server-build-internals.ts +11 -0
- package/solid.ts +132 -0
- package/src/agent-command.ts +48 -0
- package/src/agent-info-cache.ts +34 -0
- package/src/agent-reports.ts +140 -0
- package/src/agent-turn.ts +289 -0
- package/src/api.ts +174 -0
- package/src/auth.ts +39 -0
- package/src/call-error.ts +37 -0
- package/src/caller-storage.ts +32 -0
- package/src/client.ts +163 -0
- package/src/database.ts +322 -0
- package/src/functions.ts +726 -0
- package/src/hooks.ts +141 -0
- package/src/host.ts +211 -0
- package/src/http-action.ts +55 -0
- package/src/index.ts +126 -0
- package/src/internal/contracts.ts +260 -0
- package/src/internal/machine-scope.ts +21 -0
- package/src/local.ts +22 -0
- package/src/plugin.ts +162 -0
- package/src/points.ts +79 -0
- package/src/protocol.ts +64 -0
- package/src/schema.ts +136 -0
- package/src/storage.ts +193 -0
- package/src/thread-context.ts +42 -0
- package/src/transport.ts +421 -0
- package/src/triggers.ts +87 -0
- package/src/unstable.ts +66 -0
- package/src/validators.ts +248 -0
- package/src/values.ts +98 -0
- package/src/visibility.ts +8 -0
- package/src/websocket-transport.ts +91 -0
- package/src/window-state-protocol.ts +7 -0
- package/src/within.ts +24 -0
- package/src/worker-limits.ts +5 -0
- package/tools/bundle.mjs +490 -0
- package/tools/client-entry.mjs +10 -0
- package/tools/contracts.mjs +254 -0
- package/tools/http-actions.mjs +42 -0
- package/tools/placement.mjs +535 -0
- package/tools/plugin-boundary.mjs +592 -0
- package/tools/plugin-compiler.mjs +125 -0
- package/tools/plugin-descriptor.ts +75 -0
- package/tools/points.mjs +340 -0
- package/tools/result-validator.mjs +83 -0
- package/tools/server-builtins.mjs +82 -0
- package/tools/server-files.mjs +34 -0
- package/tools/table-references.mjs +98 -0
- package/tools/worker.mjs +110 -0
- package/tsconfig.plugin-server.json +12 -0
- package/tsconfig.plugin-window.json +15 -0
- package/view.ts +27 -0
- package/web.d.ts +6 -0
- package/window-state-host.ts +228 -0
- package/window-state.ts +43 -0
|
@@ -0,0 +1,1177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createComponent,
|
|
3
|
+
createContext,
|
|
4
|
+
createEffect,
|
|
5
|
+
createMemo,
|
|
6
|
+
createRenderEffect,
|
|
7
|
+
createRoot,
|
|
8
|
+
createSignal,
|
|
9
|
+
flatten,
|
|
10
|
+
getOwner,
|
|
11
|
+
onCleanup,
|
|
12
|
+
onSettled,
|
|
13
|
+
runWithOwner,
|
|
14
|
+
Errored,
|
|
15
|
+
For,
|
|
16
|
+
merge,
|
|
17
|
+
Show,
|
|
18
|
+
untrack,
|
|
19
|
+
useContext,
|
|
20
|
+
} from "solid-js";
|
|
21
|
+
import { insert, type JSX } from "@solidjs/web";
|
|
22
|
+
import { screenElementName } from "./screen-search";
|
|
23
|
+
import {
|
|
24
|
+
componentActionKind,
|
|
25
|
+
type ComponentAction,
|
|
26
|
+
type ComponentActionKind,
|
|
27
|
+
type OriginalElement,
|
|
28
|
+
} from "./component-actions";
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
attributeValues,
|
|
32
|
+
bindNativeAttributes,
|
|
33
|
+
installationOrder,
|
|
34
|
+
noAttributeLayers,
|
|
35
|
+
warnAttribute,
|
|
36
|
+
type AttributeLayers,
|
|
37
|
+
type AttributeWarning,
|
|
38
|
+
} from "./component-attributes";
|
|
39
|
+
|
|
40
|
+
export interface LoadedPartAction {
|
|
41
|
+
id: string;
|
|
42
|
+
fingerprint?: string;
|
|
43
|
+
plugin: string;
|
|
44
|
+
order: number;
|
|
45
|
+
installOrder?: number;
|
|
46
|
+
callableAttributes?: ReadonlySet<string>;
|
|
47
|
+
action: ComponentAction;
|
|
48
|
+
/** Reads the action's `when` with the props of the component that owns the element. */
|
|
49
|
+
enabled(props: object): boolean;
|
|
50
|
+
}
|
|
51
|
+
type ChildOverride = () => JSX.Element;
|
|
52
|
+
type LayerContents = {
|
|
53
|
+
draw: ChildOverride;
|
|
54
|
+
fail(cause: unknown): void;
|
|
55
|
+
};
|
|
56
|
+
type ReadOverride = () => LayerContents | undefined;
|
|
57
|
+
|
|
58
|
+
function drawContents(contents: LayerContents): JSX.Element {
|
|
59
|
+
return (
|
|
60
|
+
<Errored
|
|
61
|
+
fallback={(cause) => {
|
|
62
|
+
contents.fail(cause());
|
|
63
|
+
return undefined;
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
{contents.draw()}
|
|
67
|
+
</Errored>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
type Contents = {
|
|
71
|
+
native?: { read: ReadOverride; capture(children: ChildOverride): void };
|
|
72
|
+
(override?: () => JSX.Element): JSX.Element;
|
|
73
|
+
prepend?: () => JSX.Element;
|
|
74
|
+
append?: () => JSX.Element;
|
|
75
|
+
c: typeof createComponent;
|
|
76
|
+
spread(props: { readonly children?: JSX.Element }): object;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export interface PartObserver {
|
|
80
|
+
update(actions: readonly LoadedPartAction[]): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface PartRegistry {
|
|
84
|
+
mismatch(plugin: string, target: string): void;
|
|
85
|
+
warn(
|
|
86
|
+
key: readonly [kind: string, plugin: string, target: string],
|
|
87
|
+
message: string,
|
|
88
|
+
detail?: AttributeWarning,
|
|
89
|
+
): void;
|
|
90
|
+
failure(item: LoadedPartAction, cause: unknown, consoleMessage: string): void;
|
|
91
|
+
interference(
|
|
92
|
+
kind: string,
|
|
93
|
+
cause: LoadedPartAction,
|
|
94
|
+
affected: LoadedPartAction,
|
|
95
|
+
message: string,
|
|
96
|
+
): () => void;
|
|
97
|
+
inside(target: string): readonly LoadedPartAction[];
|
|
98
|
+
read(target: string): () => readonly LoadedPartAction[];
|
|
99
|
+
observe(target: string, observer: PartObserver): void;
|
|
100
|
+
unobserve(target: string, observer: PartObserver): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const PartActions = createContext<PartRegistry | null>(null);
|
|
104
|
+
|
|
105
|
+
export { installationOrder as compareInstallOrder } from "./component-attributes";
|
|
106
|
+
|
|
107
|
+
const noProps = {};
|
|
108
|
+
|
|
109
|
+
/** Compiler entry point, owned by the render function rather than any of its parts. */
|
|
110
|
+
export function attachRenderFunction(target: string, props?: object) {
|
|
111
|
+
// A component function can also be called as a plain helper, outside any
|
|
112
|
+
// owner (from a click handler, say). There is no instance to attach to.
|
|
113
|
+
if (!getOwner()) return;
|
|
114
|
+
const registry = useContext(PartActions);
|
|
115
|
+
if (!registry) return;
|
|
116
|
+
const observer = new RenderAttachment(registry, target, props ?? noProps);
|
|
117
|
+
onCleanup(() => observer.stop());
|
|
118
|
+
registry.observe(target, observer);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class RenderAttachment implements PartObserver {
|
|
122
|
+
private dispose?: () => void;
|
|
123
|
+
private write?: (actions: readonly LoadedPartAction[]) => void;
|
|
124
|
+
private owner = getOwner();
|
|
125
|
+
|
|
126
|
+
constructor(
|
|
127
|
+
private registry: PartRegistry,
|
|
128
|
+
private target: string,
|
|
129
|
+
private props: object,
|
|
130
|
+
) {}
|
|
131
|
+
|
|
132
|
+
stop() {
|
|
133
|
+
this.registry.unobserve(this.target, this);
|
|
134
|
+
this.dispose?.();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
update(actions: readonly LoadedPartAction[]) {
|
|
138
|
+
if (!actions.length) {
|
|
139
|
+
this.dispose?.();
|
|
140
|
+
this.dispose = this.write = undefined;
|
|
141
|
+
} else if (this.write) this.write(actions);
|
|
142
|
+
else
|
|
143
|
+
runWithOwner(this.owner, () =>
|
|
144
|
+
createRoot((dispose) => {
|
|
145
|
+
this.dispose = dispose;
|
|
146
|
+
const [read, write] = createSignal(actions, { ownedWrite: true });
|
|
147
|
+
this.write = write;
|
|
148
|
+
const tree = <For each={read()}>{(item) => attached(item, this.props, this.registry)}</For>;
|
|
149
|
+
// Evaluate lazy children (including portals) without inserting the
|
|
150
|
+
// returned nodes. A Portal can render directly into its outlet.
|
|
151
|
+
createRenderEffect(
|
|
152
|
+
() => flatten(tree),
|
|
153
|
+
() => {},
|
|
154
|
+
);
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function attached(item: LoadedPartAction, props: object, registry: PartRegistry) {
|
|
161
|
+
const [failed, setFailed] = createSignal(false, { ownedWrite: true });
|
|
162
|
+
const owner = getOwner();
|
|
163
|
+
return (
|
|
164
|
+
<Show when={!failed()}>
|
|
165
|
+
<Errored
|
|
166
|
+
fallback={(cause) => {
|
|
167
|
+
registry.failure(
|
|
168
|
+
item,
|
|
169
|
+
cause(),
|
|
170
|
+
`Component attachment ${item.plugin}/${item.id} failed: ${String(cause())}`,
|
|
171
|
+
);
|
|
172
|
+
// Solid's error boundary retains the failed scope for a possible
|
|
173
|
+
// reset. Dropping the boundary disposes it and runs its cleanups.
|
|
174
|
+
runWithOwner(owner, () =>
|
|
175
|
+
onSettled(() => {
|
|
176
|
+
setFailed(true);
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
return undefined;
|
|
180
|
+
}}
|
|
181
|
+
>
|
|
182
|
+
<Show when={item.enabled(props)}>
|
|
183
|
+
{createComponent((item.action as ComponentAction<"attach">).layer!, props)}
|
|
184
|
+
</Show>
|
|
185
|
+
</Errored>
|
|
186
|
+
</Show>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
type NativeSite = readonly [
|
|
191
|
+
target: string,
|
|
192
|
+
fingerprint: string | null | undefined,
|
|
193
|
+
children: readonly NativeSite[],
|
|
194
|
+
ref?: number | null,
|
|
195
|
+
hole?: number | null,
|
|
196
|
+
];
|
|
197
|
+
type HoleValue = JSX.Element | (() => JSX.Element);
|
|
198
|
+
type RenderHole = (id: number, render: () => HoleValue) => () => HoleValue;
|
|
199
|
+
type ParkedHoles = Map<Text, () => HoleValue>;
|
|
200
|
+
type HoleState = boolean | ParkedHoles;
|
|
201
|
+
|
|
202
|
+
function matches(
|
|
203
|
+
registry: PartRegistry,
|
|
204
|
+
target: string,
|
|
205
|
+
fingerprint: string | null | undefined,
|
|
206
|
+
item: LoadedPartAction,
|
|
207
|
+
) {
|
|
208
|
+
if (!item.fingerprint || !fingerprint || item.fingerprint === fingerprint) return true;
|
|
209
|
+
registry.mismatch(item.plugin, target);
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const plainHole: RenderHole = (_id, render) => render;
|
|
214
|
+
const plainSpread = (props: object) => props;
|
|
215
|
+
const isAttributes = (item: LoadedPartAction) => componentActionKind(item.action) === "attrs";
|
|
216
|
+
const orderedAttributes = (actions: readonly LoadedPartAction[]) =>
|
|
217
|
+
actions.filter(isAttributes).sort(installationOrder);
|
|
218
|
+
const asElement = (value: JSX.Element) =>
|
|
219
|
+
value && typeof value === "object" && "nodeType" in value && value.nodeType === 1
|
|
220
|
+
? (value as Element)
|
|
221
|
+
: undefined;
|
|
222
|
+
|
|
223
|
+
// Native templates own their DOM nodes. Additions get their own insertion
|
|
224
|
+
// ranges, so changing one path leaves the other nodes and observers alone.
|
|
225
|
+
function nativeRange(parent: ParentNode, before: Node | null, original?: Element) {
|
|
226
|
+
const start = parent.ownerDocument!.createTextNode("");
|
|
227
|
+
const end = parent.ownerDocument!.createTextNode("");
|
|
228
|
+
parent.insertBefore(start, original ?? before);
|
|
229
|
+
parent.insertBefore(end, before);
|
|
230
|
+
onCleanup(() => {
|
|
231
|
+
if (original) parent.insertBefore(original, end);
|
|
232
|
+
while (start.nextSibling && start.nextSibling !== end && start.nextSibling !== original)
|
|
233
|
+
parent.removeChild(start.nextSibling);
|
|
234
|
+
start.remove();
|
|
235
|
+
end.remove();
|
|
236
|
+
});
|
|
237
|
+
return end;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
type NativeContents = {
|
|
241
|
+
resume(): void;
|
|
242
|
+
suspend(park?: ParkedHoles): void;
|
|
243
|
+
project(parent: ParentNode, nodes: Node[]): void;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
function nativeContents(element: Element, contents: Contents, children?: NativeContents) {
|
|
247
|
+
const { prepend, append, native } = contents;
|
|
248
|
+
if (!native) return nativeAdditions(element, prepend, append);
|
|
249
|
+
if (
|
|
250
|
+
element.namespaceURI === "http://www.w3.org/1999/xhtml" &&
|
|
251
|
+
/^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/.test(element.localName)
|
|
252
|
+
)
|
|
253
|
+
return;
|
|
254
|
+
|
|
255
|
+
const changed = createMemo(() => !!native.read());
|
|
256
|
+
createEffect(
|
|
257
|
+
() => {
|
|
258
|
+
const owner = getOwner();
|
|
259
|
+
if (!changed()) {
|
|
260
|
+
onSettled(() =>
|
|
261
|
+
runWithOwner(owner, () => {
|
|
262
|
+
children?.resume();
|
|
263
|
+
nativeAdditions(element, prepend, append);
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
// Component holes unload before moving children. Leave an anchor for each
|
|
269
|
+
// direct hole so projection can mount it in its new parent on demand.
|
|
270
|
+
const holes: ParkedHoles = new Map();
|
|
271
|
+
children?.suspend(holes);
|
|
272
|
+
let restore: (() => void) | undefined;
|
|
273
|
+
onCleanup(() => restore?.());
|
|
274
|
+
onSettled(() => {
|
|
275
|
+
const parked = Array.from(element.childNodes);
|
|
276
|
+
let projection: (() => void) | undefined;
|
|
277
|
+
native.capture(() => {
|
|
278
|
+
const anchor = element.ownerDocument.createTextNode("");
|
|
279
|
+
const owner = getOwner();
|
|
280
|
+
let live = true;
|
|
281
|
+
const stop = () => {
|
|
282
|
+
live = false;
|
|
283
|
+
projection?.();
|
|
284
|
+
projection = undefined;
|
|
285
|
+
};
|
|
286
|
+
onCleanup(stop);
|
|
287
|
+
onSettled(() => {
|
|
288
|
+
if (!live || !anchor.parentNode) return;
|
|
289
|
+
const parent = anchor.parentNode;
|
|
290
|
+
runWithOwner(owner, () =>
|
|
291
|
+
createRoot((dispose) => {
|
|
292
|
+
projection = dispose;
|
|
293
|
+
children?.project(parent, parked);
|
|
294
|
+
for (const [marker, draw] of holes) {
|
|
295
|
+
if (marker.parentNode === parent)
|
|
296
|
+
insert(parent, () => untrack(draw), nativeRange(parent, marker));
|
|
297
|
+
}
|
|
298
|
+
}),
|
|
299
|
+
);
|
|
300
|
+
});
|
|
301
|
+
return [...parked, anchor];
|
|
302
|
+
});
|
|
303
|
+
element.replaceChildren();
|
|
304
|
+
restore = () => {
|
|
305
|
+
projection?.();
|
|
306
|
+
// Observers must see the restored DOM the next time they register.
|
|
307
|
+
element.replaceChildren(...parked);
|
|
308
|
+
native.capture(() => undefined);
|
|
309
|
+
};
|
|
310
|
+
runWithOwner(owner, () =>
|
|
311
|
+
insert(element, () => {
|
|
312
|
+
const override = native.read();
|
|
313
|
+
return override && drawContents(override);
|
|
314
|
+
}),
|
|
315
|
+
);
|
|
316
|
+
});
|
|
317
|
+
},
|
|
318
|
+
() => {},
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const textEffect = { sync: true, transparent: true };
|
|
323
|
+
|
|
324
|
+
export function renderNativeText(parent: Element, read: () => string | number, before: Node | null = null) {
|
|
325
|
+
let node: Text | undefined;
|
|
326
|
+
createRenderEffect(
|
|
327
|
+
read,
|
|
328
|
+
(value) => {
|
|
329
|
+
const text = String(value ?? "");
|
|
330
|
+
if (node) node.data = text;
|
|
331
|
+
else node = parent.insertBefore(parent.ownerDocument.createTextNode(text), before);
|
|
332
|
+
},
|
|
333
|
+
textEffect,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function nativeAdditions(element: Element, prepend?: ChildOverride, append?: ChildOverride) {
|
|
338
|
+
if (prepend) insert(element, prepend(), nativeRange(element, element.firstChild));
|
|
339
|
+
if (append) insert(element, append(), nativeRange(element, null));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function renderNativePart(
|
|
343
|
+
target: string,
|
|
344
|
+
fingerprint: string | null | undefined,
|
|
345
|
+
props: object,
|
|
346
|
+
row: (() => object) | undefined,
|
|
347
|
+
create: (refs: Element[], hole: RenderHole) => JSX.Element,
|
|
348
|
+
children: readonly NativeSite[],
|
|
349
|
+
rootHole?: number,
|
|
350
|
+
) {
|
|
351
|
+
const registry = useContext(PartActions);
|
|
352
|
+
if (!registry) return create([], plainHole);
|
|
353
|
+
if (!children.length)
|
|
354
|
+
return renderPart(
|
|
355
|
+
target,
|
|
356
|
+
fingerprint,
|
|
357
|
+
props,
|
|
358
|
+
row,
|
|
359
|
+
(contents) => {
|
|
360
|
+
const element = create([], plainHole) as Element;
|
|
361
|
+
nativeContents(element, contents);
|
|
362
|
+
return element;
|
|
363
|
+
},
|
|
364
|
+
undefined,
|
|
365
|
+
false,
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
return renderPart(
|
|
369
|
+
target,
|
|
370
|
+
fingerprint,
|
|
371
|
+
props,
|
|
372
|
+
row,
|
|
373
|
+
(contents) => {
|
|
374
|
+
const refs: Element[] = [];
|
|
375
|
+
const active: ReturnType<typeof createSignal<HoleState>>[] = [];
|
|
376
|
+
const hole: RenderHole = (id, render) => {
|
|
377
|
+
let state = active[id];
|
|
378
|
+
if (!state) active[id] = state = createSignal<HoleState>(true, { ownedWrite: true });
|
|
379
|
+
const [shown] = state;
|
|
380
|
+
return createMemo(() => {
|
|
381
|
+
const value = shown();
|
|
382
|
+
if (value === true) return untrack(render);
|
|
383
|
+
if (value === false) return undefined;
|
|
384
|
+
const marker = element.ownerDocument.createTextNode("");
|
|
385
|
+
value.set(marker, render);
|
|
386
|
+
return marker;
|
|
387
|
+
});
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
function setHoles(id: number | null | undefined, sites: readonly NativeSite[], enabled: HoleState) {
|
|
391
|
+
if (typeof id === "number") {
|
|
392
|
+
const state = active[id];
|
|
393
|
+
if (!state) active[id] = createSignal(enabled, { ownedWrite: true });
|
|
394
|
+
else state[1](enabled);
|
|
395
|
+
}
|
|
396
|
+
for (const [, , descendants, , hole] of sites) setHoles(hole, descendants, enabled === true);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const element = create(refs, hole) as Element;
|
|
400
|
+
const scope = { registry, refs, owner: getOwner(), props, row, setHoles };
|
|
401
|
+
let bindings: NativeBinding[] | undefined = registerNative(element, children, scope);
|
|
402
|
+
onCleanup(() => stopNative(bindings));
|
|
403
|
+
if (contents.native)
|
|
404
|
+
nativeContents(element, contents, {
|
|
405
|
+
resume() {
|
|
406
|
+
if (bindings) return;
|
|
407
|
+
setHoles(rootHole, children, true);
|
|
408
|
+
bindings = registerNative(element, children, scope);
|
|
409
|
+
},
|
|
410
|
+
suspend(park) {
|
|
411
|
+
stopNative(bindings);
|
|
412
|
+
bindings = undefined;
|
|
413
|
+
setHoles(rootHole, children, park ?? false);
|
|
414
|
+
},
|
|
415
|
+
project: (parent, nodes) => projectNative(parent, nodes, children, scope),
|
|
416
|
+
});
|
|
417
|
+
else nativeContents(element, contents);
|
|
418
|
+
return element;
|
|
419
|
+
},
|
|
420
|
+
undefined,
|
|
421
|
+
false,
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
type NativeScope = {
|
|
426
|
+
registry: PartRegistry;
|
|
427
|
+
refs: Element[];
|
|
428
|
+
owner: ReturnType<typeof getOwner>;
|
|
429
|
+
props: object;
|
|
430
|
+
row: (() => object) | undefined;
|
|
431
|
+
setHoles(id: number | null | undefined, sites: readonly NativeSite[], enabled: HoleState): void;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
function registerNative(
|
|
435
|
+
node: ParentNode,
|
|
436
|
+
sites: readonly NativeSite[],
|
|
437
|
+
scope: NativeScope,
|
|
438
|
+
projected?: Node[],
|
|
439
|
+
) {
|
|
440
|
+
let child = projected
|
|
441
|
+
? (projected.find((candidate): candidate is Element => candidate.nodeType === 1) ?? null)
|
|
442
|
+
: node.firstElementChild;
|
|
443
|
+
const bindings: NativeBinding[] = [];
|
|
444
|
+
for (let i = 0; i < sites.length; i++) {
|
|
445
|
+
const site = sites[i]!;
|
|
446
|
+
const ref = site[3];
|
|
447
|
+
const element = typeof ref === "number" ? scope.refs[ref] : child;
|
|
448
|
+
if (!element || (typeof ref === "number" && element.parentNode !== node)) continue;
|
|
449
|
+
if (i + 1 < sites.length && typeof sites[i + 1]![3] !== "number") child = element.nextElementSibling;
|
|
450
|
+
const binding = new NativeBinding(scope, element, site);
|
|
451
|
+
bindings.push(binding);
|
|
452
|
+
binding.start();
|
|
453
|
+
}
|
|
454
|
+
return bindings;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function projectNative(parent: ParentNode, nodes: Node[], sites: readonly NativeSite[], scope: NativeScope) {
|
|
458
|
+
// Only direct holes need their insertions rebuilt. Inside a moved element,
|
|
459
|
+
// existing insertion ranges still point to their original parent.
|
|
460
|
+
function showHoles(shown: boolean) {
|
|
461
|
+
for (const [, , descendants, , hole] of sites) scope.setHoles(hole, descendants, shown);
|
|
462
|
+
}
|
|
463
|
+
showHoles(true);
|
|
464
|
+
const bindings = registerNative(parent, sites, scope, nodes);
|
|
465
|
+
onCleanup(() => {
|
|
466
|
+
stopNative(bindings);
|
|
467
|
+
showHoles(false);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function stopNative(bindings: NativeBinding[] | undefined) {
|
|
472
|
+
if (bindings) for (const binding of bindings) binding.stop();
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
class NativeBinding implements PartObserver {
|
|
476
|
+
private dispose?: () => void;
|
|
477
|
+
private inner?: NativeBinding[];
|
|
478
|
+
|
|
479
|
+
constructor(
|
|
480
|
+
private scope: NativeScope,
|
|
481
|
+
private element: Element,
|
|
482
|
+
private site: NativeSite,
|
|
483
|
+
) {}
|
|
484
|
+
|
|
485
|
+
start() {
|
|
486
|
+
this.scope.registry.observe(this.site[0], this);
|
|
487
|
+
if (!this.dispose) this.registerChildren();
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
stop() {
|
|
491
|
+
this.scope.registry.unobserve(this.site[0], this);
|
|
492
|
+
this.dispose?.();
|
|
493
|
+
stopNative(this.inner);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
private registerChildren() {
|
|
497
|
+
if (this.site[2].length) this.inner ??= registerNative(this.element, this.site[2], this.scope);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
update(installed: readonly LoadedPartAction[]) {
|
|
501
|
+
this.dispose?.();
|
|
502
|
+
this.dispose = undefined;
|
|
503
|
+
const { registry, owner, props, row, setHoles } = this.scope;
|
|
504
|
+
const [target, fingerprint, children, , hole] = this.site;
|
|
505
|
+
const actions = installed.filter((item) => matches(registry, target, fingerprint, item));
|
|
506
|
+
if (!actions.length) {
|
|
507
|
+
if (typeof hole === "number" || children.length) setHoles(hole, children, true);
|
|
508
|
+
this.registerChildren();
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const destructive = actions.some(({ action }) => {
|
|
512
|
+
const kind = componentActionKind(action);
|
|
513
|
+
return kind === "hide" || kind === "layer";
|
|
514
|
+
});
|
|
515
|
+
const deactivate = (park?: ParkedHoles) => {
|
|
516
|
+
stopNative(this.inner);
|
|
517
|
+
this.inner = undefined;
|
|
518
|
+
setHoles(hole, children, park ?? false);
|
|
519
|
+
};
|
|
520
|
+
if (destructive) deactivate();
|
|
521
|
+
|
|
522
|
+
const element = this.element;
|
|
523
|
+
const parent = element.parentNode! as ParentNode;
|
|
524
|
+
const placementProps = row ? merge(props, { $row: row() }) : props;
|
|
525
|
+
if (actions.every(isAttributes)) {
|
|
526
|
+
setHoles(hole, children, true);
|
|
527
|
+
this.registerChildren();
|
|
528
|
+
const ordered = actions.sort(installationOrder);
|
|
529
|
+
this.dispose = runWithOwner(owner, () =>
|
|
530
|
+
createRoot((cleanup) => {
|
|
531
|
+
bindNativeAttributes(element, (owner, native) =>
|
|
532
|
+
attributeValues(registry, placementProps, owner, ordered, undefined, native),
|
|
533
|
+
);
|
|
534
|
+
return cleanup;
|
|
535
|
+
}),
|
|
536
|
+
);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const next = element.nextSibling;
|
|
540
|
+
runWithOwner(owner, () =>
|
|
541
|
+
createRoot((cleanup) => {
|
|
542
|
+
this.dispose = cleanup;
|
|
543
|
+
const end = nativeRange(parent, next, element);
|
|
544
|
+
const decorated = renderActions(
|
|
545
|
+
registry,
|
|
546
|
+
actions,
|
|
547
|
+
placementProps,
|
|
548
|
+
(contents) => {
|
|
549
|
+
const activate = () => {
|
|
550
|
+
// Always: a layer removed just before this rebuild left the child
|
|
551
|
+
// components off, and the actions that remain may all be additions.
|
|
552
|
+
setHoles(hole, children, true);
|
|
553
|
+
this.registerChildren();
|
|
554
|
+
};
|
|
555
|
+
if (destructive) onCleanup(() => deactivate());
|
|
556
|
+
if (contents.native) {
|
|
557
|
+
nativeContents(element, contents, {
|
|
558
|
+
resume: activate,
|
|
559
|
+
suspend: deactivate,
|
|
560
|
+
project: (parent, nodes) => projectNative(parent, nodes, children, this.scope),
|
|
561
|
+
});
|
|
562
|
+
} else {
|
|
563
|
+
activate();
|
|
564
|
+
nativeContents(element, contents);
|
|
565
|
+
}
|
|
566
|
+
return element;
|
|
567
|
+
},
|
|
568
|
+
undefined,
|
|
569
|
+
false,
|
|
570
|
+
);
|
|
571
|
+
insert(parent, decorated, end, [element]);
|
|
572
|
+
}),
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** Compiler entry point. Keep the original element and contents lazy so hiding
|
|
578
|
+
* or replacing the element disposes its entire Solid owner. */
|
|
579
|
+
export function renderPart(
|
|
580
|
+
target: string,
|
|
581
|
+
fingerprint: string | null | undefined,
|
|
582
|
+
props: object,
|
|
583
|
+
row: (() => object) | undefined,
|
|
584
|
+
render: (contents: Contents) => JSX.Element,
|
|
585
|
+
original?: () => JSX.Element,
|
|
586
|
+
component?: boolean,
|
|
587
|
+
): JSX.Element | (() => JSX.Element) {
|
|
588
|
+
const registry = useContext(PartActions);
|
|
589
|
+
const read = registry?.read(target);
|
|
590
|
+
const plain = ((override = original) => override?.()) as Contents;
|
|
591
|
+
plain.spread = plainSpread;
|
|
592
|
+
plain.c = createComponent;
|
|
593
|
+
if (!read) return render(plain);
|
|
594
|
+
|
|
595
|
+
// The insert can move between wrapper owners. Keep the original in a
|
|
596
|
+
// separate owner so a sibling placement never remounts the element.
|
|
597
|
+
const owner = getOwner();
|
|
598
|
+
let disposeOriginal: (() => void) | undefined;
|
|
599
|
+
let disposeActions: (() => void) | undefined;
|
|
600
|
+
let element: JSX.Element;
|
|
601
|
+
let decorated: JSX.Element;
|
|
602
|
+
let previous: readonly LoadedPartAction[] | undefined;
|
|
603
|
+
onCleanup(() => {
|
|
604
|
+
disposeActions?.();
|
|
605
|
+
disposeOriginal?.();
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
let originalAttributes: readonly LoadedPartAction[] = [];
|
|
609
|
+
function renderOriginal(contents = plain) {
|
|
610
|
+
if (!disposeOriginal) {
|
|
611
|
+
element = runWithOwner(owner, () =>
|
|
612
|
+
createRoot((dispose) => {
|
|
613
|
+
disposeOriginal = dispose;
|
|
614
|
+
return render(contents);
|
|
615
|
+
}),
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
return element;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
return () => {
|
|
622
|
+
const installed = read();
|
|
623
|
+
if (installed === previous) return decorated;
|
|
624
|
+
previous = installed;
|
|
625
|
+
return untrack(() => {
|
|
626
|
+
disposeActions?.();
|
|
627
|
+
disposeActions = undefined;
|
|
628
|
+
let legacy: LoadedPartAction[] | undefined;
|
|
629
|
+
const actions = installed.filter((item) => {
|
|
630
|
+
if (!matches(registry!, target, fingerprint, item)) return false;
|
|
631
|
+
if (component !== undefined || !isAttributes(item)) return true;
|
|
632
|
+
(legacy ??= []).push(item);
|
|
633
|
+
return false;
|
|
634
|
+
});
|
|
635
|
+
const outside = actions.every(({ action }) => {
|
|
636
|
+
const kind = componentActionKind(action);
|
|
637
|
+
return kind === "before" || kind === "after" || kind === "attrs";
|
|
638
|
+
});
|
|
639
|
+
const attributes = component && outside ? orderedAttributes(actions) : [];
|
|
640
|
+
if (
|
|
641
|
+
!outside ||
|
|
642
|
+
attributes.length !== originalAttributes.length ||
|
|
643
|
+
attributes.some((item, index) => item !== originalAttributes[index])
|
|
644
|
+
) {
|
|
645
|
+
disposeOriginal?.();
|
|
646
|
+
disposeOriginal = undefined;
|
|
647
|
+
}
|
|
648
|
+
originalAttributes = attributes;
|
|
649
|
+
if (!actions.length && !legacy) return (decorated = renderOriginal());
|
|
650
|
+
const legacyActions = legacy;
|
|
651
|
+
const placementProps = row ? merge(props, { $row: row() }) : props;
|
|
652
|
+
return (decorated = runWithOwner(owner, () =>
|
|
653
|
+
createRoot((dispose) => {
|
|
654
|
+
disposeActions = dispose;
|
|
655
|
+
if (legacyActions)
|
|
656
|
+
createEffect(
|
|
657
|
+
() => {
|
|
658
|
+
for (const item of legacyActions)
|
|
659
|
+
if (item.enabled(placementProps)) warnLegacyAttributes(registry!, item);
|
|
660
|
+
},
|
|
661
|
+
() => {},
|
|
662
|
+
);
|
|
663
|
+
if (!actions.length) return renderOriginal();
|
|
664
|
+
return renderActions(
|
|
665
|
+
registry!,
|
|
666
|
+
actions,
|
|
667
|
+
placementProps,
|
|
668
|
+
outside
|
|
669
|
+
? (contents) => renderOriginal(component && attributes.length ? contents : plain)
|
|
670
|
+
: render,
|
|
671
|
+
original,
|
|
672
|
+
component,
|
|
673
|
+
);
|
|
674
|
+
}),
|
|
675
|
+
));
|
|
676
|
+
});
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function warnLegacyAttributes(registry: PartRegistry, item: LoadedPartAction) {
|
|
681
|
+
const target = item.action.target;
|
|
682
|
+
const owner = target.split(".")[0];
|
|
683
|
+
warnAttribute(
|
|
684
|
+
registry,
|
|
685
|
+
{ kind: "attr-legacy", plugin: item.plugin, target, otherPlugin: owner },
|
|
686
|
+
`Plugin "${item.plugin}" cannot change attributes on ${target}: owner plugin "${owner}" must be rebuilt`,
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
export function renderComponentPart(
|
|
691
|
+
target: string,
|
|
692
|
+
fingerprint: string | null | undefined,
|
|
693
|
+
props: object,
|
|
694
|
+
row: (() => object) | undefined,
|
|
695
|
+
render: (contents: Contents) => JSX.Element,
|
|
696
|
+
original?: () => JSX.Element,
|
|
697
|
+
) {
|
|
698
|
+
return renderPart(target, fingerprint, props, row, render, original, true);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
export function renderPlainPart(
|
|
702
|
+
target: string,
|
|
703
|
+
fingerprint: string | null | undefined,
|
|
704
|
+
props: object,
|
|
705
|
+
row: (() => object) | undefined,
|
|
706
|
+
render: (contents: Contents) => JSX.Element,
|
|
707
|
+
original?: () => JSX.Element,
|
|
708
|
+
) {
|
|
709
|
+
return renderPart(target, fingerprint, props, row, render, original, false);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function renderActions(
|
|
713
|
+
registry: PartRegistry,
|
|
714
|
+
actions: readonly LoadedPartAction[],
|
|
715
|
+
props: object,
|
|
716
|
+
render: (contents: Contents) => JSX.Element,
|
|
717
|
+
original?: () => JSX.Element,
|
|
718
|
+
component?: boolean,
|
|
719
|
+
): JSX.Element {
|
|
720
|
+
const at = (kind: ComponentActionKind) =>
|
|
721
|
+
actions.filter((item) => componentActionKind(item.action) === kind);
|
|
722
|
+
const enabled = (item: LoadedPartAction) => item.enabled(props);
|
|
723
|
+
const hides = at("hide");
|
|
724
|
+
const layers = at("layer").sort(installationOrder);
|
|
725
|
+
const attributes = orderedAttributes(actions);
|
|
726
|
+
|
|
727
|
+
function additions(items: LoadedPartAction[]) {
|
|
728
|
+
return (
|
|
729
|
+
<For each={items}>
|
|
730
|
+
{(item) => (
|
|
731
|
+
<Show when={enabled(item)}>
|
|
732
|
+
<Errored
|
|
733
|
+
fallback={(cause) => {
|
|
734
|
+
registry.failure(
|
|
735
|
+
item,
|
|
736
|
+
cause(),
|
|
737
|
+
`Component addition ${item.plugin}/${item.id} failed: ${String(cause())}`,
|
|
738
|
+
);
|
|
739
|
+
return null;
|
|
740
|
+
}}
|
|
741
|
+
>
|
|
742
|
+
{untrack(() => createComponent((item.action as ComponentAction<"before">).layer!, props))}
|
|
743
|
+
</Errored>
|
|
744
|
+
</Show>
|
|
745
|
+
)}
|
|
746
|
+
</For>
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
const leading = at("prepend");
|
|
751
|
+
const trailing = at("append");
|
|
752
|
+
const hasInside = leading.length > 0 || trailing.length > 0;
|
|
753
|
+
const ForwardedChildren = layers.length ? createContext<ChildOverride | null>(null) : undefined;
|
|
754
|
+
let nativeChildren: ChildOverride | undefined;
|
|
755
|
+
function authored(children = original ?? nativeChildren) {
|
|
756
|
+
if (!hasInside) return children?.();
|
|
757
|
+
return (
|
|
758
|
+
<>
|
|
759
|
+
{additions(leading)}
|
|
760
|
+
{children?.()}
|
|
761
|
+
{additions(trailing)}
|
|
762
|
+
</>
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function renderElement(read?: ReadOverride, readLayers?: AttributeLayers, forwarded = false) {
|
|
767
|
+
const contents: Contents = (children) => {
|
|
768
|
+
if (!read) return authored(children);
|
|
769
|
+
const current = createMemo(() => {
|
|
770
|
+
const override = read();
|
|
771
|
+
if (!override) return authored(children);
|
|
772
|
+
if (!children || !ForwardedChildren) return drawContents(override);
|
|
773
|
+
return (
|
|
774
|
+
<ForwardedChildren value={() => authored(children)}>{drawContents(override)}</ForwardedChildren>
|
|
775
|
+
);
|
|
776
|
+
});
|
|
777
|
+
return <>{current()}</>;
|
|
778
|
+
};
|
|
779
|
+
if (read)
|
|
780
|
+
contents.native = {
|
|
781
|
+
read,
|
|
782
|
+
capture: (children) => {
|
|
783
|
+
nativeChildren = children;
|
|
784
|
+
},
|
|
785
|
+
};
|
|
786
|
+
if (leading.length) contents.prepend = () => additions(leading);
|
|
787
|
+
if (trailing.length) contents.append = () => additions(trailing);
|
|
788
|
+
contents.spread = (forwarded: { readonly children?: JSX.Element }) =>
|
|
789
|
+
hasInside || read
|
|
790
|
+
? merge(forwarded, {
|
|
791
|
+
get children() {
|
|
792
|
+
return contents(() => forwarded.children);
|
|
793
|
+
},
|
|
794
|
+
})
|
|
795
|
+
: forwarded;
|
|
796
|
+
contents.c =
|
|
797
|
+
component && (attributes.length || forwarded)
|
|
798
|
+
? (draw, authored, ...debug) =>
|
|
799
|
+
createComponent(
|
|
800
|
+
draw,
|
|
801
|
+
merge(authored, () =>
|
|
802
|
+
attributeValues(registry, props, authored, attributes, readLayers?.()),
|
|
803
|
+
) as typeof authored,
|
|
804
|
+
...debug,
|
|
805
|
+
)
|
|
806
|
+
: createComponent;
|
|
807
|
+
const written = render(contents);
|
|
808
|
+
if (component === false && (attributes.length || readLayers)) {
|
|
809
|
+
const element = asElement(written);
|
|
810
|
+
if (element)
|
|
811
|
+
bindNativeAttributes(element, (owner, native) =>
|
|
812
|
+
attributeValues(registry, props, owner, attributes, readLayers?.(), native),
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
return written;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function report(kind: string, cause: LoadedPartAction, affected: LoadedPartAction, message: string) {
|
|
819
|
+
onCleanup(registry.interference(kind, cause, affected, message));
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
function hiddenBy(hide: LoadedPartAction) {
|
|
823
|
+
// Another plugin's hide loses nothing: it wanted the element gone too.
|
|
824
|
+
const loses = (item: LoadedPartAction) =>
|
|
825
|
+
item.plugin !== hide.plugin && componentActionKind(item.action) !== "hide";
|
|
826
|
+
const same = actions.filter((item) => loses(item) && enabled(item));
|
|
827
|
+
const inside = registry.inside(hide.action.target).filter(loses);
|
|
828
|
+
if (!same.length && !inside.length) return;
|
|
829
|
+
return () => {
|
|
830
|
+
for (const item of [...same, ...inside])
|
|
831
|
+
report(
|
|
832
|
+
"hide",
|
|
833
|
+
hide,
|
|
834
|
+
item,
|
|
835
|
+
`${hide.plugin} hides ${item.plugin}'s addition on ${screenElementName(item.action.target)}`,
|
|
836
|
+
);
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// `contentsOnly`: the layer drew Original with its own children and did not
|
|
841
|
+
// put the owner's back. The element and the layers below survive; what other
|
|
842
|
+
// plugins added inside it does not.
|
|
843
|
+
function covered(cover: LoadedPartAction, below: readonly LoadedPartAction[], contentsOnly = false) {
|
|
844
|
+
const other = (item: LoadedPartAction) => item.plugin !== cover.plugin;
|
|
845
|
+
const lower = contentsOnly ? [] : below.filter((item) => other(item) && enabled(item));
|
|
846
|
+
const children = [...leading, ...trailing].filter((item) => other(item) && enabled(item));
|
|
847
|
+
const inside = registry.inside(cover.action.target).filter(other);
|
|
848
|
+
const blocked = contentsOnly ? [] : attributes.filter((item) => other(item) && enabled(item));
|
|
849
|
+
if (!lower.length && !children.length && !inside.length && !blocked.length) return;
|
|
850
|
+
return () => {
|
|
851
|
+
const element = screenElementName(cover.action.target);
|
|
852
|
+
for (const item of lower)
|
|
853
|
+
report(
|
|
854
|
+
"cover-layer",
|
|
855
|
+
cover,
|
|
856
|
+
item,
|
|
857
|
+
`${cover.plugin}'s layer covers ${item.plugin}'s layer on ${element}`,
|
|
858
|
+
);
|
|
859
|
+
for (const item of children)
|
|
860
|
+
report(
|
|
861
|
+
"cover-inside",
|
|
862
|
+
cover,
|
|
863
|
+
item,
|
|
864
|
+
`${cover.plugin}'s layer hides ${item.plugin}'s inside addition on ${element}`,
|
|
865
|
+
);
|
|
866
|
+
for (const item of inside)
|
|
867
|
+
report(
|
|
868
|
+
"cover-inside",
|
|
869
|
+
cover,
|
|
870
|
+
item,
|
|
871
|
+
`${cover.plugin}'s layer on ${element} hides ${item.plugin}'s addition on ${screenElementName(item.action.target)}`,
|
|
872
|
+
);
|
|
873
|
+
for (const item of blocked)
|
|
874
|
+
warnAttribute(
|
|
875
|
+
registry,
|
|
876
|
+
{
|
|
877
|
+
kind: "attr-covered",
|
|
878
|
+
plugin: item.plugin,
|
|
879
|
+
target: item.action.target,
|
|
880
|
+
otherPlugin: cover.plugin,
|
|
881
|
+
},
|
|
882
|
+
`Plugin "${item.plugin}" cannot change attributes on ${item.action.target}: plugin "${cover.plugin}" covers the element`,
|
|
883
|
+
);
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
function shown() {
|
|
888
|
+
return (
|
|
889
|
+
<>
|
|
890
|
+
{additions(at("before"))}
|
|
891
|
+
{layers.length
|
|
892
|
+
? renderLayers(
|
|
893
|
+
registry,
|
|
894
|
+
layers,
|
|
895
|
+
props,
|
|
896
|
+
renderElement,
|
|
897
|
+
() => (useContext(ForwardedChildren!) ?? authored)(),
|
|
898
|
+
component,
|
|
899
|
+
attributes.length > 0,
|
|
900
|
+
covered,
|
|
901
|
+
)
|
|
902
|
+
: renderElement()}
|
|
903
|
+
{additions(at("after"))}
|
|
904
|
+
</>
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
if (!hides.length) return shown();
|
|
908
|
+
const hider = createMemo(() => hides.find(enabled));
|
|
909
|
+
trackInterference(() => {
|
|
910
|
+
const hide = hider();
|
|
911
|
+
return hide && hiddenBy(hide);
|
|
912
|
+
});
|
|
913
|
+
return <Show when={!hider()}>{shown()}</Show>;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// A branch can relinquish and reclaim Original in one render. Report only
|
|
917
|
+
// the settled result, and let the tracking scope retire its alerts on change.
|
|
918
|
+
function trackInterference(read: () => (() => void) | undefined) {
|
|
919
|
+
const owner = getOwner();
|
|
920
|
+
createEffect(
|
|
921
|
+
() => {
|
|
922
|
+
const report = read();
|
|
923
|
+
if (!report) return;
|
|
924
|
+
let active = true;
|
|
925
|
+
let release: (() => void) | undefined;
|
|
926
|
+
onCleanup(() => {
|
|
927
|
+
active = false;
|
|
928
|
+
release?.();
|
|
929
|
+
});
|
|
930
|
+
return () => {
|
|
931
|
+
if (!active) return;
|
|
932
|
+
release = runWithOwner(owner, () =>
|
|
933
|
+
createRoot((dispose) => {
|
|
934
|
+
report();
|
|
935
|
+
return dispose;
|
|
936
|
+
}),
|
|
937
|
+
);
|
|
938
|
+
};
|
|
939
|
+
},
|
|
940
|
+
(report) => {
|
|
941
|
+
if (report) onSettled(report);
|
|
942
|
+
},
|
|
943
|
+
);
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
const noOverride: ReadOverride = () => undefined;
|
|
947
|
+
|
|
948
|
+
function renderLayers(
|
|
949
|
+
registry: PartRegistry,
|
|
950
|
+
layers: LoadedPartAction[],
|
|
951
|
+
props: object,
|
|
952
|
+
render: (read: ReadOverride, attributes: AttributeLayers, forwarded?: boolean) => JSX.Element,
|
|
953
|
+
authored: () => JSX.Element,
|
|
954
|
+
component: boolean | undefined,
|
|
955
|
+
hasAttributes: boolean,
|
|
956
|
+
covered: (
|
|
957
|
+
item: LoadedPartAction,
|
|
958
|
+
below: readonly LoadedPartAction[],
|
|
959
|
+
contentsOnly?: boolean,
|
|
960
|
+
) => (() => void) | undefined,
|
|
961
|
+
) {
|
|
962
|
+
const owner = getOwner();
|
|
963
|
+
const pending = new Set<() => void>();
|
|
964
|
+
const [revision, revise] = createSignal(0, { ownedWrite: true });
|
|
965
|
+
let closed = false;
|
|
966
|
+
createEffect(revision, () => {
|
|
967
|
+
for (const release of pending) {
|
|
968
|
+
pending.delete(release);
|
|
969
|
+
release();
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
const disposers: (() => void)[] = [];
|
|
973
|
+
onCleanup(() => {
|
|
974
|
+
closed = true;
|
|
975
|
+
for (const dispose of disposers) dispose();
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
// A stage's owner lives outside the layer that requests it. Switching
|
|
979
|
+
// between a wrapper and its fallback moves the stage without remounting it.
|
|
980
|
+
function stage(draw: typeof render, mergeProps = false) {
|
|
981
|
+
let users = 0;
|
|
982
|
+
let dispose: (() => void) | undefined;
|
|
983
|
+
let output: JSX.Element;
|
|
984
|
+
let setInput: (read: ReadOverride, attributes: AttributeLayers) => void;
|
|
985
|
+
function release() {
|
|
986
|
+
if (users || !dispose) return;
|
|
987
|
+
dispose();
|
|
988
|
+
dispose = undefined;
|
|
989
|
+
output = undefined;
|
|
990
|
+
}
|
|
991
|
+
disposers.push(() => dispose?.());
|
|
992
|
+
return (input: ReadOverride, attributes: AttributeLayers): JSX.Element => {
|
|
993
|
+
users++;
|
|
994
|
+
onCleanup(() => {
|
|
995
|
+
if (--users || closed) return;
|
|
996
|
+
pending.add(release);
|
|
997
|
+
revise((n) => n + 1);
|
|
998
|
+
});
|
|
999
|
+
if (dispose) setInput(input, attributes);
|
|
1000
|
+
else {
|
|
1001
|
+
output = runWithOwner(owner, () =>
|
|
1002
|
+
createRoot((cleanup) => {
|
|
1003
|
+
dispose = cleanup;
|
|
1004
|
+
const [readInput, writeInput] = createSignal({ input, attributes }, { ownedWrite: true });
|
|
1005
|
+
setInput = (input, attributes) =>
|
|
1006
|
+
writeInput((current) =>
|
|
1007
|
+
current.input === input && current.attributes === attributes
|
|
1008
|
+
? current
|
|
1009
|
+
: { input, attributes },
|
|
1010
|
+
);
|
|
1011
|
+
const children = () => readInput().input();
|
|
1012
|
+
const layers = () => readInput().attributes();
|
|
1013
|
+
// A component with a layer on it takes merged props for as long as
|
|
1014
|
+
// the layer is installed, whether or not Original passes any right
|
|
1015
|
+
// now. Switching per flip would remount the owner's component each
|
|
1016
|
+
// time a layer's condition changed.
|
|
1017
|
+
return draw(createMemo(children), layers, mergeProps);
|
|
1018
|
+
}),
|
|
1019
|
+
);
|
|
1020
|
+
}
|
|
1021
|
+
return output;
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
const top = layers.reduceRight(
|
|
1026
|
+
(next, item, index) => {
|
|
1027
|
+
const below = layers.slice(index + 1);
|
|
1028
|
+
return stage((read, attributes) =>
|
|
1029
|
+
renderLayer(
|
|
1030
|
+
registry,
|
|
1031
|
+
item,
|
|
1032
|
+
props,
|
|
1033
|
+
authored,
|
|
1034
|
+
next,
|
|
1035
|
+
read,
|
|
1036
|
+
attributes,
|
|
1037
|
+
component !== undefined,
|
|
1038
|
+
(contentsOnly) => covered(item, below, contentsOnly),
|
|
1039
|
+
),
|
|
1040
|
+
);
|
|
1041
|
+
},
|
|
1042
|
+
stage(render, component === true && !hasAttributes),
|
|
1043
|
+
);
|
|
1044
|
+
return top(noOverride, noAttributeLayers);
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
function renderLayer(
|
|
1048
|
+
registry: PartRegistry,
|
|
1049
|
+
item: LoadedPartAction,
|
|
1050
|
+
props: object,
|
|
1051
|
+
authored: ChildOverride,
|
|
1052
|
+
next: (read: ReadOverride, attributes: AttributeLayers) => JSX.Element,
|
|
1053
|
+
read: ReadOverride,
|
|
1054
|
+
attributes: AttributeLayers,
|
|
1055
|
+
supportsAttributes: boolean,
|
|
1056
|
+
covered: (contentsOnly?: boolean) => (() => void) | undefined,
|
|
1057
|
+
) {
|
|
1058
|
+
const target = item.action.target;
|
|
1059
|
+
const draw = (item.action as ComponentAction<"layer">).layer!;
|
|
1060
|
+
function warn(cause: unknown) {
|
|
1061
|
+
registry.failure(item, cause, `Component layer ${item.plugin}/${item.id} failed: ${String(cause)}`);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
function activeLayer() {
|
|
1065
|
+
// Counts, not flags: when a layer switches between two uses of Original
|
|
1066
|
+
// (one branch of a Show to the other), the new use is created before the
|
|
1067
|
+
// old one is disposed, so for a moment both exist.
|
|
1068
|
+
let uses = 0;
|
|
1069
|
+
const count = () => createSignal(0, { ownedWrite: true });
|
|
1070
|
+
const [claims, setClaims] = count();
|
|
1071
|
+
const [supplied, setSupplied] = count();
|
|
1072
|
+
const [restored, setRestored] = count();
|
|
1073
|
+
const held = (set: (change: (n: number) => number) => void) => {
|
|
1074
|
+
set((n) => n + 1);
|
|
1075
|
+
onCleanup(() => set((n) => n - 1));
|
|
1076
|
+
};
|
|
1077
|
+
// The default stands for "not inside Original": a context needs one to be read there.
|
|
1078
|
+
const outside = { drawn: true };
|
|
1079
|
+
const Outlet = createContext(outside);
|
|
1080
|
+
const [failed, setFailed] = createSignal(false, { ownedWrite: true });
|
|
1081
|
+
const owner = getOwner();
|
|
1082
|
+
trackInterference(() => {
|
|
1083
|
+
if (failed()) return;
|
|
1084
|
+
if (!claims()) return covered();
|
|
1085
|
+
if (supplied() && !restored()) return covered(true);
|
|
1086
|
+
});
|
|
1087
|
+
|
|
1088
|
+
function drawOriginal(passed: { readonly children?: JSX.Element }) {
|
|
1089
|
+
held(setClaims);
|
|
1090
|
+
const hasAttributes = Object.keys(passed).some((key) => key !== "children");
|
|
1091
|
+
if (hasAttributes && !supportsAttributes) warnLegacyAttributes(registry, item);
|
|
1092
|
+
const forwarded: AttributeLayers =
|
|
1093
|
+
hasAttributes && supportsAttributes ? () => [...attributes(), { item, values: passed }] : attributes;
|
|
1094
|
+
if (!("children" in passed)) return next(read, forwarded);
|
|
1095
|
+
held(setSupplied);
|
|
1096
|
+
const use = { drawn: false };
|
|
1097
|
+
const children: LayerContents = {
|
|
1098
|
+
draw: () => <Outlet value={use}>{passed.children}</Outlet>,
|
|
1099
|
+
fail(cause) {
|
|
1100
|
+
// Children render at the bottom of the stack. Report their
|
|
1101
|
+
// failure back to the layer that supplied them.
|
|
1102
|
+
warn(cause);
|
|
1103
|
+
runWithOwner(owner, () =>
|
|
1104
|
+
onSettled(() => {
|
|
1105
|
+
setFailed(true);
|
|
1106
|
+
}),
|
|
1107
|
+
);
|
|
1108
|
+
},
|
|
1109
|
+
};
|
|
1110
|
+
return next(() => children, forwarded);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
const Original: OriginalElement<any> = Object.assign(
|
|
1114
|
+
(passed: { readonly children?: JSX.Element }) => {
|
|
1115
|
+
uses++;
|
|
1116
|
+
let mounted = true;
|
|
1117
|
+
onCleanup(() => {
|
|
1118
|
+
mounted = false;
|
|
1119
|
+
uses--;
|
|
1120
|
+
});
|
|
1121
|
+
// The element is one node, so the newest use holds it: a switch from
|
|
1122
|
+
// one branch to the other hands it over without remounting. If an
|
|
1123
|
+
// earlier use is still mounted once the update settles, the layer
|
|
1124
|
+
// really drew Original twice, and only the last one shows.
|
|
1125
|
+
onSettled(() => {
|
|
1126
|
+
if (mounted && uses > 1)
|
|
1127
|
+
registry.warn(
|
|
1128
|
+
["duplicate", item.plugin, target],
|
|
1129
|
+
`Component layer ${item.plugin}/${item.id} drew Original twice for ${target}`,
|
|
1130
|
+
);
|
|
1131
|
+
});
|
|
1132
|
+
return drawOriginal(passed);
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
Children() {
|
|
1136
|
+
const use = useContext(Outlet);
|
|
1137
|
+
if (use.drawn) {
|
|
1138
|
+
registry.warn(
|
|
1139
|
+
["children", item.plugin, target],
|
|
1140
|
+
`Component layer ${item.plugin}/${item.id} must draw Original.Children once inside Original for ${target}`,
|
|
1141
|
+
);
|
|
1142
|
+
return undefined;
|
|
1143
|
+
}
|
|
1144
|
+
use.drawn = true;
|
|
1145
|
+
onCleanup(() => {
|
|
1146
|
+
use.drawn = false;
|
|
1147
|
+
});
|
|
1148
|
+
held(setRestored);
|
|
1149
|
+
const previous = () => {
|
|
1150
|
+
const inherited = read();
|
|
1151
|
+
return inherited ? drawContents(inherited) : authored();
|
|
1152
|
+
};
|
|
1153
|
+
return <>{previous()}</>;
|
|
1154
|
+
},
|
|
1155
|
+
},
|
|
1156
|
+
);
|
|
1157
|
+
return (
|
|
1158
|
+
<Show when={!failed()} fallback={next(read, attributes)}>
|
|
1159
|
+
<Errored
|
|
1160
|
+
fallback={(cause) => {
|
|
1161
|
+
warn(cause());
|
|
1162
|
+
// The fallback draws the lower stage even though the layer did not.
|
|
1163
|
+
held(setClaims);
|
|
1164
|
+
return next(read, attributes);
|
|
1165
|
+
}}
|
|
1166
|
+
>
|
|
1167
|
+
{draw(props, Original)}
|
|
1168
|
+
</Errored>
|
|
1169
|
+
</Show>
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1172
|
+
return (
|
|
1173
|
+
<Show when={item.enabled(props)} fallback={next(read, attributes)}>
|
|
1174
|
+
{activeLayer()}
|
|
1175
|
+
</Show>
|
|
1176
|
+
);
|
|
1177
|
+
}
|