@blaze-cache/react-query 0.1.1 → 0.2.1-beta
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 +15 -6
- package/dist/hash.d.ts +26 -5
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +30 -4
- package/dist/hash.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/sync.d.ts +32 -5
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +88 -7
- package/dist/sync.js.map +1 -1
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useBlazeQuery.d.ts +23 -3
- package/dist/useBlazeQuery.d.ts.map +1 -1
- package/dist/useBlazeQuery.js +50 -14
- package/dist/useBlazeQuery.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -10,9 +10,12 @@ stale-while-revalidate still works.
|
|
|
10
10
|
## Install
|
|
11
11
|
|
|
12
12
|
```sh
|
|
13
|
-
npm install @blaze-cache/react-query # @tanstack/react-query ≥ 5 is a peer dependency
|
|
13
|
+
npm install @blaze-cache/react-query@beta # @tanstack/react-query ≥ 5 is a peer dependency
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
`@beta` is npm's dist-tag for the prerelease; drop it to install the last stable
|
|
17
|
+
release.
|
|
18
|
+
|
|
16
19
|
## Quick start
|
|
17
20
|
|
|
18
21
|
```ts
|
|
@@ -34,14 +37,20 @@ const todos = useBlazeQuery(cache, {
|
|
|
34
37
|
|
|
35
38
|
A mirrored query is injected as `initialData` **with its original `dataUpdatedAt`** —
|
|
36
39
|
the first paint after a reload comes from the cache instantly, while TanStack still
|
|
37
|
-
treats the data as stale and refetches in the background.
|
|
40
|
+
treats the data as stale and refetches in the background. Options that already carry
|
|
41
|
+
`initialData` or `initialDataUpdatedAt` are left entirely alone, and the mirror is looked
|
|
42
|
+
up with TanStack's own hashing, so a custom `queryKeyHashFn` is honoured.
|
|
38
43
|
|
|
39
44
|
## Behaviour
|
|
40
45
|
|
|
41
|
-
* Successful fetches and `setQueryData` are written to the shared cache
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
* Successful fetches and `setQueryData` are written to the shared cache.
|
|
47
|
+
* An **explicit** `removeQueries` or `clear` deletes from it. Ordinary garbage
|
|
48
|
+
collection — a query left unobserved past `gcTime` — deliberately does **not**: that
|
|
49
|
+
is in-session housekeeping, and the persisted copy is what paints the next session.
|
|
50
|
+
* Invalidation keeps the data too, so stale-while-revalidate always has something to
|
|
51
|
+
paint.
|
|
52
|
+
* Renders never touch cache statistics: the pre-fetch lookup prefers `peek` over `get`,
|
|
53
|
+
so StrictMode's double render cannot move the hit rate or influence eviction.
|
|
45
54
|
* Error states stay in TanStack — only data crosses into the cache.
|
|
46
55
|
* TanStack owns freshness *within* a session; Blaze Cache owns it *across* sessions.
|
|
47
56
|
|
package/dist/hash.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type { QueryKey } from '@tanstack/react-query';
|
|
1
|
+
import type { QueryClient, QueryKey, QueryOptions } from '@tanstack/react-query';
|
|
2
2
|
/**
|
|
3
3
|
* Canonical hash for a query key — the very hash TanStack Query stores in
|
|
4
|
-
* `query.queryHash
|
|
4
|
+
* `query.queryHash`, *when the default hasher is in play*.
|
|
5
5
|
*
|
|
6
|
-
* Delegating to TanStack's own `hashKey` rather than re-implementing it means the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* Delegating to TanStack's own `hashKey` rather than re-implementing it means the lookup
|
|
7
|
+
* never drifts from the key the sync layer writes, even across library upgrades. When a
|
|
8
|
+
* custom `queryKeyHashFn` configures the hashing, that is {@link resolveQueryHash}'s job —
|
|
9
|
+
* the raw default hash would land on a key the mirror never used.
|
|
9
10
|
*
|
|
10
11
|
* ```ts
|
|
11
12
|
* hashQueryKey(['todos', { page: 1, done: true }]);
|
|
@@ -13,4 +14,24 @@ import type { QueryKey } from '@tanstack/react-query';
|
|
|
13
14
|
* ```
|
|
14
15
|
*/
|
|
15
16
|
export declare function hashQueryKey(queryKey: QueryKey): string;
|
|
17
|
+
/**
|
|
18
|
+
* The hash TanStack will file this query under — the exact string `queryCache.build` computes
|
|
19
|
+
* for `query.queryHash`, and therefore the exact key {@link syncBlazeQueryCache} writes to.
|
|
20
|
+
*
|
|
21
|
+
* TanStack only reaches the default {@link hashQueryKey} after a chain of overrides, replayed
|
|
22
|
+
* here from `defaultQueryOptions` exactly as TanStack orders it: an explicit `queryHash` on
|
|
23
|
+
* the options wins outright, then a per-query `queryKeyHashFn`, then one registered through
|
|
24
|
+
* `setQueryDefaults`, then the client's `defaultOptions.queries`. Looking up the mirror with
|
|
25
|
+
* plain `hashQueryKey` while any of those are configured is a silent no-op — sync writes under
|
|
26
|
+
* the custom hash and the lookup never finds it, so the "instant first paint" feature quietly
|
|
27
|
+
* stops working for everyone who configured a hasher.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* const client = new QueryClient({
|
|
31
|
+
* defaultOptions: { queries: { queryKeyHashFn: (key) => myHash(key) } },
|
|
32
|
+
* });
|
|
33
|
+
* resolveQueryHash(client, ['todos']); // myHash(['todos']) — what sync wrote
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function resolveQueryHash<TQueryKey extends QueryKey = QueryKey>(client: QueryClient, queryKey: TQueryKey, options?: Pick<QueryOptions<unknown, unknown, unknown, TQueryKey>, 'queryHash' | 'queryKeyHashFn'>): string;
|
|
16
37
|
//# sourceMappingURL=hash.d.ts.map
|
package/dist/hash.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAEjF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAEvD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,SAAS,QAAQ,GAAG,QAAQ,EACpE,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,SAAS,EACnB,OAAO,CAAC,EAAE,IAAI,CACZ,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,CAAC,EAClD,WAAW,GAAG,gBAAgB,CAC/B,GACA,MAAM,CAOR"}
|
package/dist/hash.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { hashKey } from '@tanstack/react-query';
|
|
2
2
|
/**
|
|
3
3
|
* Canonical hash for a query key — the very hash TanStack Query stores in
|
|
4
|
-
* `query.queryHash
|
|
4
|
+
* `query.queryHash`, *when the default hasher is in play*.
|
|
5
5
|
*
|
|
6
|
-
* Delegating to TanStack's own `hashKey` rather than re-implementing it means the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* Delegating to TanStack's own `hashKey` rather than re-implementing it means the lookup
|
|
7
|
+
* never drifts from the key the sync layer writes, even across library upgrades. When a
|
|
8
|
+
* custom `queryKeyHashFn` configures the hashing, that is {@link resolveQueryHash}'s job —
|
|
9
|
+
* the raw default hash would land on a key the mirror never used.
|
|
9
10
|
*
|
|
10
11
|
* ```ts
|
|
11
12
|
* hashQueryKey(['todos', { page: 1, done: true }]);
|
|
@@ -15,4 +16,29 @@ import { hashKey } from '@tanstack/react-query';
|
|
|
15
16
|
export function hashQueryKey(queryKey) {
|
|
16
17
|
return hashKey(queryKey);
|
|
17
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* The hash TanStack will file this query under — the exact string `queryCache.build` computes
|
|
21
|
+
* for `query.queryHash`, and therefore the exact key {@link syncBlazeQueryCache} writes to.
|
|
22
|
+
*
|
|
23
|
+
* TanStack only reaches the default {@link hashQueryKey} after a chain of overrides, replayed
|
|
24
|
+
* here from `defaultQueryOptions` exactly as TanStack orders it: an explicit `queryHash` on
|
|
25
|
+
* the options wins outright, then a per-query `queryKeyHashFn`, then one registered through
|
|
26
|
+
* `setQueryDefaults`, then the client's `defaultOptions.queries`. Looking up the mirror with
|
|
27
|
+
* plain `hashQueryKey` while any of those are configured is a silent no-op — sync writes under
|
|
28
|
+
* the custom hash and the lookup never finds it, so the "instant first paint" feature quietly
|
|
29
|
+
* stops working for everyone who configured a hasher.
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* const client = new QueryClient({
|
|
33
|
+
* defaultOptions: { queries: { queryKeyHashFn: (key) => myHash(key) } },
|
|
34
|
+
* });
|
|
35
|
+
* resolveQueryHash(client, ['todos']); // myHash(['todos']) — what sync wrote
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function resolveQueryHash(client, queryKey, options) {
|
|
39
|
+
const queryKeyHashFn = options?.queryKeyHashFn ??
|
|
40
|
+
client.getQueryDefaults(queryKey).queryKeyHashFn ??
|
|
41
|
+
client.getDefaultOptions().queries?.queryKeyHashFn;
|
|
42
|
+
return options?.queryHash ?? (queryKeyHashFn ?? hashKey)(queryKey);
|
|
43
|
+
}
|
|
18
44
|
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAGhD
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAGhD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,YAAY,CAAC,QAAkB;IAC7C,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,MAAmB,EACnB,QAAmB,EACnB,OAGC;IAED,MAAM,cAAc,GAClB,OAAO,EAAE,cAAc;QACvB,MAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,cAAc;QAChD,MAAM,CAAC,iBAAiB,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC;IAErD,OAAO,OAAO,EAAE,SAAS,IAAI,CAAC,cAAc,IAAI,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC;AACrE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
* @packageDocumentation
|
|
25
25
|
*/
|
|
26
26
|
export { createBlazeQueryClient, syncBlazeQueryCache } from './sync.js';
|
|
27
|
-
export { hashQueryKey } from './hash.js';
|
|
28
|
-
export { useBlazeQuery } from './useBlazeQuery.js';
|
|
27
|
+
export { hashQueryKey, resolveQueryHash } from './hash.js';
|
|
28
|
+
export { seedBlazeQueryOptions, useBlazeQuery } from './useBlazeQuery.js';
|
|
29
29
|
export type { BlazeQueryCacheLike, BlazeQueryClientOptions, BlazeQueryRecord, BlazeQuerySyncOptions, } from './types.js';
|
|
30
30
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAE1E,YAAY,EACV,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,6 @@
|
|
|
24
24
|
* @packageDocumentation
|
|
25
25
|
*/
|
|
26
26
|
export { createBlazeQueryClient, syncBlazeQueryCache } from './sync.js';
|
|
27
|
-
export { hashQueryKey } from './hash.js';
|
|
28
|
-
export { useBlazeQuery } from './useBlazeQuery.js';
|
|
27
|
+
export { hashQueryKey, resolveQueryHash } from './hash.js';
|
|
28
|
+
export { seedBlazeQueryOptions, useBlazeQuery } from './useBlazeQuery.js';
|
|
29
29
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/sync.d.ts
CHANGED
|
@@ -10,10 +10,34 @@ import type { BlazeQueryClientOptions, BlazeQuerySyncOptions } from './types.js'
|
|
|
10
10
|
* | `updated` + `success`/`setState` | mirrored — a completed fetch or `setQueryData` |
|
|
11
11
|
* | `updated` + `invalidate` | **kept** — stale data stays so `staleTime: 0` can still paint instantly |
|
|
12
12
|
* | `updated` + `error` | ignored — TanStack owns error state in-session |
|
|
13
|
-
* | `removed` | deleted, so `removeQueries` really
|
|
13
|
+
* | `removed` after an explicit purge | deleted, so `removeQueries`/`clear` really remove it |
|
|
14
|
+
* | `removed` after garbage collection | **kept** — GC is in-session housekeeping, not a purge |
|
|
14
15
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
16
|
+
* ### Telling garbage collection from an explicit purge
|
|
17
|
+
*
|
|
18
|
+
* TanStack emits the same `removed` event for both, carrying nothing that distinguishes
|
|
19
|
+
* them: once a query has sat at zero observers for `gcTime` (5 minutes by default),
|
|
20
|
+
* `optionalRemove()` calls `queryCache.remove()` and the subscriber is none the wiser.
|
|
21
|
+
* Deleting the mirror there would gut the design — navigate away for a few minutes, come
|
|
22
|
+
* back, and every persisted query has silently vanished, taking the "instant first paint
|
|
23
|
+
* from IndexedDB" feature with it.
|
|
24
|
+
*
|
|
25
|
+
* Since the event offers no hook, the explicit APIs are wrapped instead: for the lifetime of
|
|
26
|
+
* this subscription, `client.removeQueries` and `client.clear` raise an `explicitRemoval`
|
|
27
|
+
* flag around the original call — in a `try/finally`, bound to the client so the signature
|
|
28
|
+
* and return value are untouched — and the subscriber deletes the mirror only while that
|
|
29
|
+
* flag is up (`skip` still applies). The returned unsubscribe function restores the
|
|
30
|
+
* originals verbatim, so a long-lived client is never left permanently patched; if a second
|
|
31
|
+
* sync is stacked on the same client, wrappers restore in whichever order they detach (the
|
|
32
|
+
* outermost one always puts back exactly what it found).
|
|
33
|
+
*
|
|
34
|
+
* Returns the unsubscribe function — call it to detach. {@link createBlazeQueryClient} does
|
|
35
|
+
* not need it: the listener hangs off the client's own `QueryCache`, so client, cache and
|
|
36
|
+
* subscription are collected together when you drop the client. There is also nothing to
|
|
37
|
+
* wire teardown into — TanStack's `QueryClient` has no `destroy()` method, and its real
|
|
38
|
+
* lifecycle hooks (`mount`/`unmount`) fire on every `QueryClientProvider` mount — including
|
|
39
|
+
* StrictMode's immediate re-mount — so hooking them would detach the mirror while the client
|
|
40
|
+
* very much still lives.
|
|
17
41
|
*/
|
|
18
42
|
export declare function syncBlazeQueryCache(client: QueryClient, options: BlazeQuerySyncOptions): () => void;
|
|
19
43
|
/**
|
|
@@ -27,8 +51,11 @@ export declare function syncBlazeQueryCache(client: QueryClient, options: BlazeQ
|
|
|
27
51
|
* ```
|
|
28
52
|
*
|
|
29
53
|
* The subscription lives exactly as long as the client: the `QueryCache` holds the listener,
|
|
30
|
-
* and the client holds the cache, so they are collected together once you drop the client
|
|
31
|
-
*
|
|
54
|
+
* and the client holds the cache, so they are collected together once you drop the client —
|
|
55
|
+
* there is no `destroy()` on TanStack's `QueryClient` to wire teardown into, and none is
|
|
56
|
+
* needed. The method wrappers {@link syncBlazeQueryCache} installs live just as long and go
|
|
57
|
+
* with the client; attach by hand via {@link syncBlazeQueryCache} if you want an unsubscribe
|
|
58
|
+
* that releases them earlier.
|
|
32
59
|
*/
|
|
33
60
|
export declare function createBlazeQueryClient(options: BlazeQueryClientOptions): QueryClient;
|
|
34
61
|
//# sourceMappingURL=sync.d.ts.map
|
package/dist/sync.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,KAAK,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAOjF
|
|
1
|
+
{"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,KAAK,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAOjF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,qBAAqB,GAC7B,MAAM,IAAI,CAsFZ;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG,WAAW,CAKpF"}
|
package/dist/sync.js
CHANGED
|
@@ -13,20 +13,93 @@ const DEFAULT_TAGS = ['react-query'];
|
|
|
13
13
|
* | `updated` + `success`/`setState` | mirrored — a completed fetch or `setQueryData` |
|
|
14
14
|
* | `updated` + `invalidate` | **kept** — stale data stays so `staleTime: 0` can still paint instantly |
|
|
15
15
|
* | `updated` + `error` | ignored — TanStack owns error state in-session |
|
|
16
|
-
* | `removed` | deleted, so `removeQueries` really
|
|
16
|
+
* | `removed` after an explicit purge | deleted, so `removeQueries`/`clear` really remove it |
|
|
17
|
+
* | `removed` after garbage collection | **kept** — GC is in-session housekeeping, not a purge |
|
|
17
18
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
19
|
+
* ### Telling garbage collection from an explicit purge
|
|
20
|
+
*
|
|
21
|
+
* TanStack emits the same `removed` event for both, carrying nothing that distinguishes
|
|
22
|
+
* them: once a query has sat at zero observers for `gcTime` (5 minutes by default),
|
|
23
|
+
* `optionalRemove()` calls `queryCache.remove()` and the subscriber is none the wiser.
|
|
24
|
+
* Deleting the mirror there would gut the design — navigate away for a few minutes, come
|
|
25
|
+
* back, and every persisted query has silently vanished, taking the "instant first paint
|
|
26
|
+
* from IndexedDB" feature with it.
|
|
27
|
+
*
|
|
28
|
+
* Since the event offers no hook, the explicit APIs are wrapped instead: for the lifetime of
|
|
29
|
+
* this subscription, `client.removeQueries` and `client.clear` raise an `explicitRemoval`
|
|
30
|
+
* flag around the original call — in a `try/finally`, bound to the client so the signature
|
|
31
|
+
* and return value are untouched — and the subscriber deletes the mirror only while that
|
|
32
|
+
* flag is up (`skip` still applies). The returned unsubscribe function restores the
|
|
33
|
+
* originals verbatim, so a long-lived client is never left permanently patched; if a second
|
|
34
|
+
* sync is stacked on the same client, wrappers restore in whichever order they detach (the
|
|
35
|
+
* outermost one always puts back exactly what it found).
|
|
36
|
+
*
|
|
37
|
+
* Returns the unsubscribe function — call it to detach. {@link createBlazeQueryClient} does
|
|
38
|
+
* not need it: the listener hangs off the client's own `QueryCache`, so client, cache and
|
|
39
|
+
* subscription are collected together when you drop the client. There is also nothing to
|
|
40
|
+
* wire teardown into — TanStack's `QueryClient` has no `destroy()` method, and its real
|
|
41
|
+
* lifecycle hooks (`mount`/`unmount`) fire on every `QueryClientProvider` mount — including
|
|
42
|
+
* StrictMode's immediate re-mount — so hooking them would detach the mirror while the client
|
|
43
|
+
* very much still lives.
|
|
20
44
|
*/
|
|
21
45
|
export function syncBlazeQueryCache(client, options) {
|
|
22
46
|
const { cache } = options;
|
|
23
47
|
const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
24
48
|
const tags = options.tags ?? DEFAULT_TAGS;
|
|
25
|
-
|
|
49
|
+
// Raised only while a wrapped explicit purge is on the stack — see the doc comment above.
|
|
50
|
+
let explicitRemoval = false;
|
|
51
|
+
// Whatever sits on the client's own properties right now — usually nothing, since the
|
|
52
|
+
// methods live on `QueryClient.prototype`, but another sync stacked on this client would
|
|
53
|
+
// have left its wrapper here, and we must put that back untouched.
|
|
54
|
+
const previousRemoveQueries = Object.getOwnPropertyDescriptor(client, 'removeQueries');
|
|
55
|
+
const previousClear = Object.getOwnPropertyDescriptor(client, 'clear');
|
|
56
|
+
// Bound to the client, so the wrappers below are transparent: same signature, same return
|
|
57
|
+
// value, same private-field access as the methods they stand in for.
|
|
58
|
+
const originalRemoveQueries = client.removeQueries.bind(client);
|
|
59
|
+
const originalClear = client.clear.bind(client);
|
|
60
|
+
const patchedRemoveQueries = (filters) => {
|
|
61
|
+
explicitRemoval = true;
|
|
62
|
+
try {
|
|
63
|
+
return originalRemoveQueries(filters);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
explicitRemoval = false;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const patchedClear = () => {
|
|
70
|
+
explicitRemoval = true;
|
|
71
|
+
try {
|
|
72
|
+
return originalClear();
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
explicitRemoval = false;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
client.removeQueries = patchedRemoveQueries;
|
|
79
|
+
client.clear = patchedClear;
|
|
80
|
+
/**
|
|
81
|
+
* Puts back exactly what was on the client before this sync patched it — normally removing
|
|
82
|
+
* our own property so the prototype method shows through again. Only while our wrapper is
|
|
83
|
+
* still the outermost one: a sync attached later stacked its wrapper on top of ours, and
|
|
84
|
+
* it will restore us itself when *it* detaches, so unwinding out of order must not rip it
|
|
85
|
+
* away mid-flight.
|
|
86
|
+
*/
|
|
87
|
+
const restore = (name, wrapper, previous) => {
|
|
88
|
+
if (client[name] !== wrapper)
|
|
89
|
+
return;
|
|
90
|
+
if (previous)
|
|
91
|
+
Object.defineProperty(client, name, previous);
|
|
92
|
+
else
|
|
93
|
+
Reflect.deleteProperty(client, name);
|
|
94
|
+
};
|
|
95
|
+
const unsubscribe = client.getQueryCache().subscribe((event) => {
|
|
26
96
|
const { query } = event;
|
|
27
97
|
const hash = query.queryHash;
|
|
28
98
|
if (event.type === 'removed') {
|
|
29
|
-
|
|
99
|
+
// GC and explicit purges are indistinguishable in the event — the wrapper flag above
|
|
100
|
+
// is the only difference. Keep the mirror through GC: the persisted copy exists for
|
|
101
|
+
// the next session's first paint, not for this one's housekeeping.
|
|
102
|
+
if (explicitRemoval && !options.skip?.(query.queryKey, hash))
|
|
30
103
|
cache.delete(hash);
|
|
31
104
|
return;
|
|
32
105
|
}
|
|
@@ -42,6 +115,11 @@ export function syncBlazeQueryCache(client, options) {
|
|
|
42
115
|
return;
|
|
43
116
|
cache.set(hash, { data, dataUpdatedAt: query.state.dataUpdatedAt }, { ttlMs, tags });
|
|
44
117
|
});
|
|
118
|
+
return () => {
|
|
119
|
+
unsubscribe();
|
|
120
|
+
restore('removeQueries', patchedRemoveQueries, previousRemoveQueries);
|
|
121
|
+
restore('clear', patchedClear, previousClear);
|
|
122
|
+
};
|
|
45
123
|
}
|
|
46
124
|
/**
|
|
47
125
|
* A `QueryClient` that mirrors every successful query into a Blaze Cache.
|
|
@@ -54,8 +132,11 @@ export function syncBlazeQueryCache(client, options) {
|
|
|
54
132
|
* ```
|
|
55
133
|
*
|
|
56
134
|
* The subscription lives exactly as long as the client: the `QueryCache` holds the listener,
|
|
57
|
-
* and the client holds the cache, so they are collected together once you drop the client
|
|
58
|
-
*
|
|
135
|
+
* and the client holds the cache, so they are collected together once you drop the client —
|
|
136
|
+
* there is no `destroy()` on TanStack's `QueryClient` to wire teardown into, and none is
|
|
137
|
+
* needed. The method wrappers {@link syncBlazeQueryCache} installs live just as long and go
|
|
138
|
+
* with the client; attach by hand via {@link syncBlazeQueryCache} if you want an unsubscribe
|
|
139
|
+
* that releases them earlier.
|
|
59
140
|
*/
|
|
60
141
|
export function createBlazeQueryClient(options) {
|
|
61
142
|
const { client: clientOptions, ...syncOptions } = options;
|
package/dist/sync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAKpD,gGAAgG;AAChG,MAAM,cAAc,GAAG,UAAU,CAAC;AAClC,qFAAqF;AACrF,MAAM,YAAY,GAAsB,CAAC,aAAa,CAAC,CAAC;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MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;QAC9B,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,YAAY;YAAE,OAAO;QACxD,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC;YAAE,OAAO;QAEjD,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACvF,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,EAAE;QACV,WAAW,EAAE,CAAC;QACd,OAAO,CAAC,eAAe,EAAE,oBAAoB,EAAE,qBAAqB,CAAC,CAAC;QACtE,OAAO,CAAC,OAAO,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC;IAChD,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAgC;IACrE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC;IAC1D,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,aAAa,CAAC,CAAC;IAC9C,mBAAmB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -18,6 +18,17 @@ export interface BlazeQueryRecord<T = unknown> {
|
|
|
18
18
|
*/
|
|
19
19
|
export interface BlazeQueryCacheLike {
|
|
20
20
|
get(key: string): BlazeQueryRecord | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* The same lookup as {@link BlazeQueryCacheLike.get}, minus every side effect: no hit or miss
|
|
23
|
+
* is recorded and the entry is not marked visited for eviction.
|
|
24
|
+
*
|
|
25
|
+
* Optional, so a cache that only implements `get` keeps typechecking — {@link useBlazeQuery}
|
|
26
|
+
* falls back to the recording read for those. The concrete `BlazeCache` has it, and the hook
|
|
27
|
+
* prefers it: rendering must not be able to move the hit rate or decide who gets evicted
|
|
28
|
+
* next, and renders happen far more often than real reads (StrictMode double-renders,
|
|
29
|
+
* discarded concurrent renders).
|
|
30
|
+
*/
|
|
31
|
+
peek?(key: string): BlazeQueryRecord | undefined;
|
|
21
32
|
set(key: string, value: BlazeQueryRecord, options?: {
|
|
22
33
|
ttlMs?: number;
|
|
23
34
|
tags?: readonly string[];
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAyB,iBAAiB,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEhG;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,OAAO;IAC3C,IAAI,EAAE,CAAC,CAAC;IACR,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAC/C,GAAG,CACD,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,gBAAgB,EACvB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,GACrD,IAAI,CAAC;IACR,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC3B;AAED,kFAAkF;AAClF,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,mBAAmB,CAAC;IAE3B;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,yFAAyF;IACzF,IAAI,CAAC,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IAEf,yFAAyF;IACzF,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAEzB,qEAAqE;IACrE,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC;IAE1D,mEAAmE;IACnE,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,iFAAiF;AACjF,MAAM,WAAW,uBAAwB,SAAQ,qBAAqB;IACpE,oFAAoF;IACpF,MAAM,CAAC,EAAE,iBAAiB,CAAC;CAC5B"}
|
package/dist/useBlazeQuery.d.ts
CHANGED
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
import type { DefaultError, QueryKey, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
1
|
+
import type { DefaultError, QueryClient, QueryKey, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BlazeQueryCacheLike } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The options {@link useBlazeQuery} hands to `useQuery`: the caller's options with the Blaze
|
|
5
|
+
* Cache mirror seeded in, or the caller's options returned untouched.
|
|
6
|
+
*
|
|
7
|
+
* The whole render-time data path — hash resolution, mirror read, seeding — is a plain
|
|
8
|
+
* function, so it can be exercised without a DOM renderer (see the package's tests).
|
|
9
|
+
*
|
|
10
|
+
* The rules, in order:
|
|
11
|
+
*
|
|
12
|
+
* * A caller-supplied `initialData` **or** `initialDataUpdatedAt` is authoritative: the mirror
|
|
13
|
+
* stays out of it entirely. Its own timestamp belongs to whatever *it* mirrored and must
|
|
14
|
+
* never ride along with data the caller already dated — stamping it on fresh `initialData`
|
|
15
|
+
* would mark the caller's data ancient and force an immediate, unwanted refetch.
|
|
16
|
+
* * With neither supplied, the mirror seeds **both** fields together: the original
|
|
17
|
+
* `dataUpdatedAt` makes the first paint instant while TanStack still treats the data as
|
|
18
|
+
* stale, so the background refetch happens exactly as it would without the cache.
|
|
19
|
+
*/
|
|
20
|
+
export declare function seedBlazeQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(cache: BlazeQueryCacheLike, client: QueryClient, options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>): UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>;
|
|
3
21
|
/**
|
|
4
22
|
* `useQuery` that starts from the Blaze Cache.
|
|
5
23
|
*
|
|
6
24
|
* A previously mirrored result is injected as `initialData` **with its original
|
|
7
25
|
* `dataUpdatedAt`**, so the first render after a reload paints real data immediately and
|
|
8
26
|
* TanStack still considers it stale — the background refetch happens exactly as it would
|
|
9
|
-
* without the cache.
|
|
27
|
+
* without the cache. Caller-supplied `initialData`/`initialDataUpdatedAt` short-circuit the
|
|
28
|
+
* seeding entirely; see {@link seedBlazeQueryOptions}.
|
|
10
29
|
*
|
|
11
30
|
* ```tsx
|
|
12
31
|
* const todos = useBlazeQuery(cache, {
|
|
@@ -15,7 +34,8 @@ import type { BlazeQueryCacheLike } from './types.js';
|
|
|
15
34
|
* });
|
|
16
35
|
* ```
|
|
17
36
|
*
|
|
18
|
-
* This requires a `QueryClientProvider` above it, exactly like `useQuery
|
|
37
|
+
* This requires a `QueryClientProvider` above it, exactly like `useQuery` — the client is
|
|
38
|
+
* read from context to replay TanStack's own option defaults when resolving the lookup hash.
|
|
19
39
|
*/
|
|
20
40
|
export declare function useBlazeQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(cache: BlazeQueryCacheLike, options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>): UseQueryResult<TData, TError>;
|
|
21
41
|
//# sourceMappingURL=useBlazeQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBlazeQuery.d.ts","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,QAAQ,EACR,eAAe,EACf,cAAc,EACf,MA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,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,GAC/D,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAM/B"}
|
package/dist/useBlazeQuery.js
CHANGED
|
@@ -1,12 +1,55 @@
|
|
|
1
|
-
import { useQuery } from '@tanstack/react-query';
|
|
2
|
-
import {
|
|
1
|
+
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { resolveQueryHash } from './hash.js';
|
|
3
|
+
/**
|
|
4
|
+
* The options {@link useBlazeQuery} hands to `useQuery`: the caller's options with the Blaze
|
|
5
|
+
* Cache mirror seeded in, or the caller's options returned untouched.
|
|
6
|
+
*
|
|
7
|
+
* The whole render-time data path — hash resolution, mirror read, seeding — is a plain
|
|
8
|
+
* function, so it can be exercised without a DOM renderer (see the package's tests).
|
|
9
|
+
*
|
|
10
|
+
* The rules, in order:
|
|
11
|
+
*
|
|
12
|
+
* * A caller-supplied `initialData` **or** `initialDataUpdatedAt` is authoritative: the mirror
|
|
13
|
+
* stays out of it entirely. Its own timestamp belongs to whatever *it* mirrored and must
|
|
14
|
+
* never ride along with data the caller already dated — stamping it on fresh `initialData`
|
|
15
|
+
* would mark the caller's data ancient and force an immediate, unwanted refetch.
|
|
16
|
+
* * With neither supplied, the mirror seeds **both** fields together: the original
|
|
17
|
+
* `dataUpdatedAt` makes the first paint instant while TanStack still treats the data as
|
|
18
|
+
* stale, so the background refetch happens exactly as it would without the cache.
|
|
19
|
+
*/
|
|
20
|
+
export function seedBlazeQueryOptions(cache, client, options) {
|
|
21
|
+
// `undefined` in either slot means "not supplied" — which keeps an explicit `initialData`
|
|
22
|
+
// (including its function form) authoritative, exactly like a `??` chain would.
|
|
23
|
+
if (options.initialData !== undefined || options.initialDataUpdatedAt !== undefined) {
|
|
24
|
+
return options;
|
|
25
|
+
}
|
|
26
|
+
// Sync writes under `query.queryHash`, which honors a configured `queryKeyHashFn`; resolve
|
|
27
|
+
// the hash the same way TanStack does, or a custom hasher points the lookup at a key the
|
|
28
|
+
// mirror never used.
|
|
29
|
+
const hash = resolveQueryHash(client, options.queryKey, options);
|
|
30
|
+
// `peek` first: `get` marks the entry used for SIEVE and counts a hit/miss, so a render
|
|
31
|
+
// would mutate global cache state — StrictMode double-renders and discarded concurrent
|
|
32
|
+
// renders included — and skew the stats panel. `get` stays the fallback for caches that
|
|
33
|
+
// predate `peek` (it is optional on `BlazeQueryCacheLike` for their sake).
|
|
34
|
+
const record = cache.peek ? cache.peek(hash) : cache.get(hash);
|
|
35
|
+
if (record === undefined)
|
|
36
|
+
return options;
|
|
37
|
+
// `initialData` is declared against `TData` in one overload and `TQueryFnData` in another;
|
|
38
|
+
// the spread makes the union unreadable to the overloads, so it is asserted once here.
|
|
39
|
+
return {
|
|
40
|
+
...options,
|
|
41
|
+
initialData: record.data,
|
|
42
|
+
initialDataUpdatedAt: record.dataUpdatedAt,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
3
45
|
/**
|
|
4
46
|
* `useQuery` that starts from the Blaze Cache.
|
|
5
47
|
*
|
|
6
48
|
* A previously mirrored result is injected as `initialData` **with its original
|
|
7
49
|
* `dataUpdatedAt`**, so the first render after a reload paints real data immediately and
|
|
8
50
|
* TanStack still considers it stale — the background refetch happens exactly as it would
|
|
9
|
-
* without the cache.
|
|
51
|
+
* without the cache. Caller-supplied `initialData`/`initialDataUpdatedAt` short-circuit the
|
|
52
|
+
* seeding entirely; see {@link seedBlazeQueryOptions}.
|
|
10
53
|
*
|
|
11
54
|
* ```tsx
|
|
12
55
|
* const todos = useBlazeQuery(cache, {
|
|
@@ -15,18 +58,11 @@ import { hashQueryKey } from './hash.js';
|
|
|
15
58
|
* });
|
|
16
59
|
* ```
|
|
17
60
|
*
|
|
18
|
-
* This requires a `QueryClientProvider` above it, exactly like `useQuery
|
|
61
|
+
* This requires a `QueryClientProvider` above it, exactly like `useQuery` — the client is
|
|
62
|
+
* read from context to replay TanStack's own option defaults when resolving the lookup hash.
|
|
19
63
|
*/
|
|
20
64
|
export function useBlazeQuery(cache, options) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// `initialData` is declared against `TData` in one overload and `TQueryFnData` in another;
|
|
24
|
-
// the spread makes the union unreadable to the overloads, so it is asserted once here.
|
|
25
|
-
const seeded = {
|
|
26
|
-
...options,
|
|
27
|
-
initialData: options.initialData ?? record?.data,
|
|
28
|
-
initialDataUpdatedAt: options.initialDataUpdatedAt ?? record?.dataUpdatedAt,
|
|
29
|
-
};
|
|
30
|
-
return useQuery(seeded);
|
|
65
|
+
const client = useQueryClient();
|
|
66
|
+
return useQuery(seedBlazeQueryOptions(cache, client, options));
|
|
31
67
|
}
|
|
32
68
|
//# sourceMappingURL=useBlazeQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBlazeQuery.js","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"useBlazeQuery.js","sourceRoot":"","sources":["../src/useBlazeQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AASjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAG7C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,qBAAqB,CAMnC,KAA0B,EAC1B,MAAmB,EACnB,OAAgE;IAEhE,0FAA0F;IAC1F,gFAAgF;IAChF,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,OAAO,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;QACpF,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,2FAA2F;IAC3F,yFAAyF;IACzF,qBAAqB;IACrB,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAEjE,wFAAwF;IACxF,uFAAuF;IACvF,wFAAwF;IACxF,2EAA2E;IAC3E,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IAEzC,2FAA2F;IAC3F,uFAAuF;IACvF,OAAO;QACL,GAAG,OAAO;QACV,WAAW,EAAE,MAAM,CAAC,IAAoB;QACxC,oBAAoB,EAAE,MAAM,CAAC,aAAa;KACgB,CAAC;AAC/D,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,aAAa,CAM3B,KAA0B,EAC1B,OAAgE;IAEhE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAEhC,OAAO,QAAQ,CACb,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAC9C,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaze-cache/react-query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1-beta",
|
|
4
4
|
"description": "Persist TanStack Query v5 in WebAssembly — instant first paint after reload while stale-while-revalidate still works",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-query",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"url": "git+https://github.com/blaze-cache/blaze-cache.git",
|
|
21
21
|
"directory": "packages/react-query"
|
|
22
22
|
},
|
|
23
|
+
"homepage": "https://blaze-cache.github.io/docs/",
|
|
23
24
|
"publishConfig": {
|
|
24
25
|
"access": "public"
|
|
25
26
|
},
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"dist"
|
|
38
39
|
],
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"@blaze-cache/core": "0.
|
|
41
|
+
"@blaze-cache/core": "0.2.1-beta"
|
|
41
42
|
},
|
|
42
43
|
"peerDependencies": {
|
|
43
44
|
"@tanstack/react-query": "^5.0.0"
|
|
@@ -48,6 +49,7 @@
|
|
|
48
49
|
},
|
|
49
50
|
"scripts": {
|
|
50
51
|
"build": "tsc -p tsconfig.json",
|
|
52
|
+
"test": "pnpm run build && node --test",
|
|
51
53
|
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
52
54
|
}
|
|
53
55
|
}
|