@nifrajs/web-svelte 2.13.0 → 3.1.0
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/dist/Chain.svelte +1 -1
- package/dist/Router.svelte +1 -0
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcher.js +6 -9
- package/dist/fetcher.js.map +1 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +5 -25
- package/dist/query.js.map +1 -1
- package/package.json +5 -5
- package/src/Chain.svelte +1 -1
- package/src/Router.svelte +1 -0
- package/src/fetcher.ts +10 -10
- package/src/query.ts +9 -25
package/dist/Chain.svelte
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
</svelte:boundary>
|
|
38
38
|
{:else}
|
|
39
39
|
{@const Layout = chain[0]}
|
|
40
|
-
<Layout data={layoutData?.[0] ?? null}>
|
|
40
|
+
<Layout data={layoutData?.[0] ?? null} boundaries={props.boundaries}>
|
|
41
41
|
{#snippet children()}
|
|
42
42
|
<Self chain={chain.slice(1)} {props} layoutData={layoutData?.slice(1)} />
|
|
43
43
|
{/snippet}
|
package/dist/Router.svelte
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
pending: snapshot.pending,
|
|
38
38
|
search: searchOfChain(searchSchemas?.[snapshot.routeId] ?? [], q === -1 ? "" : snapshot.path.slice(q)),
|
|
39
39
|
...(snapshot.submission ? { submission: snapshot.submission } : {}),
|
|
40
|
+
...(snapshot.boundaries !== undefined ? { boundaries: snapshot.boundaries } : {}),
|
|
40
41
|
}
|
|
41
42
|
})
|
|
42
43
|
</script>
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAMvE;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAItD,oGAAoG;AACpG,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,GAAG,IAAI,CAEvE;AAED,kGAAkG;AAClG,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,YAAY,CAAC,GAAG;IAClD,qGAAqG;IACrG,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9C,oFAAoF;IACpF,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC3F,CAAA;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAWpD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAAC,SAAS,OAAO,EAAE,CAAC,CAO1D"}
|
package/dist/fetcher.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createMountedRouterRef, IDLE_FETCHER_STATE, noopAsync, } from "@nifrajs/web/internal/fetcher-runtime";
|
|
1
2
|
/**
|
|
2
3
|
* `@nifrajs/web-svelte/fetcher` - Svelte bindings for concurrent fetchers, as **Svelte stores** (plain
|
|
3
4
|
* `.ts`, no runes/compiler needed). `useFetcher(key)` returns a `Readable<FetcherState>` augmented with
|
|
@@ -11,23 +12,19 @@
|
|
|
11
12
|
* (fetchers are client-only); the first client render starts from the same idle snapshot - no mismatch.
|
|
12
13
|
*/
|
|
13
14
|
import { readable } from "svelte/store";
|
|
14
|
-
|
|
15
|
-
// page, and fetchers never exist on the server. Shared with `client.ts`'s `mountRouter`.
|
|
16
|
-
let mountedRouter;
|
|
15
|
+
const mountedRouter = createMountedRouterRef();
|
|
17
16
|
/** Register (or clear) the router that owns fetchers - called by `mountRouter`. Not for app use. */
|
|
18
17
|
export function setMountedRouter(router) {
|
|
19
|
-
mountedRouter
|
|
18
|
+
mountedRouter.set(router);
|
|
20
19
|
}
|
|
21
|
-
const IDLE = { pending: false, data: undefined };
|
|
22
|
-
const noopAsync = async () => { };
|
|
23
20
|
/**
|
|
24
21
|
* Subscribe to the independent fetcher for `key` (created lazily, stable across renders). Returns a
|
|
25
22
|
* store of its state (`pending`/`data`/`actionData`/`submission`) augmented with `load`/`submit`.
|
|
26
23
|
* Multiple `useFetcher` calls with different keys run concurrently without disturbing the active route.
|
|
27
24
|
*/
|
|
28
25
|
export function useFetcher(key) {
|
|
29
|
-
const fetcher = mountedRouter?.fetcher(key);
|
|
30
|
-
const store = readable(fetcher ? fetcher.snapshot() :
|
|
26
|
+
const fetcher = mountedRouter.get()?.fetcher(key);
|
|
27
|
+
const store = readable(fetcher ? fetcher.snapshot() : IDLE_FETCHER_STATE, (set) => {
|
|
31
28
|
if (fetcher === undefined)
|
|
32
29
|
return;
|
|
33
30
|
set(fetcher.snapshot());
|
|
@@ -44,7 +41,7 @@ export function useFetcher(key) {
|
|
|
44
41
|
* new one is created.
|
|
45
42
|
*/
|
|
46
43
|
export function useFetchers() {
|
|
47
|
-
const router = mountedRouter;
|
|
44
|
+
const router = mountedRouter.get();
|
|
48
45
|
return readable(router ? router.fetchers() : [], (set) => {
|
|
49
46
|
if (router === undefined)
|
|
50
47
|
return;
|
package/dist/fetcher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.js","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;GAWG;AACH,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,
|
|
1
|
+
{"version":3,"file":"fetcher.js","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AACA,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,SAAS,GACV,MAAM,uCAAuC,CAAA;AAC9C;;;;;;;;;;;GAWG;AACH,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,MAAM,aAAa,GAAG,sBAAsB,EAAE,CAAA;AAE9C,oGAAoG;AACpG,MAAM,UAAU,gBAAgB,CAAC,MAAgC;IAC/D,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAC3B,CAAC;AAUD;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;IACjD,MAAM,KAAK,GAAG,QAAQ,CAAe,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,kBAAkB,EAAE,CAAC,GAAG,EAAE,EAAE;QAC9F,IAAI,OAAO,KAAK,SAAS;YAAE,OAAM;QACjC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAA;QACvB,OAAO,OAAO,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;IACzD,CAAC,CAAC,CAAA;IACF,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;QAC1B,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,SAAS;QAChC,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,SAAS;KACrC,CAAC,CAAA;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW;IACzB,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,EAAE,CAAA;IAClC,OAAO,QAAQ,CAAqB,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3E,IAAI,MAAM,KAAK,SAAS;YAAE,OAAM;QAChC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;IAC/D,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/query.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAe,UAAU,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAe,UAAU,EAAE,MAAM,cAAc,CAAA;AAMxE;;;;;;;;;;GAUG;AACH,OAAO,EAAE,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAEtD,wGAAwG;AACxG,wBAAgB,cAAc,IAAI,IAAI,CAAC,WAAW,EAAE,mBAAmB,CAAC,CAEvE;AAED,gFAAgF;AAChF,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG;IACpD,6CAA6C;IAC7C,QAAQ,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;CACnC,CAAA;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAa7E"}
|
package/dist/query.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
// server, which Vite's dev server evaluates rather than tree-shakes.
|
|
3
|
-
import { createQueryClient } from "@nifrajs/web/client";
|
|
1
|
+
import { createNoClientRefetch, getQueryClientSingleton, IDLE_QUERY_STATE, } from "@nifrajs/web/internal/query-runtime";
|
|
4
2
|
/**
|
|
5
3
|
* `@nifrajs/web-svelte/query` - Svelte bindings for the keyed query-cache, as **Svelte stores** (plain
|
|
6
4
|
* `.ts`). `useQuery(key, fn)` returns a `Readable<QueryState<T>>` augmented with `refetch` - read it
|
|
@@ -13,33 +11,17 @@ import { createQueryClient } from "@nifrajs/web/client";
|
|
|
13
11
|
* SSR data source). The first client render uses the same idle snapshot, so no mismatch.
|
|
14
12
|
*/
|
|
15
13
|
import { readable } from "svelte/store";
|
|
16
|
-
let client;
|
|
17
|
-
function getClient() {
|
|
18
|
-
if (typeof window === "undefined")
|
|
19
|
-
return undefined; // SSR → no client (queries render idle)
|
|
20
|
-
if (client === undefined)
|
|
21
|
-
client = createQueryClient({ now: () => Date.now() });
|
|
22
|
-
return client;
|
|
23
|
-
}
|
|
24
14
|
/** Access the query client to imperatively `invalidateQueries(keyOrPrefix)` (e.g. after a mutation). */
|
|
25
15
|
export function useQueryClient() {
|
|
26
|
-
return
|
|
16
|
+
return getQueryClientSingleton() ?? { invalidateQueries: () => { } };
|
|
27
17
|
}
|
|
28
|
-
// Stable idle snapshot for the server / pre-fetch render.
|
|
29
|
-
const IDLE = Object.freeze({
|
|
30
|
-
status: "pending",
|
|
31
|
-
data: undefined,
|
|
32
|
-
error: undefined,
|
|
33
|
-
isFetching: false,
|
|
34
|
-
updatedAt: Number.NEGATIVE_INFINITY,
|
|
35
|
-
});
|
|
36
18
|
/**
|
|
37
19
|
* Subscribe to the keyed query for `key`, fetched via `fn`. Returns a store of `{ status, data, error,
|
|
38
20
|
* isFetching, updatedAt }` augmented with `refetch`. Fetches on mount (first `$`-subscription); SSR-idle.
|
|
39
21
|
*/
|
|
40
22
|
export function useQuery(key, fn) {
|
|
41
|
-
const handle =
|
|
42
|
-
const store = readable(handle ? handle.snapshot() :
|
|
23
|
+
const handle = getQueryClientSingleton()?.query(key, fn);
|
|
24
|
+
const store = readable(handle ? handle.snapshot() : IDLE_QUERY_STATE, (set) => {
|
|
43
25
|
if (handle === undefined)
|
|
44
26
|
return;
|
|
45
27
|
set(handle.snapshot());
|
|
@@ -49,9 +31,7 @@ export function useQuery(key, fn) {
|
|
|
49
31
|
});
|
|
50
32
|
const refetch = handle
|
|
51
33
|
? handle.refetch
|
|
52
|
-
: (
|
|
53
|
-
throw new Error("[nifra/web-svelte] useQuery.refetch called with no query client (server?)");
|
|
54
|
-
});
|
|
34
|
+
: createNoClientRefetch("[nifra/web-svelte]", "useQuery.refetch");
|
|
55
35
|
return Object.assign(store, { refetch });
|
|
56
36
|
}
|
|
57
37
|
//# sourceMappingURL=query.js.map
|
package/dist/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AACA,
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EACrB,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,qCAAqC,CAAA;AAC5C;;;;;;;;;;GAUG;AACH,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,wGAAwG;AACxG,MAAM,UAAU,cAAc;IAC5B,OAAO,uBAAuB,EAAE,IAAI,EAAE,iBAAiB,EAAE,GAAG,EAAE,GAAE,CAAC,EAAE,CAAA;AACrE,CAAC;AAQD;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAI,GAAY,EAAE,EAAoB;IAC5D,MAAM,MAAM,GAA+B,uBAAuB,EAAE,EAAE,KAAK,CAAI,GAAG,EAAE,EAAE,CAAC,CAAA;IACvF,MAAM,KAAK,GAAG,QAAQ,CAAgB,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,gBAAgB,EAAE,CAAC,GAAG,EAAE,EAAE;QAC3F,IAAI,MAAM,KAAK,SAAS;YAAE,OAAM;QAChC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;QACtB,MAAM,WAAW,GAAG,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;QAClE,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA,CAAC,iDAAiD;QAChF,OAAO,WAAW,CAAA;IACpB,CAAC,CAAC,CAAA;IACF,MAAM,OAAO,GAAG,MAAM;QACpB,CAAC,CAAC,MAAM,CAAC,OAAO;QAChB,CAAC,CAAC,qBAAqB,CAAI,oBAAoB,EAAE,kBAAkB,CAAC,CAAA;IACtE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,CAAA;AAC1C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nifrajs/web-svelte",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Svelte 5 render adapter for @nifrajs/web - SSR + hydration + the .svelte compiler Bun-plugin.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bun",
|
|
@@ -100,10 +100,10 @@
|
|
|
100
100
|
"access": "public"
|
|
101
101
|
},
|
|
102
102
|
"dependencies": {
|
|
103
|
-
"@nifrajs/core": "
|
|
104
|
-
"@nifrajs/i18n": "
|
|
105
|
-
"@nifrajs/image": "
|
|
106
|
-
"@nifrajs/web": "
|
|
103
|
+
"@nifrajs/core": "3.1.0",
|
|
104
|
+
"@nifrajs/i18n": "3.1.0",
|
|
105
|
+
"@nifrajs/image": "3.1.0",
|
|
106
|
+
"@nifrajs/web": "3.1.0"
|
|
107
107
|
},
|
|
108
108
|
"peerDependencies": {
|
|
109
109
|
"mdsvex": "^0.12",
|
package/src/Chain.svelte
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
</svelte:boundary>
|
|
38
38
|
{:else}
|
|
39
39
|
{@const Layout = chain[0]}
|
|
40
|
-
<Layout data={layoutData?.[0] ?? null}>
|
|
40
|
+
<Layout data={layoutData?.[0] ?? null} boundaries={props.boundaries}>
|
|
41
41
|
{#snippet children()}
|
|
42
42
|
<Self chain={chain.slice(1)} {props} layoutData={layoutData?.slice(1)} />
|
|
43
43
|
{/snippet}
|
package/src/Router.svelte
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
pending: snapshot.pending,
|
|
38
38
|
search: searchOfChain(searchSchemas?.[snapshot.routeId] ?? [], q === -1 ? "" : snapshot.path.slice(q)),
|
|
39
39
|
...(snapshot.submission ? { submission: snapshot.submission } : {}),
|
|
40
|
+
...(snapshot.boundaries !== undefined ? { boundaries: snapshot.boundaries } : {}),
|
|
40
41
|
}
|
|
41
42
|
})
|
|
42
43
|
</script>
|
package/src/fetcher.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { ClientRouter, Fetcher, FetcherState } from "@nifrajs/web"
|
|
2
|
+
import {
|
|
3
|
+
createMountedRouterRef,
|
|
4
|
+
IDLE_FETCHER_STATE,
|
|
5
|
+
noopAsync,
|
|
6
|
+
} from "@nifrajs/web/internal/fetcher-runtime"
|
|
2
7
|
/**
|
|
3
8
|
* `@nifrajs/web-svelte/fetcher` - Svelte bindings for concurrent fetchers, as **Svelte stores** (plain
|
|
4
9
|
* `.ts`, no runes/compiler needed). `useFetcher(key)` returns a `Readable<FetcherState>` augmented with
|
|
@@ -13,18 +18,13 @@ import type { ClientRouter, Fetcher, FetcherState } from "@nifrajs/web"
|
|
|
13
18
|
*/
|
|
14
19
|
import { type Readable, readable } from "svelte/store"
|
|
15
20
|
|
|
16
|
-
|
|
17
|
-
// page, and fetchers never exist on the server. Shared with `client.ts`'s `mountRouter`.
|
|
18
|
-
let mountedRouter: ClientRouter | undefined
|
|
21
|
+
const mountedRouter = createMountedRouterRef()
|
|
19
22
|
|
|
20
23
|
/** Register (or clear) the router that owns fetchers - called by `mountRouter`. Not for app use. */
|
|
21
24
|
export function setMountedRouter(router: ClientRouter | undefined): void {
|
|
22
|
-
mountedRouter
|
|
25
|
+
mountedRouter.set(router)
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
const IDLE: FetcherState = { pending: false, data: undefined }
|
|
26
|
-
const noopAsync = async (): Promise<void> => {}
|
|
27
|
-
|
|
28
28
|
/** A fetcher store: a `Readable<FetcherState>` (read via `$`) plus imperative `load`/`submit`. */
|
|
29
29
|
export type FetcherStore = Readable<FetcherState> & {
|
|
30
30
|
/** Load a route path's loader data into this fetcher (concurrent; doesn't touch the active view). */
|
|
@@ -39,8 +39,8 @@ export type FetcherStore = Readable<FetcherState> & {
|
|
|
39
39
|
* Multiple `useFetcher` calls with different keys run concurrently without disturbing the active route.
|
|
40
40
|
*/
|
|
41
41
|
export function useFetcher(key: string): FetcherStore {
|
|
42
|
-
const fetcher = mountedRouter?.fetcher(key)
|
|
43
|
-
const store = readable<FetcherState>(fetcher ? fetcher.snapshot() :
|
|
42
|
+
const fetcher = mountedRouter.get()?.fetcher(key)
|
|
43
|
+
const store = readable<FetcherState>(fetcher ? fetcher.snapshot() : IDLE_FETCHER_STATE, (set) => {
|
|
44
44
|
if (fetcher === undefined) return
|
|
45
45
|
set(fetcher.snapshot())
|
|
46
46
|
return fetcher.subscribe(() => set(fetcher.snapshot()))
|
|
@@ -57,7 +57,7 @@ export function useFetcher(key: string): FetcherStore {
|
|
|
57
57
|
* new one is created.
|
|
58
58
|
*/
|
|
59
59
|
export function useFetchers(): Readable<readonly Fetcher[]> {
|
|
60
|
-
const router = mountedRouter
|
|
60
|
+
const router = mountedRouter.get()
|
|
61
61
|
return readable<readonly Fetcher[]>(router ? router.fetchers() : [], (set) => {
|
|
62
62
|
if (router === undefined) return
|
|
63
63
|
set(router.fetchers())
|
package/src/query.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { QueryClient, QueryHandle, QueryState } from "@nifrajs/web"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import {
|
|
3
|
+
createNoClientRefetch,
|
|
4
|
+
getQueryClientSingleton,
|
|
5
|
+
IDLE_QUERY_STATE,
|
|
6
|
+
} from "@nifrajs/web/internal/query-runtime"
|
|
5
7
|
/**
|
|
6
8
|
* `@nifrajs/web-svelte/query` - Svelte bindings for the keyed query-cache, as **Svelte stores** (plain
|
|
7
9
|
* `.ts`). `useQuery(key, fn)` returns a `Readable<QueryState<T>>` augmented with `refetch` - read it
|
|
@@ -15,27 +17,11 @@ import { createQueryClient } from "@nifrajs/web/client"
|
|
|
15
17
|
*/
|
|
16
18
|
import { type Readable, readable } from "svelte/store"
|
|
17
19
|
|
|
18
|
-
let client: QueryClient | undefined
|
|
19
|
-
function getClient(): QueryClient | undefined {
|
|
20
|
-
if (typeof window === "undefined") return undefined // SSR → no client (queries render idle)
|
|
21
|
-
if (client === undefined) client = createQueryClient({ now: () => Date.now() })
|
|
22
|
-
return client
|
|
23
|
-
}
|
|
24
|
-
|
|
25
20
|
/** Access the query client to imperatively `invalidateQueries(keyOrPrefix)` (e.g. after a mutation). */
|
|
26
21
|
export function useQueryClient(): Pick<QueryClient, "invalidateQueries"> {
|
|
27
|
-
return
|
|
22
|
+
return getQueryClientSingleton() ?? { invalidateQueries: () => {} }
|
|
28
23
|
}
|
|
29
24
|
|
|
30
|
-
// Stable idle snapshot for the server / pre-fetch render.
|
|
31
|
-
const IDLE: QueryState<never> = Object.freeze({
|
|
32
|
-
status: "pending",
|
|
33
|
-
data: undefined,
|
|
34
|
-
error: undefined,
|
|
35
|
-
isFetching: false,
|
|
36
|
-
updatedAt: Number.NEGATIVE_INFINITY,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
25
|
/** A query store: a `Readable<QueryState<T>>` (read via `$`) plus `refetch`. */
|
|
40
26
|
export type QueryStore<T> = Readable<QueryState<T>> & {
|
|
41
27
|
/** Force a refetch (ignores `staleTime`). */
|
|
@@ -47,8 +33,8 @@ export type QueryStore<T> = Readable<QueryState<T>> & {
|
|
|
47
33
|
* isFetching, updatedAt }` augmented with `refetch`. Fetches on mount (first `$`-subscription); SSR-idle.
|
|
48
34
|
*/
|
|
49
35
|
export function useQuery<T>(key: unknown, fn: () => Promise<T>): QueryStore<T> {
|
|
50
|
-
const handle: QueryHandle<T> | undefined =
|
|
51
|
-
const store = readable<QueryState<T>>(handle ? handle.snapshot() :
|
|
36
|
+
const handle: QueryHandle<T> | undefined = getQueryClientSingleton()?.query<T>(key, fn)
|
|
37
|
+
const store = readable<QueryState<T>>(handle ? handle.snapshot() : IDLE_QUERY_STATE, (set) => {
|
|
52
38
|
if (handle === undefined) return
|
|
53
39
|
set(handle.snapshot())
|
|
54
40
|
const unsubscribe = handle.subscribe(() => set(handle.snapshot()))
|
|
@@ -57,8 +43,6 @@ export function useQuery<T>(key: unknown, fn: () => Promise<T>): QueryStore<T> {
|
|
|
57
43
|
})
|
|
58
44
|
const refetch = handle
|
|
59
45
|
? handle.refetch
|
|
60
|
-
: (
|
|
61
|
-
throw new Error("[nifra/web-svelte] useQuery.refetch called with no query client (server?)")
|
|
62
|
-
}) as () => Promise<T>)
|
|
46
|
+
: createNoClientRefetch<T>("[nifra/web-svelte]", "useQuery.refetch")
|
|
63
47
|
return Object.assign(store, { refetch })
|
|
64
48
|
}
|