@opsydyn/astro-effect-ui 1.0.0-next.0 → 1.0.0-next.2
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/README.md +537 -0
- package/client.d.mts +9 -2
- package/client.mjs +146 -35
- package/component.d.mts +21 -3
- package/component.mjs +11 -3
- package/directive-interaction.d.mts +17 -2
- package/directive-interaction.mjs +118 -12
- package/index.d.mts +10 -1
- package/index.mjs +77 -12
- package/package.json +19 -5
- package/runtime.d.mts +49 -0
- package/runtime.mjs +19 -0
- package/server.d.mts +12 -1
- package/server.mjs +37 -14
- package/slots-ELUCsLv9.mjs +105 -0
package/client.mjs
CHANGED
|
@@ -1,47 +1,158 @@
|
|
|
1
1
|
import { IslandRendererError, asIslandComponent } from "./component.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { a as slotFiller, i as keepSlotNodes, o as slotsOf, r as fillSlotElements, s as withSlots } from "./slots-ELUCsLv9.mjs";
|
|
3
|
+
import { Cause, Effect, Exit, ManagedRuntime, Scope } from "effect";
|
|
3
4
|
import { BrowserHost, HtmlHydrate } from "@effect/ui/html";
|
|
4
5
|
//#region src/client.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Dispatched on an island whose component failed. Its detail holds the `error` and the
|
|
8
|
+
* `phase`: `activate` when the island never came alive, `run` when it failed later.
|
|
9
|
+
*/
|
|
10
|
+
const ERROR_EVENT = "effect-ui:error";
|
|
11
|
+
/**
|
|
12
|
+
* Starts the component in the island. `fresh` builds it from its props, as `client:only`
|
|
13
|
+
* does and as a kept island does when its props change; otherwise the server's markup
|
|
14
|
+
* is adopted.
|
|
15
|
+
*/
|
|
16
|
+
const activate = (host, island, component, props, slots, fresh) => {
|
|
17
|
+
const scope = Scope.makeUnsafe();
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
let closing;
|
|
20
|
+
let restore = () => {};
|
|
21
|
+
let live = false;
|
|
22
|
+
let failure;
|
|
23
|
+
const dispose = () => {
|
|
24
|
+
closing ??= Promise.resolve().then(() => {
|
|
14
25
|
controller.abort();
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
island.
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
return Effect.runPromise(Scope.close(scope, Exit.void));
|
|
27
|
+
});
|
|
28
|
+
return closing;
|
|
29
|
+
};
|
|
30
|
+
const report = (detail) => {
|
|
31
|
+
island.setAttribute("data-effect-ui-failed", "");
|
|
32
|
+
const view = island.ownerDocument.defaultView;
|
|
33
|
+
if (view !== null) island.dispatchEvent(new view.CustomEvent(ERROR_EVENT, {
|
|
34
|
+
bubbles: true,
|
|
35
|
+
composed: true,
|
|
36
|
+
detail
|
|
37
|
+
}));
|
|
38
|
+
return host.notify?.(detail);
|
|
39
|
+
};
|
|
40
|
+
const fail = async (detail) => {
|
|
41
|
+
if (failure === void 0 || host.fallback === void 0) {
|
|
42
|
+
await dispose().catch((cause) => console.error("Could not dispose Effect UI island.", cause));
|
|
43
|
+
restore();
|
|
44
|
+
}
|
|
45
|
+
await Promise.resolve(report(detail)).catch((cause) => console.error("Could not report the Effect UI island failure.", cause));
|
|
46
|
+
};
|
|
47
|
+
/** The component failed: Effect UI calls this through the fallback it is given. */
|
|
48
|
+
const componentFailed = (cause) => {
|
|
49
|
+
if (failure !== void 0) return;
|
|
50
|
+
const error = new IslandRendererError({
|
|
51
|
+
message: `The ${component.definition.name} island failed.`,
|
|
52
|
+
cause: Cause.squash(cause)
|
|
53
|
+
});
|
|
54
|
+
failure = error;
|
|
55
|
+
if (live) queueMicrotask(() => void fail({
|
|
56
|
+
error,
|
|
57
|
+
phase: "run"
|
|
58
|
+
}));
|
|
59
|
+
};
|
|
60
|
+
const fallback = (result, h) => {
|
|
61
|
+
componentFailed(result.cause);
|
|
62
|
+
return host.fallback === void 0 ? h.span.attr("hidden", "") : host.fallback(result, h);
|
|
63
|
+
};
|
|
64
|
+
const options = {
|
|
65
|
+
head: false,
|
|
66
|
+
fallback
|
|
67
|
+
};
|
|
68
|
+
const traced = Effect.gen(function* () {
|
|
69
|
+
const definition = asIslandComponent(component);
|
|
70
|
+
const filled = slotsOf(component, slots);
|
|
71
|
+
const root = island.querySelector(":scope > [data-effect-ui-root]");
|
|
72
|
+
const fill = filled === void 0 ? void 0 : slotFiller(filled, fresh ? /* @__PURE__ */ new Map() : keepSlotNodes(island));
|
|
73
|
+
const target = filled === void 0 ? definition : withSlots(component, props, filled, {
|
|
74
|
+
fill,
|
|
75
|
+
fallback
|
|
76
|
+
});
|
|
77
|
+
const given = filled === void 0 ? props : {};
|
|
78
|
+
if (fresh) {
|
|
79
|
+
const before = [...island.childNodes];
|
|
80
|
+
const container = root ?? island.ownerDocument.createElement("div");
|
|
81
|
+
island.querySelector(":scope > script[data-effect-ui-state]")?.remove();
|
|
82
|
+
if (root === null) {
|
|
27
83
|
island.replaceChildren(container);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
84
|
+
restore = () => island.replaceChildren(...before);
|
|
85
|
+
} else container.replaceChildren();
|
|
86
|
+
yield* BrowserHost.mountEffect(target, given, container, options);
|
|
87
|
+
} else {
|
|
32
88
|
const state = island.querySelector(":scope > script[data-effect-ui-state]")?.textContent;
|
|
33
|
-
if (!
|
|
89
|
+
if (!root || !state) return yield* new IslandRendererError({ message: "Missing Effect UI hydration markup or state." });
|
|
90
|
+
const markup = root.innerHTML;
|
|
91
|
+
restore = () => {
|
|
92
|
+
root.innerHTML = markup;
|
|
93
|
+
};
|
|
34
94
|
const hydrationState = JSON.parse(state);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
95
|
+
const hydrated = yield* HtmlHydrate.hydrateEffect(target, given, root, hydrationState, options);
|
|
96
|
+
if (fill !== void 0) fillSlotElements(island, fill);
|
|
97
|
+
yield* hydrated.adopted;
|
|
98
|
+
}
|
|
99
|
+
if (fill !== void 0) fillSlotElements(island, fill);
|
|
100
|
+
if (failure !== void 0) return yield* failure;
|
|
101
|
+
live = true;
|
|
102
|
+
}).pipe(Scope.provide(scope), Effect.withSpan("astro-effect-ui.activate", { attributes: { component: component.definition?.name ?? "unknown" } }));
|
|
103
|
+
return {
|
|
104
|
+
ready: host.run(traced, { signal: controller.signal }).catch(async (error) => {
|
|
38
105
|
if (controller.signal.aborted) return;
|
|
39
|
-
await
|
|
106
|
+
await fail({
|
|
107
|
+
error,
|
|
108
|
+
phase: "activate"
|
|
109
|
+
});
|
|
40
110
|
throw error;
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
|
|
111
|
+
}),
|
|
112
|
+
dispose
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/** The client renderer, with every island run in the app's runtime when it has one. */
|
|
116
|
+
function makeClientRenderer(runtime = {}) {
|
|
117
|
+
const { onError } = runtime;
|
|
118
|
+
const run = runtime.layer === void 0 ? Effect.runPromise : ManagedRuntime.make(runtime.layer).runPromise;
|
|
119
|
+
const host = {
|
|
120
|
+
run,
|
|
121
|
+
fallback: runtime.fallback,
|
|
122
|
+
notify: onError === void 0 ? void 0 : (failure) => run(onError(failure))
|
|
123
|
+
};
|
|
124
|
+
const entries = /* @__PURE__ */ new WeakMap();
|
|
125
|
+
return function renderer(island) {
|
|
126
|
+
return (component, props, slots, { client }) => {
|
|
127
|
+
const requested = island.getAttribute("props");
|
|
128
|
+
const current = entries.get(island);
|
|
129
|
+
if (current !== void 0 && current.props === requested) return current.ready;
|
|
130
|
+
let run;
|
|
131
|
+
if (current === void 0) {
|
|
132
|
+
const first = activate(host, island, component, props, slots, client === "only");
|
|
133
|
+
run = Promise.resolve(first);
|
|
134
|
+
const onUnmount = () => {
|
|
135
|
+
const last = entries.get(island);
|
|
136
|
+
entries.delete(island);
|
|
137
|
+
last?.run.then((started) => started.dispose()).catch((error) => console.error("Could not dispose Effect UI island.", error));
|
|
138
|
+
};
|
|
139
|
+
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
140
|
+
} else run = current.run.then(async (last) => {
|
|
141
|
+
await last.ready.catch(() => void 0);
|
|
142
|
+
await last.dispose();
|
|
143
|
+
island.removeAttribute("data-effect-ui-failed");
|
|
144
|
+
return activate(host, island, component, props, slots, true);
|
|
145
|
+
});
|
|
146
|
+
const entry = {
|
|
147
|
+
props: requested,
|
|
148
|
+
run,
|
|
149
|
+
ready: run.then((started) => started.ready)
|
|
150
|
+
};
|
|
151
|
+
entries.set(island, entry);
|
|
152
|
+
return entry.ready;
|
|
153
|
+
};
|
|
44
154
|
};
|
|
45
155
|
}
|
|
156
|
+
var client_default = makeClientRenderer();
|
|
46
157
|
//#endregion
|
|
47
|
-
export {
|
|
158
|
+
export { client_default as default, makeClientRenderer };
|
package/component.d.mts
CHANGED
|
@@ -9,11 +9,29 @@ declare const IslandRendererError_base: new <A extends Record<string, any> = {}>
|
|
|
9
9
|
} & Readonly<A>;
|
|
10
10
|
declare class IslandRendererError extends IslandRendererError_base<{
|
|
11
11
|
readonly message: string;
|
|
12
|
+
readonly cause?: unknown;
|
|
12
13
|
}> {}
|
|
13
14
|
type IslandProps = object;
|
|
14
15
|
type IslandComponent = Component.Members<string, IslandProps, unknown, never, never, Html.Host, Html.Elements>;
|
|
15
16
|
declare function asIslandComponent(value: Component.Any): IslandComponent;
|
|
16
|
-
/**
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* What a component still needs once the HTML host and `Provided` are taken away.
|
|
19
|
+
* Requirements typed as `any` are let through, since nothing can be read from them.
|
|
20
|
+
*/
|
|
21
|
+
type Missing<C, Provided> = Exclude<Component.Requirements<C>, Html.Host | Provided>;
|
|
22
|
+
/** Nothing for a component whose services are all provided, and a named error otherwise. */
|
|
23
|
+
type Accepted<C, Provided = never> = 0 extends 1 & Component.Requirements<C> ? unknown : [Missing<C, Provided>] extends [never] ? unknown : {
|
|
24
|
+
readonly 'astroComponent: no island runtime provides': Missing<C, Provided>;
|
|
25
|
+
};
|
|
26
|
+
/** What Astro sees: a component that takes these props. */
|
|
27
|
+
type AstroComponent<C> = (props: Component.Props<C>) => void;
|
|
28
|
+
/** The component as Astro sees it. Only the renderer calls the original view. */
|
|
29
|
+
declare function toAstro<C>(component: Component.Any): AstroComponent<C>;
|
|
30
|
+
/**
|
|
31
|
+
* Describes the props Astro passes while preserving the component the renderer detects.
|
|
32
|
+
* A component that needs a service is a type error here: declare the services with
|
|
33
|
+
* `defineIslands` and use the `astroComponent` it returns.
|
|
34
|
+
*/
|
|
35
|
+
declare function astroComponent<C extends Component.Any>(component: C & Accepted<C>): AstroComponent<C>;
|
|
18
36
|
//#endregion
|
|
19
|
-
export { IslandComponent, IslandProps, IslandRendererError, asIslandComponent, astroComponent };
|
|
37
|
+
export { Accepted, AstroComponent, IslandComponent, IslandProps, IslandRendererError, asIslandComponent, astroComponent, toAstro };
|
package/component.mjs
CHANGED
|
@@ -6,9 +6,17 @@ function asIslandComponent(value) {
|
|
|
6
6
|
if (!Component.isComponent(value)) throw new IslandRendererError({ message: "Expected an Effect UI component." });
|
|
7
7
|
return value;
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
10
|
-
function
|
|
9
|
+
/** The component as Astro sees it. Only the renderer calls the original view. */
|
|
10
|
+
function toAstro(component) {
|
|
11
11
|
return component;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Describes the props Astro passes while preserving the component the renderer detects.
|
|
15
|
+
* A component that needs a service is a type error here: declare the services with
|
|
16
|
+
* `defineIslands` and use the `astroComponent` it returns.
|
|
17
|
+
*/
|
|
18
|
+
function astroComponent(component) {
|
|
19
|
+
return toAstro(component);
|
|
20
|
+
}
|
|
13
21
|
//#endregion
|
|
14
|
-
export { IslandRendererError, asIslandComponent, astroComponent };
|
|
22
|
+
export { IslandRendererError, asIslandComponent, astroComponent, toAstro };
|
|
@@ -2,8 +2,23 @@ import { ClientDirective } from "astro";
|
|
|
2
2
|
|
|
3
3
|
//#region src/directive-interaction.d.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Hydrate on the reader's first press, focus, key or (with `hover`) mouse-over.
|
|
6
|
+
*
|
|
7
|
+
* Clicks made while the island loads are held and replayed once Effect UI has adopted
|
|
8
|
+
* the tree; Effect UI itself reads pending form edits from adopted browser controls.
|
|
9
|
+
*
|
|
10
|
+
* A mouse resting on the island is replayed too. The browser sent its
|
|
11
|
+
* `mouseenter` events before anything listened, and sends no more until the
|
|
12
|
+
* pointer crosses another element, so the adopted tree is told what the
|
|
13
|
+
* pointer is already over: one `mouseenter` for each element under it, the
|
|
14
|
+
* outermost first, as the browser sends them.
|
|
15
|
+
*
|
|
16
|
+
* With `preload`, the island's code is fetched when the browser is idle or the island
|
|
17
|
+
* nears the viewport, so the first interaction only has to hydrate.
|
|
18
|
+
*
|
|
19
|
+
* Code that fails to load is asked for again on the next interaction. A component that
|
|
20
|
+
* fails to hydrate is not retried. Either way the island keeps its server markup, and
|
|
21
|
+
* the failure is raised as `astro:hydration-error`, as Astro's own directives do.
|
|
7
22
|
*/
|
|
8
23
|
declare const interaction: ClientDirective;
|
|
9
24
|
//#endregion
|
|
@@ -1,28 +1,60 @@
|
|
|
1
1
|
//#region src/directive-interaction.ts
|
|
2
|
-
const
|
|
2
|
+
const optionsOf = (value) => value instanceof Object ? value : {};
|
|
3
3
|
const triggers = [
|
|
4
4
|
"pointerdown",
|
|
5
5
|
"focusin",
|
|
6
6
|
"keydown",
|
|
7
7
|
"pointerover"
|
|
8
8
|
];
|
|
9
|
+
/** How many times an island's code is asked for before the island is left as markup. */
|
|
10
|
+
const ATTEMPTS = 3;
|
|
9
11
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
+
* Islands this directive is waiting on. Astro runs a directive again whenever its island
|
|
13
|
+
* is put back in the document, as slot content is while its parent island hydrates.
|
|
14
|
+
*/
|
|
15
|
+
const armed = /* @__PURE__ */ new WeakSet();
|
|
16
|
+
/**
|
|
17
|
+
* Hydrate on the reader's first press, focus, key or (with `hover`) mouse-over.
|
|
18
|
+
*
|
|
19
|
+
* Clicks made while the island loads are held and replayed once Effect UI has adopted
|
|
20
|
+
* the tree; Effect UI itself reads pending form edits from adopted browser controls.
|
|
21
|
+
*
|
|
22
|
+
* A mouse resting on the island is replayed too. The browser sent its
|
|
23
|
+
* `mouseenter` events before anything listened, and sends no more until the
|
|
24
|
+
* pointer crosses another element, so the adopted tree is told what the
|
|
25
|
+
* pointer is already over: one `mouseenter` for each element under it, the
|
|
26
|
+
* outermost first, as the browser sends them.
|
|
27
|
+
*
|
|
28
|
+
* With `preload`, the island's code is fetched when the browser is idle or the island
|
|
29
|
+
* nears the viewport, so the first interaction only has to hydrate.
|
|
30
|
+
*
|
|
31
|
+
* Code that fails to load is asked for again on the next interaction. A component that
|
|
32
|
+
* fails to hydrate is not retried. Either way the island keeps its server markup, and
|
|
33
|
+
* the failure is raised as `astro:hydration-error`, as Astro's own directives do.
|
|
12
34
|
*/
|
|
13
35
|
const interaction = (load, options, island) => {
|
|
14
36
|
const root = island.querySelector(":scope > [data-effect-ui-root]");
|
|
15
37
|
const window = island.ownerDocument.defaultView;
|
|
16
|
-
const hover =
|
|
38
|
+
const { hover = false, preload } = optionsOf(options.value);
|
|
17
39
|
if (!root || !window) {
|
|
18
40
|
load().then((hydrate) => hydrate());
|
|
19
41
|
return;
|
|
20
42
|
}
|
|
43
|
+
if (armed.has(island) || !island.hasAttribute("ssr")) return;
|
|
44
|
+
armed.add(island);
|
|
21
45
|
const clicks = [];
|
|
22
46
|
let started = false;
|
|
23
47
|
let removed = false;
|
|
48
|
+
let attempts = 0;
|
|
49
|
+
let loading;
|
|
50
|
+
let cancelPreload = () => {};
|
|
51
|
+
let rest;
|
|
24
52
|
const stop = () => {
|
|
53
|
+
armed.delete(island);
|
|
54
|
+
cancelPreload();
|
|
25
55
|
for (const type of triggers) island.removeEventListener(type, start, true);
|
|
56
|
+
island.removeEventListener("pointerover", trackRest, true);
|
|
57
|
+
island.removeEventListener("pointerout", trackRest, true);
|
|
26
58
|
island.removeEventListener("click", captureClick, true);
|
|
27
59
|
island.removeEventListener("astro:unmount", onUnmount);
|
|
28
60
|
};
|
|
@@ -30,22 +62,73 @@ const interaction = (load, options, island) => {
|
|
|
30
62
|
removed = true;
|
|
31
63
|
stop();
|
|
32
64
|
};
|
|
65
|
+
const gone = () => removed || !island.isConnected;
|
|
66
|
+
const report = (detail) => {
|
|
67
|
+
const event = new window.CustomEvent("astro:hydration-error", {
|
|
68
|
+
cancelable: true,
|
|
69
|
+
bubbles: true,
|
|
70
|
+
composed: true,
|
|
71
|
+
detail: {
|
|
72
|
+
...detail,
|
|
73
|
+
componentUrl: island.getAttribute("component-url")
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (island.dispatchEvent(event)) console.error("Could not activate the Effect UI island.", detail.error);
|
|
77
|
+
};
|
|
78
|
+
/** The island's code, fetched once until a fetch fails. */
|
|
79
|
+
const fetchCode = () => loading ??= load();
|
|
33
80
|
const targetInsideRoot = (event) => {
|
|
34
81
|
const target = event.target;
|
|
35
82
|
return target instanceof window.Element && root.contains(target) && target.closest("astro-island") === island ? target : null;
|
|
36
83
|
};
|
|
84
|
+
const trackRest = (event) => {
|
|
85
|
+
if (!(event instanceof window.MouseEvent) || !("pointerType" in event) || event.pointerType !== "mouse") return;
|
|
86
|
+
const target = event.type === "pointerover" ? targetInsideRoot(event) : null;
|
|
87
|
+
rest = target === null ? void 0 : {
|
|
88
|
+
target,
|
|
89
|
+
x: event.clientX,
|
|
90
|
+
y: event.clientY
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const replayHover = () => {
|
|
94
|
+
if (rest === void 0 || !root.contains(rest.target)) return;
|
|
95
|
+
const entered = [];
|
|
96
|
+
for (let element = rest.target; element !== null && element !== root; element = element.parentElement) entered.unshift(element);
|
|
97
|
+
for (const element of entered) element.dispatchEvent(new window.MouseEvent("mouseenter", {
|
|
98
|
+
clientX: rest.x,
|
|
99
|
+
clientY: rest.y
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
102
|
+
const replayClicks = () => {
|
|
103
|
+
for (const { target, event } of clicks) if (root.contains(target) && target.closest("astro-island") === island && target.closest(":disabled") === null) target.dispatchEvent(new window.MouseEvent("click", event));
|
|
104
|
+
};
|
|
105
|
+
/** The code did not arrive: wait for another interaction, up to the limit. */
|
|
106
|
+
const retryLater = () => {
|
|
107
|
+
loading = void 0;
|
|
108
|
+
clicks.length = 0;
|
|
109
|
+
started = false;
|
|
110
|
+
if (attempts >= ATTEMPTS) stop();
|
|
111
|
+
};
|
|
112
|
+
const hydrated = () => {
|
|
113
|
+
if (gone()) return;
|
|
114
|
+
if (island.hasAttribute("ssr")) return retryLater();
|
|
115
|
+
stop();
|
|
116
|
+
replayHover();
|
|
117
|
+
replayClicks();
|
|
118
|
+
};
|
|
119
|
+
const hydrateNow = () => fetchCode().then((hydrate) => gone() ? void 0 : Promise.resolve().then(hydrate).then(hydrated, (error) => {
|
|
120
|
+
stop();
|
|
121
|
+
report({ error });
|
|
122
|
+
}), (error) => {
|
|
123
|
+
if (!gone()) report({ error });
|
|
124
|
+
retryLater();
|
|
125
|
+
}).catch((error) => console.error("Could not activate the Effect UI island.", error));
|
|
37
126
|
const start = (event) => {
|
|
38
127
|
if (started || !targetInsideRoot(event)) return;
|
|
39
128
|
if (event.type === "pointerover" && (!hover || !("pointerType" in event) || event.pointerType !== "mouse")) return;
|
|
40
129
|
started = true;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (removed || !island.isConnected) return;
|
|
44
|
-
for (const { target, event } of clicks) if (root.contains(target) && target.closest("astro-island") === island && target.closest(":disabled") === null) target.dispatchEvent(new window.MouseEvent("click", event));
|
|
45
|
-
}).catch((error) => {
|
|
46
|
-
stop();
|
|
47
|
-
console.error("Could not activate the Effect UI island.", error);
|
|
48
|
-
});
|
|
130
|
+
attempts += 1;
|
|
131
|
+
hydrateNow();
|
|
49
132
|
};
|
|
50
133
|
const captureClick = (event) => {
|
|
51
134
|
const target = targetInsideRoot(event);
|
|
@@ -59,9 +142,32 @@ const interaction = (load, options, island) => {
|
|
|
59
142
|
event
|
|
60
143
|
});
|
|
61
144
|
};
|
|
145
|
+
const preloadCode = () => {
|
|
146
|
+
cancelPreload = () => {};
|
|
147
|
+
if (!started) fetchCode().catch(() => loading = void 0);
|
|
148
|
+
};
|
|
149
|
+
island.addEventListener("pointerover", trackRest, true);
|
|
150
|
+
island.addEventListener("pointerout", trackRest, true);
|
|
62
151
|
for (const type of triggers) island.addEventListener(type, start, true);
|
|
63
152
|
island.addEventListener("click", captureClick, true);
|
|
64
153
|
island.addEventListener("astro:unmount", onUnmount, { once: true });
|
|
154
|
+
if (preload === "idle") if ("requestIdleCallback" in window) {
|
|
155
|
+
const id = window.requestIdleCallback(preloadCode);
|
|
156
|
+
cancelPreload = () => window.cancelIdleCallback(id);
|
|
157
|
+
} else {
|
|
158
|
+
const id = setTimeout(preloadCode, 200);
|
|
159
|
+
cancelPreload = () => clearTimeout(id);
|
|
160
|
+
}
|
|
161
|
+
else if (preload === "visible" && "IntersectionObserver" in window) {
|
|
162
|
+
const observer = new window.IntersectionObserver((entries) => {
|
|
163
|
+
if (entries.some((entry) => entry.isIntersecting)) {
|
|
164
|
+
observer.disconnect();
|
|
165
|
+
preloadCode();
|
|
166
|
+
}
|
|
167
|
+
}, { rootMargin: "200px" });
|
|
168
|
+
for (const child of island.children) observer.observe(child);
|
|
169
|
+
cancelPreload = () => observer.disconnect();
|
|
170
|
+
}
|
|
65
171
|
};
|
|
66
172
|
//#endregion
|
|
67
173
|
export { interaction as default };
|
package/index.d.mts
CHANGED
|
@@ -3,9 +3,18 @@ import { AstroIntegration } from "astro";
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
type EffectUiOptions = {
|
|
5
5
|
readonly interaction?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Modules that describe what islands share, as paths from the project root. Each
|
|
8
|
+
* default-exports what `serverRuntime` or `clientRuntime` of `defineIslands` returns.
|
|
9
|
+
*/
|
|
10
|
+
readonly runtime?: {
|
|
11
|
+
readonly server?: string;
|
|
12
|
+
readonly client?: string;
|
|
13
|
+
};
|
|
6
14
|
};
|
|
7
15
|
declare function effectUi({
|
|
8
|
-
interaction
|
|
16
|
+
interaction,
|
|
17
|
+
runtime
|
|
9
18
|
}?: EffectUiOptions): AstroIntegration;
|
|
10
19
|
//#endregion
|
|
11
20
|
export { EffectUiOptions, effectUi as default };
|
package/index.mjs
CHANGED
|
@@ -1,23 +1,88 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
1
2
|
import { fileURLToPath } from "node:url";
|
|
2
3
|
//#region src/index.ts
|
|
3
4
|
const extension = import.meta.url.endsWith(".ts") ? "ts" : "mjs";
|
|
4
5
|
function entrypoint(name) {
|
|
5
6
|
return fileURLToPath(new URL(`./${name}.${extension}`, import.meta.url));
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
+
const factories = {
|
|
9
|
+
client: "makeClientRenderer",
|
|
10
|
+
server: "makeServerRenderer"
|
|
11
|
+
};
|
|
12
|
+
/** A path as an import specifier, whatever the platform's separator. */
|
|
13
|
+
const specifier = (path) => JSON.stringify(path.replaceAll("\\", "/"));
|
|
14
|
+
/**
|
|
15
|
+
* The renderer entry for one side when the app has a runtime module for it: this
|
|
16
|
+
* package's renderer, made with that module's default export.
|
|
17
|
+
*/
|
|
18
|
+
function entryWith(side, runtime) {
|
|
19
|
+
const factory = factories[side];
|
|
20
|
+
return `import { ${factory} } from ${specifier(entrypoint(side))};
|
|
21
|
+
import runtime from ${specifier(runtime)};
|
|
22
|
+
|
|
23
|
+
export default ${factory}(runtime);
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The `client:interaction` attribute, for Astro's type check. Astro 5, 6 and 7 all read
|
|
28
|
+
* custom directives from `Astro.ClientDirectives`.
|
|
29
|
+
*/
|
|
30
|
+
const directiveTypes = `declare namespace Astro {
|
|
31
|
+
interface ClientDirectives {
|
|
32
|
+
/**
|
|
33
|
+
* Hydrate when a reader first presses, focuses or types in the island and, with
|
|
34
|
+
* \`hover\`, when a mouse pointer enters it. \`preload\` fetches the island's code
|
|
35
|
+
* ahead of that, when the browser is idle or the island nears the viewport.
|
|
36
|
+
*/
|
|
37
|
+
'client:interaction'?:
|
|
38
|
+
| boolean
|
|
39
|
+
| { readonly hover?: boolean; readonly preload?: 'idle' | 'visible' };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
/**
|
|
44
|
+
* Effect and Effect UI are prebundled up front, so the dev server does not find them on
|
|
45
|
+
* the first island and reload the page, and are resolved once, so the renderer and the
|
|
46
|
+
* app's components share one copy of each.
|
|
47
|
+
*/
|
|
48
|
+
const vite = {
|
|
49
|
+
optimizeDeps: { include: [
|
|
50
|
+
"effect",
|
|
51
|
+
"@effect/ui",
|
|
52
|
+
"@effect/ui/html"
|
|
53
|
+
] },
|
|
54
|
+
resolve: { dedupe: ["effect", "@effect/ui"] }
|
|
55
|
+
};
|
|
56
|
+
function effectUi({ interaction = true, runtime = {} } = {}) {
|
|
8
57
|
return {
|
|
9
58
|
name: "astro-effect-ui",
|
|
10
|
-
hooks: {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
59
|
+
hooks: {
|
|
60
|
+
"astro:config:done": ({ injectTypes }) => {
|
|
61
|
+
if (interaction) injectTypes({
|
|
62
|
+
filename: "client-directives.d.ts",
|
|
63
|
+
content: directiveTypes
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
"astro:config:setup": ({ addClientDirective, addRenderer, config, createCodegenDir, updateConfig }) => {
|
|
67
|
+
updateConfig({ vite });
|
|
68
|
+
if (interaction) addClientDirective({
|
|
69
|
+
name: "interaction",
|
|
70
|
+
entrypoint: entrypoint("directive-interaction")
|
|
71
|
+
});
|
|
72
|
+
const entryFor = (side) => {
|
|
73
|
+
const module = runtime[side];
|
|
74
|
+
if (module === void 0) return entrypoint(side);
|
|
75
|
+
const file = new URL(`${side}-entry.mjs`, createCodegenDir());
|
|
76
|
+
writeFileSync(file, entryWith(side, fileURLToPath(new URL(module, config.root))));
|
|
77
|
+
return fileURLToPath(file);
|
|
78
|
+
};
|
|
79
|
+
addRenderer({
|
|
80
|
+
name: "astro-effect-ui",
|
|
81
|
+
serverEntrypoint: entryFor("server"),
|
|
82
|
+
clientEntrypoint: entryFor("client")
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
21
86
|
};
|
|
22
87
|
}
|
|
23
88
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opsydyn/astro-effect-ui",
|
|
3
|
-
"version": "1.0.0-next.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0-next.2",
|
|
4
|
+
"description": "Render Effect UI components in Astro: on the server, as islands, or both.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"astro",
|
|
7
|
+
"astro-integration",
|
|
8
|
+
"astro-renderer",
|
|
9
|
+
"effect",
|
|
10
|
+
"effect-ui",
|
|
11
|
+
"islands"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/opsydyn/fold-kit-experiments/tree/main/packages/astro-effect-ui#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/opsydyn/fold-kit-experiments/issues"
|
|
16
|
+
},
|
|
5
17
|
"license": "MIT",
|
|
6
18
|
"type": "module",
|
|
7
19
|
"exports": {
|
|
@@ -12,12 +24,14 @@
|
|
|
12
24
|
"./component": {
|
|
13
25
|
"types": "./component.d.mts",
|
|
14
26
|
"import": "./component.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./runtime": {
|
|
29
|
+
"types": "./runtime.d.mts",
|
|
30
|
+
"import": "./runtime.mjs"
|
|
15
31
|
}
|
|
16
32
|
},
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"@effect/ui": "https://pkg.pr.new/Effect-TS/ui/@effect/ui@77dcd92"
|
|
19
|
-
},
|
|
20
33
|
"peerDependencies": {
|
|
34
|
+
"@effect/ui": "*",
|
|
21
35
|
"astro": ">=5.0.0 <8",
|
|
22
36
|
"effect": "~4.1.0"
|
|
23
37
|
},
|
package/runtime.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Accepted, AstroComponent } from "./component.mjs";
|
|
2
|
+
import { Component } from "@effect/ui";
|
|
3
|
+
import { Duration, Effect, Layer } from "effect";
|
|
4
|
+
import { BrowserHost, Html } from "@effect/ui/html";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime.d.ts
|
|
7
|
+
/** An island whose component failed: as it started (`activate`) or later (`run`). */
|
|
8
|
+
type IslandFailure = Readonly<{
|
|
9
|
+
error: unknown;
|
|
10
|
+
phase: 'activate' | 'run';
|
|
11
|
+
}>;
|
|
12
|
+
/** What Effect UI shows in a root's place when the root fails, as `BrowserHost.mount` takes it. */
|
|
13
|
+
type IslandFallback = NonNullable<BrowserHost.MountOptions<Html.Elements>['fallback']>;
|
|
14
|
+
/** The Layer of a runtime: required as soon as the app's islands declare a service. */
|
|
15
|
+
type Wiring<Services> = [Services] extends [never] ? {
|
|
16
|
+
readonly layer?: Layer.Layer<never, unknown>;
|
|
17
|
+
} : {
|
|
18
|
+
readonly layer: Layer.Layer<Services, unknown>;
|
|
19
|
+
};
|
|
20
|
+
/** What every server render shares. */
|
|
21
|
+
type ServerRuntime<Services = never> = Wiring<Services> & Readonly<{
|
|
22
|
+
/** How long a render waits for Layers and queries. Effect UI's default is 500 ms. */budget?: Duration.Input;
|
|
23
|
+
}>;
|
|
24
|
+
/** What every island on a page shares. */
|
|
25
|
+
type ClientRuntime<Services = never> = Wiring<Services> & Readonly<{
|
|
26
|
+
/** Shown in place of an island that failed. Without it the island keeps its earlier markup. */fallback?: IslandFallback; /** Run in the page's runtime when an island fails, as well as the `effect-ui:error` event. */
|
|
27
|
+
onError?: (failure: IslandFailure) => Effect.Effect<void, never, Services>;
|
|
28
|
+
}>;
|
|
29
|
+
/** A server runtime as the renderer receives it: checked where it was defined. */
|
|
30
|
+
type AnyServerRuntime = ServerRuntime;
|
|
31
|
+
/** A client runtime as the renderer receives it, whatever services its hook uses. */
|
|
32
|
+
type AnyClientRuntime = Omit<ClientRuntime, 'onError'> & Readonly<{
|
|
33
|
+
onError?: (failure: IslandFailure) => Effect.Effect<void, never, unknown>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Names the services an app's islands may use, once. Both runtimes must then provide
|
|
37
|
+
* them, and `astroComponent` accepts a component only if they cover what it needs.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* export const islands = defineIslands<DeleteTodo | Flags>();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function defineIslands<Services = never>(): {
|
|
44
|
+
astroComponent: <C extends Component.Any>(component: C & Accepted<C, Services>) => AstroComponent<C>;
|
|
45
|
+
serverRuntime: (runtime: ServerRuntime<Services>) => ServerRuntime<Services>;
|
|
46
|
+
clientRuntime: (runtime: ClientRuntime<Services>) => ClientRuntime<Services>;
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { AnyClientRuntime, AnyServerRuntime, ClientRuntime, IslandFailure, IslandFallback, ServerRuntime, defineIslands };
|