@hamur/adapter-preact 0.0.0-stage → 0.0.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 +22 -0
- package/README.md +35 -2
- package/dist/chunks/browser-index-89d71ade.js +118 -0
- package/dist/chunks/browser-index-ezte15pp.js +67 -0
- package/dist/chunks/browser-index-hsy7tnbg.js +21 -0
- package/dist/chunks/browser-index-k3m2axbw.js +274 -0
- package/dist/src/adapter.js +456 -0
- package/dist/src/error-page.js +391 -0
- package/dist/src/index.js +1506 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +96 -0
- package/dist/src/runtime/island.js +33 -0
- package/dist/src/runtime/server.js +95 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +13 -0
- package/dist/types/src/client-component.d.ts +11 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/context.d.ts +81 -0
- package/dist/types/src/error-page.d.ts +1 -0
- package/dist/types/src/head.d.ts +5 -0
- package/dist/types/src/index.d.ts +429 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/island.d.ts +19 -0
- package/dist/types/src/jsx-locator.d.ts +6 -0
- package/dist/types/src/keepalive.d.ts +26 -0
- package/dist/types/src/lazy-hydration.d.ts +12 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +12 -0
- package/dist/types/src/plugin.d.ts +36 -0
- package/dist/types/src/route-boundary.d.ts +41 -0
- package/dist/types/src/runtime/client.d.ts +10 -0
- package/dist/types/src/runtime/hmr.d.ts +31 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +11 -0
- package/dist/types/src/server-island.d.ts +10 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +50 -4
- package/src/activation.ts +51 -0
- package/src/adapter.ts +35 -0
- package/src/async-data.ts +65 -0
- package/src/client-component.ts +23 -0
- package/src/client-fallback.ts +2 -0
- package/src/context.ts +372 -0
- package/src/env.d.ts +3 -0
- package/src/error-page.ts +1 -0
- package/src/head.ts +34 -0
- package/src/index.ts +1868 -0
- package/src/island-html.ts +58 -0
- package/src/island.ts +56 -0
- package/src/jsx-locator.ts +199 -0
- package/src/keepalive.ts +100 -0
- package/src/lazy-hydration.ts +101 -0
- package/src/markdown-page.ts +18 -0
- package/src/markdown.ts +58 -0
- package/src/plugin.ts +231 -0
- package/src/route-boundary.ts +51 -0
- package/src/runtime/client.ts +85 -0
- package/src/runtime/hmr.ts +141 -0
- package/src/runtime/hydration-dev.ts +8 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +37 -0
- package/src/runtime/server.ts +102 -0
- package/src/server-island.ts +35 -0
- package/src/svg.ts +27 -0
- package/src/webmcp.d.ts +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 productdevbook
|
|
4
|
+
Copyright (c) 2019 Material-UI SAS
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hamur/adapter-preact
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Preact 11 adapter for [Hamur](https://github.com/productdevbook/hamur), a full-stack web framework for Bun. It renders pages and layouts with Preact (server rendering returns a string) and gives you `useData`, `useHead`, `useState` and the other hooks. Routing, loaders, head, state and the server come from `hamur`.
|
|
4
|
+
|
|
5
|
+
One app uses one library. Set `framework: "preact"` in `hamur.config.ts`, or install only this adapter and Hamur detects it.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add hamur @hamur/adapter-preact preact preact-render-to-string
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or start from a template: `bunx hamur init my-app --framework preact`.
|
|
14
|
+
|
|
15
|
+
## Example
|
|
16
|
+
|
|
17
|
+
`app/pages/post.tsx`:
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { useData, useHead, useState } from "@hamur/adapter-preact"
|
|
21
|
+
|
|
22
|
+
export default function Post() {
|
|
23
|
+
const { title } = useData<{ title: string }>()
|
|
24
|
+
const [likes, setLikes] = useState("likes", 0)
|
|
25
|
+
useHead({ title })
|
|
26
|
+
return (
|
|
27
|
+
<button onClick={() => setLikes(likes + 1)}>
|
|
28
|
+
{title}: {likes}
|
|
29
|
+
</button>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Docs
|
|
35
|
+
|
|
36
|
+
[Documentation](https://docs.productdevbook.com/) and the [adapters page](https://docs.productdevbook.com/adapters). MIT license.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NeverHydratedContext,
|
|
3
|
+
AppContextContext
|
|
4
|
+
} from "./browser-index-k3m2axbw.js";
|
|
5
|
+
|
|
6
|
+
// packages/adapter-preact/src/lazy-hydration.ts
|
|
7
|
+
import { createElement } from "preact";
|
|
8
|
+
import { Suspense } from "preact/compat";
|
|
9
|
+
import { useContext, useEffect, useRef, useState } from "preact/hooks";
|
|
10
|
+
import { HYDRATE_ELEMENT, hydrateOnAttribute, markWaiting, replayInteraction, whenTriggered } from "hamur/adapter";
|
|
11
|
+
var isServer = typeof window === "undefined";
|
|
12
|
+
var served = (element) => element.firstElementChild !== null || (element.textContent ?? "") !== "";
|
|
13
|
+
var pending = new Set;
|
|
14
|
+
function track(promise) {
|
|
15
|
+
const loading = { promise: Promise.resolve() };
|
|
16
|
+
loading.promise = promise.then((loaded) => void (loading.module = loaded), (error) => void (loading.failed = { error }));
|
|
17
|
+
return loading;
|
|
18
|
+
}
|
|
19
|
+
function lazyModules() {
|
|
20
|
+
return pending.size ? Promise.all(pending) : undefined;
|
|
21
|
+
}
|
|
22
|
+
function Hydrated({ loading, props, done }) {
|
|
23
|
+
if (loading.failed)
|
|
24
|
+
throw loading.failed.error;
|
|
25
|
+
if (!loading.module)
|
|
26
|
+
throw loading.promise;
|
|
27
|
+
useEffect(done, []);
|
|
28
|
+
return createElement(loading.module.default, props);
|
|
29
|
+
}
|
|
30
|
+
function defineLazyHydrationComponent2(on, loader, options = {}) {
|
|
31
|
+
const attribute = hydrateOnAttribute(on);
|
|
32
|
+
let server;
|
|
33
|
+
if (isServer) {
|
|
34
|
+
server = track(loader());
|
|
35
|
+
pending.add(server.promise);
|
|
36
|
+
server.promise.then(() => pending.delete(server.promise));
|
|
37
|
+
}
|
|
38
|
+
return function LazyHydration({ onHydrated, ...props }) {
|
|
39
|
+
const app = useContext(AppContextContext);
|
|
40
|
+
const wrapper = useRef(null);
|
|
41
|
+
const hydrated = useRef(onHydrated);
|
|
42
|
+
hydrated.current = onHydrated;
|
|
43
|
+
const [gate] = useState(() => {
|
|
44
|
+
if (server)
|
|
45
|
+
return { loading: server };
|
|
46
|
+
let release;
|
|
47
|
+
const triggered = new Promise((resolve) => release = resolve);
|
|
48
|
+
return { loading: track(triggered.then(loader)), release };
|
|
49
|
+
});
|
|
50
|
+
const [done] = useState(() => {
|
|
51
|
+
let called = false;
|
|
52
|
+
return () => {
|
|
53
|
+
if (called)
|
|
54
|
+
return;
|
|
55
|
+
called = true;
|
|
56
|
+
hydrated.current?.();
|
|
57
|
+
if (wrapper.current)
|
|
58
|
+
replayInteraction(wrapper.current);
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const element = wrapper.current;
|
|
63
|
+
if (!gate.release || !element)
|
|
64
|
+
return;
|
|
65
|
+
if (!served(element))
|
|
66
|
+
return void gate.release();
|
|
67
|
+
const controller = new AbortController;
|
|
68
|
+
whenTriggered(element, on, { ...options, signal: controller.signal }).then(gate.release);
|
|
69
|
+
return () => controller.abort();
|
|
70
|
+
}, [gate]);
|
|
71
|
+
const waiting = isServer && !gate.loading.module && !gate.loading.failed;
|
|
72
|
+
if (waiting && app)
|
|
73
|
+
markWaiting(app.asyncData);
|
|
74
|
+
const inner = waiting ? null : createElement(Suspense, { fallback: null }, createElement(Hydrated, { loading: gate.loading, props, done }));
|
|
75
|
+
return createElement(HYDRATE_ELEMENT, isServer ? { on: attribute } : { ref: wrapper }, on === "never" ? createElement(NeverHydratedContext.Provider, { value: true }, inner) : inner);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// packages/adapter-preact/src/island-html.ts
|
|
80
|
+
import { markWaiting as markWaiting2 } from "hamur/adapter";
|
|
81
|
+
var outcomes = new WeakMap;
|
|
82
|
+
var current;
|
|
83
|
+
function settledIsland(promise, pending = current) {
|
|
84
|
+
const outcome = outcomes.get(promise);
|
|
85
|
+
if (outcome) {
|
|
86
|
+
if ("error" in outcome)
|
|
87
|
+
throw outcome.error;
|
|
88
|
+
return outcome.value;
|
|
89
|
+
}
|
|
90
|
+
pending?.push(promise.then((value) => void outcomes.set(promise, { value }), (error) => void outcomes.set(promise, { error })));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
function collectingIslands(pending, render) {
|
|
94
|
+
const outer = current;
|
|
95
|
+
current = pending;
|
|
96
|
+
try {
|
|
97
|
+
return render();
|
|
98
|
+
} finally {
|
|
99
|
+
current = outer;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function islandsSettled(app, pending) {
|
|
103
|
+
if (!pending.length)
|
|
104
|
+
return;
|
|
105
|
+
await Promise.all(pending);
|
|
106
|
+
markWaiting2(app.asyncData);
|
|
107
|
+
}
|
|
108
|
+
async function renderSettled(render) {
|
|
109
|
+
for (;; ) {
|
|
110
|
+
const pending = [];
|
|
111
|
+
const result = await collectingIslands(pending, () => render(pending));
|
|
112
|
+
if (!pending.length)
|
|
113
|
+
return result;
|
|
114
|
+
await Promise.all(pending);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { lazyModules, defineLazyHydrationComponent2, settledIsland, collectingIslands, islandsSettled, renderSettled };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// packages/adapter-preact/src/keepalive.ts
|
|
17
|
+
var exports_keepalive = {};
|
|
18
|
+
__export(exports_keepalive, {
|
|
19
|
+
createKeptOutlet: () => createKeptOutlet2
|
|
20
|
+
});
|
|
21
|
+
var SHOWN = "\x00shown";
|
|
22
|
+
var EMPTY = {};
|
|
23
|
+
function createKeptOutlet2({
|
|
24
|
+
createElement,
|
|
25
|
+
Fragment,
|
|
26
|
+
useContext,
|
|
27
|
+
useMemo,
|
|
28
|
+
useLayoutEffect,
|
|
29
|
+
ActivationContext,
|
|
30
|
+
activation,
|
|
31
|
+
detach,
|
|
32
|
+
setHidden,
|
|
33
|
+
RouteContext,
|
|
34
|
+
DataContext,
|
|
35
|
+
routeValue,
|
|
36
|
+
nest
|
|
37
|
+
}) {
|
|
38
|
+
const keptRoutes = new WeakMap;
|
|
39
|
+
function KeptPage(props) {
|
|
40
|
+
const parent = useContext(ActivationContext);
|
|
41
|
+
const own = useMemo(() => activation(parent, props.hidden), []);
|
|
42
|
+
useLayoutEffect(() => setHidden(own, props.hidden), [props.hidden]);
|
|
43
|
+
useLayoutEffect(() => () => detach(own), []);
|
|
44
|
+
const attributes = props.hidden ? { "data-hamur-keepalive": props.name, hidden: true } : { "data-hamur-keepalive": props.name, style: "display:contents" };
|
|
45
|
+
return createElement(ActivationContext.Provider, { value: own }, createElement("div", attributes, props.children));
|
|
46
|
+
}
|
|
47
|
+
const scoped = (route, payload, node) => createElement(RouteContext.Provider, { value: route }, createElement(DataContext.Provider, { value: payload?.data ?? EMPTY }, node));
|
|
48
|
+
return (holder, below, child, ctx) => {
|
|
49
|
+
const units = (holder.kept ?? []).map((unit) => {
|
|
50
|
+
const own = { ...ctx, route: unit.route, payload: unit.payload, tree: unit.tree };
|
|
51
|
+
let route = keptRoutes.get(unit);
|
|
52
|
+
if (!route)
|
|
53
|
+
keptRoutes.set(unit, route = routeValue(own));
|
|
54
|
+
return createElement(KeptPage, { key: unit.key, name: unit.key, hidden: true }, scoped(route, unit.payload, nest(unit.tree, own)));
|
|
55
|
+
});
|
|
56
|
+
if (child === undefined)
|
|
57
|
+
return units;
|
|
58
|
+
const shown = scoped(routeValue(ctx), ctx.payload, child);
|
|
59
|
+
const key = below?.keepalive;
|
|
60
|
+
return [
|
|
61
|
+
...units,
|
|
62
|
+
key === undefined ? createElement(Fragment, { key: SHOWN }, shown) : createElement(KeptPage, { key, name: key, hidden: false }, shown)
|
|
63
|
+
];
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { createKeptOutlet2, exports_keepalive };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// packages/adapter-preact/src/client-component.ts
|
|
2
|
+
import { createContext, createElement } from "preact";
|
|
3
|
+
import { Suspense, lazy } from "preact/compat";
|
|
4
|
+
import { useContext, useEffect, useState } from "preact/hooks";
|
|
5
|
+
var ClientOnlyContext = createContext(null);
|
|
6
|
+
function clientComponent2(load) {
|
|
7
|
+
const Loaded = lazy(load);
|
|
8
|
+
return function ClientComponent(props) {
|
|
9
|
+
const scope = useContext(ClientOnlyContext);
|
|
10
|
+
const [hydrated, setHydrated] = useState(scope !== null);
|
|
11
|
+
useEffect(() => setHydrated(true), []);
|
|
12
|
+
if (!hydrated)
|
|
13
|
+
return null;
|
|
14
|
+
return createElement(Suspense, { fallback: scope?.fallback ?? null }, createElement(Loaded, props));
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// packages/adapter-preact/src/client-fallback.ts
|
|
19
|
+
var clientFallbackIds = new Set;
|
|
20
|
+
|
|
21
|
+
export { ClientOnlyContext, clientComponent2, clientFallbackIds };
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// packages/adapter-preact/src/context.ts
|
|
2
|
+
import { Component as Component2, createContext, createElement, Fragment } from "preact";
|
|
3
|
+
import { useContext, useLayoutEffect, useMemo } from "preact/hooks";
|
|
4
|
+
import { createRouterApi, layoutOf, reportClientError, routeBoundary } from "hamur/adapter";
|
|
5
|
+
|
|
6
|
+
// packages/adapter-preact/src/activation.ts
|
|
7
|
+
function activation(parent, hidden) {
|
|
8
|
+
const own = { hidden, parent, children: new Set, on: new Set, off: new Set };
|
|
9
|
+
parent?.children.add(own);
|
|
10
|
+
return own;
|
|
11
|
+
}
|
|
12
|
+
function detach(own) {
|
|
13
|
+
own.parent?.children.delete(own);
|
|
14
|
+
}
|
|
15
|
+
function shown(own) {
|
|
16
|
+
return !own || !own.hidden && shown(own.parent);
|
|
17
|
+
}
|
|
18
|
+
var queue;
|
|
19
|
+
function later(isShown, fn) {
|
|
20
|
+
if (!queue) {
|
|
21
|
+
queue = [];
|
|
22
|
+
queueMicrotask(() => {
|
|
23
|
+
const run = queue;
|
|
24
|
+
queue = undefined;
|
|
25
|
+
for (const item of run)
|
|
26
|
+
if (!item.shown)
|
|
27
|
+
item.fn();
|
|
28
|
+
for (const item of run)
|
|
29
|
+
if (item.shown)
|
|
30
|
+
item.fn();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
queue.push({ shown: isShown, fn });
|
|
34
|
+
}
|
|
35
|
+
function fire(own, isShown) {
|
|
36
|
+
for (const fn of isShown ? own.on : own.off)
|
|
37
|
+
later(isShown, fn);
|
|
38
|
+
for (const child of own.children)
|
|
39
|
+
if (!child.hidden)
|
|
40
|
+
fire(child, isShown);
|
|
41
|
+
}
|
|
42
|
+
function setHidden(own, hidden) {
|
|
43
|
+
if (own.hidden === hidden)
|
|
44
|
+
return;
|
|
45
|
+
const before = shown(own);
|
|
46
|
+
own.hidden = hidden;
|
|
47
|
+
if (shown(own) !== before)
|
|
48
|
+
fire(own, !before);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// packages/adapter-preact/src/route-boundary.ts
|
|
52
|
+
import { Component } from "preact";
|
|
53
|
+
|
|
54
|
+
class RouteBoundary extends Component {
|
|
55
|
+
state = { failed: false, error: undefined };
|
|
56
|
+
failedFor;
|
|
57
|
+
static getDerivedStateFromError(error) {
|
|
58
|
+
return { failed: true, error };
|
|
59
|
+
}
|
|
60
|
+
render() {
|
|
61
|
+
if (this.state.failed) {
|
|
62
|
+
this.failedFor ??= this.props.resetOn;
|
|
63
|
+
if (this.failedFor === this.props.resetOn)
|
|
64
|
+
return this.props.fallback(this.state.error);
|
|
65
|
+
this.failedFor = undefined;
|
|
66
|
+
this.state = { failed: false, error: undefined };
|
|
67
|
+
}
|
|
68
|
+
return this.props.children;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
var rethrown = new WeakSet;
|
|
72
|
+
|
|
73
|
+
class Guard extends Component {
|
|
74
|
+
state = { failed: false, error: undefined };
|
|
75
|
+
static getDerivedStateFromError(error) {
|
|
76
|
+
if (typeof error === "object" && error !== null)
|
|
77
|
+
rethrown.add(error);
|
|
78
|
+
return { failed: true, error };
|
|
79
|
+
}
|
|
80
|
+
render() {
|
|
81
|
+
if (this.state.failed)
|
|
82
|
+
throw this.state.error;
|
|
83
|
+
return this.props.children;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// packages/adapter-preact/src/context.ts
|
|
88
|
+
import { routeStateOf, setServerHTML, emptyServerHTML, SLOT_ELEMENT } from "hamur/adapter";
|
|
89
|
+
import { withBase } from "hamur/app";
|
|
90
|
+
var RouteContext = createContext(null);
|
|
91
|
+
var RouterContext = createContext(null);
|
|
92
|
+
var NeverHydratedContext = createContext(false);
|
|
93
|
+
var SearchContext = createContext(null);
|
|
94
|
+
var searchStores = new WeakMap;
|
|
95
|
+
function searchStoreOf(app, search, router) {
|
|
96
|
+
let entry = searchStores.get(app);
|
|
97
|
+
if (!entry) {
|
|
98
|
+
let current = search;
|
|
99
|
+
let currentRouter = router;
|
|
100
|
+
const listeners = new Set;
|
|
101
|
+
entry = {
|
|
102
|
+
store: {
|
|
103
|
+
get: () => current,
|
|
104
|
+
router: () => currentRouter,
|
|
105
|
+
subscribe: (listener) => (listeners.add(listener), () => void listeners.delete(listener))
|
|
106
|
+
},
|
|
107
|
+
set(next, nextRouter) {
|
|
108
|
+
currentRouter = nextRouter;
|
|
109
|
+
if (next === current)
|
|
110
|
+
return;
|
|
111
|
+
current = next;
|
|
112
|
+
for (const listener of [...listeners])
|
|
113
|
+
listener();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
searchStores.set(app, entry);
|
|
117
|
+
}
|
|
118
|
+
entry.set(search, router);
|
|
119
|
+
return entry.store;
|
|
120
|
+
}
|
|
121
|
+
var BaseURLContext = createContext("/");
|
|
122
|
+
var HrefContext = createContext((to) => {
|
|
123
|
+
if (typeof to !== "string")
|
|
124
|
+
throw new Error(`Link to the route "${to.name}" must be rendered inside a Hamur app`);
|
|
125
|
+
return withBase(to, "/");
|
|
126
|
+
});
|
|
127
|
+
var DataContext = createContext({});
|
|
128
|
+
var EntryContext = createContext(null);
|
|
129
|
+
var ErrorContext = createContext({});
|
|
130
|
+
var HeadContext = createContext(null);
|
|
131
|
+
var StateContext = createContext(null);
|
|
132
|
+
var OutletContext = createContext(null);
|
|
133
|
+
var SlotsContext = createContext(null);
|
|
134
|
+
var OutletValueContext = createContext(undefined);
|
|
135
|
+
var LayoutContext = createContext(null);
|
|
136
|
+
var ActivationContext = createContext(undefined);
|
|
137
|
+
var SubmittedContext = createContext({});
|
|
138
|
+
var FormStatusContext = createContext(null);
|
|
139
|
+
var AppContextContext = createContext(null);
|
|
140
|
+
var NavigationContext = createContext(null);
|
|
141
|
+
var LinkHrefContext = createContext(null);
|
|
142
|
+
function navigationOf(navigator) {
|
|
143
|
+
const source = navigator;
|
|
144
|
+
return typeof source?.get === "function" && typeof source.subscribe === "function" ? source : null;
|
|
145
|
+
}
|
|
146
|
+
function routerOf(navigator, baseURL = "/", page) {
|
|
147
|
+
return createRouterApi({ ...page, navigator, baseURL });
|
|
148
|
+
}
|
|
149
|
+
function routeValue(ctx) {
|
|
150
|
+
const { route } = ctx;
|
|
151
|
+
return {
|
|
152
|
+
path: route.path,
|
|
153
|
+
fullPath: route.fullPath,
|
|
154
|
+
params: route.params,
|
|
155
|
+
query: route.query,
|
|
156
|
+
search: route.search,
|
|
157
|
+
hash: route.hash,
|
|
158
|
+
name: route.name,
|
|
159
|
+
meta: route.meta,
|
|
160
|
+
matched: route.matched,
|
|
161
|
+
groups: route.groups ?? [],
|
|
162
|
+
...routeStateOf(ctx)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function boundaryView(ctx, entry, error) {
|
|
166
|
+
if (typeof error === "object" && error !== null && rethrown.has(error))
|
|
167
|
+
throw error;
|
|
168
|
+
const caught = routeBoundary(ctx, entry, error);
|
|
169
|
+
if (!caught)
|
|
170
|
+
throw error;
|
|
171
|
+
const clearError = (options) => ctx.app.error?.clear(options) ?? Promise.resolve();
|
|
172
|
+
return createElement(Guard, null, createElement(EntryContext.Provider, { value: caught.id }, createElement(ErrorContext.Provider, { value: { initial: caught.error } }, createElement(caught.component, { error: caught.error, clearError }))));
|
|
173
|
+
}
|
|
174
|
+
function intercepted(slot, ctx, node) {
|
|
175
|
+
if (!slot.route)
|
|
176
|
+
return node ?? null;
|
|
177
|
+
const { state, key, navigationType } = ctx.route;
|
|
178
|
+
return createElement(RouteContext.Provider, { value: routeValue({ ...ctx, route: { ...slot.route, state, key, navigationType }, tree: slot }) }, node);
|
|
179
|
+
}
|
|
180
|
+
var keptOutlet;
|
|
181
|
+
function installKeepAlive2(module) {
|
|
182
|
+
keptOutlet ??= module.createKeptOutlet({
|
|
183
|
+
createElement,
|
|
184
|
+
Fragment,
|
|
185
|
+
useContext,
|
|
186
|
+
useMemo,
|
|
187
|
+
useLayoutEffect,
|
|
188
|
+
ActivationContext,
|
|
189
|
+
activation,
|
|
190
|
+
detach,
|
|
191
|
+
setHidden,
|
|
192
|
+
RouteContext,
|
|
193
|
+
DataContext,
|
|
194
|
+
routeValue,
|
|
195
|
+
nest
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
function keepAlive() {
|
|
199
|
+
if (!keptOutlet)
|
|
200
|
+
throw new Error("A tree with kept pages was rendered before the keep-alive part of the Preact adapter was installed");
|
|
201
|
+
return keptOutlet;
|
|
202
|
+
}
|
|
203
|
+
function nest(tree, ctx) {
|
|
204
|
+
let child;
|
|
205
|
+
for (let index = tree.components.length - 1;index >= 0; index--) {
|
|
206
|
+
const entry = tree.components[index];
|
|
207
|
+
const { id, component, props } = entry;
|
|
208
|
+
if (entry.kept !== undefined)
|
|
209
|
+
child = keepAlive()(entry, tree.components[index + 1], child, ctx);
|
|
210
|
+
let element = child === undefined ? createElement(component, props ?? null) : createElement(component, props ?? null, child);
|
|
211
|
+
if (entry.caught)
|
|
212
|
+
element = createElement(Guard, null, createElement(ErrorContext.Provider, { value: { initial: props?.error } }, element));
|
|
213
|
+
const slots = entry.outlets ? Object.fromEntries(Object.entries(entry.outlets).map(([name, slot]) => [name, intercepted(slot, ctx, nest(slot, ctx))])) : null;
|
|
214
|
+
child = createElement(EntryContext.Provider, { value: id }, createElement(SlotsContext.Provider, { value: slots }, createElement(OutletContext.Provider, { value: child ?? null }, element)));
|
|
215
|
+
if (entry.boundary)
|
|
216
|
+
child = createElement(RouteBoundary, { resetOn: entry, fallback: (error) => boundaryView(ctx, entry, error) }, child);
|
|
217
|
+
if (entry.key !== undefined)
|
|
218
|
+
child = createElement(Fragment, { key: entry.key }, child);
|
|
219
|
+
}
|
|
220
|
+
return child;
|
|
221
|
+
}
|
|
222
|
+
var EMPTY = {};
|
|
223
|
+
function buildElement(ctx) {
|
|
224
|
+
const error = {
|
|
225
|
+
store: ctx.app.error,
|
|
226
|
+
initial: routeStateOf({ payload: ctx.payload, tree: ctx.tree }).error
|
|
227
|
+
};
|
|
228
|
+
const route = routeValue(ctx);
|
|
229
|
+
const router = routerOf(ctx.navigator, ctx.baseURL, ctx);
|
|
230
|
+
return createElement(RouteContext.Provider, { value: route }, createElement(SearchContext.Provider, { value: searchStoreOf(ctx.app, route.search, router) }, createElement(RouterContext.Provider, { value: router }, createElement(DataContext.Provider, { value: ctx.payload?.data ?? EMPTY }, createElement(ErrorContext.Provider, { value: error }, createElement(HeadContext.Provider, { value: ctx.head }, createElement(StateContext.Provider, { value: ctx.app.state }, createElement(AppContextContext.Provider, { value: ctx.app }, createElement(SubmittedContext.Provider, { value: { handlerState: ctx.payload?.handlerState } }, createElement(NavigationContext.Provider, { value: navigationOf(ctx.navigator) }, createElement(BaseURLContext.Provider, { value: ctx.baseURL ?? "/" }, createElement(HrefContext.Provider, { value: ctx.href }, createElement(LayoutContext.Provider, { value: layoutOf(ctx.tree) }, ctx.tree.kept === undefined ? nest(ctx.tree, ctx) : keepAlive()(ctx.tree, ctx.tree.components[0], nest(ctx.tree, ctx), ctx))))))))))))));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
class RootBoundary extends Component2 {
|
|
234
|
+
componentDidCatch(error) {
|
|
235
|
+
this.setState({ failed: this.props.children });
|
|
236
|
+
queueMicrotask(() => reportClientError(this.props.app, error, { render: true }));
|
|
237
|
+
}
|
|
238
|
+
render() {
|
|
239
|
+
return this.state.failed === this.props.children ? null : this.props.children;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
var ISLAND_ENTRY = "island";
|
|
243
|
+
var keptSlot;
|
|
244
|
+
function slotElement(uid, html) {
|
|
245
|
+
if (typeof window === "undefined")
|
|
246
|
+
return createElement(SLOT_ELEMENT, { uid, dangerouslySetInnerHTML: { __html: html } });
|
|
247
|
+
keptSlot ??= { __html: emptyServerHTML() };
|
|
248
|
+
const fill = (node) => {
|
|
249
|
+
if (node && html && !node.hasChildNodes())
|
|
250
|
+
setServerHTML(node, html);
|
|
251
|
+
};
|
|
252
|
+
return createElement(SLOT_ELEMENT, { uid, dangerouslySetInnerHTML: keptSlot, ref: fill });
|
|
253
|
+
}
|
|
254
|
+
function islandElement(component, props, ctx) {
|
|
255
|
+
const href = (to) => {
|
|
256
|
+
if (typeof to !== "string")
|
|
257
|
+
throw new Error(`Link to the route "${to.name}" cannot render in an island; give it a path`);
|
|
258
|
+
return withBase(to, ctx.baseURL);
|
|
259
|
+
};
|
|
260
|
+
const slot = ctx.slots?.default;
|
|
261
|
+
const entryProps = slot === undefined ? props : { ...props, children: slotElement(`s${ctx.idPrefix}`, slot) };
|
|
262
|
+
const root = buildElement({
|
|
263
|
+
app: ctx.app,
|
|
264
|
+
head: ctx.app.head,
|
|
265
|
+
route: ctx.route,
|
|
266
|
+
baseURL: ctx.baseURL,
|
|
267
|
+
href,
|
|
268
|
+
tree: { components: [{ id: ISLAND_ENTRY, component, props: entryProps }] }
|
|
269
|
+
});
|
|
270
|
+
root.__m = [ctx.idPrefix, 0];
|
|
271
|
+
return root;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export { shown, later, RouteContext, RouterContext, NeverHydratedContext, SearchContext, BaseURLContext, HrefContext, DataContext, EntryContext, ErrorContext, HeadContext, StateContext, OutletContext, SlotsContext, OutletValueContext, LayoutContext, ActivationContext, SubmittedContext, FormStatusContext, AppContextContext, NavigationContext, LinkHrefContext, installKeepAlive2, buildElement, RootBoundary, islandElement };
|