@khanacademy/wonder-blocks-data 13.0.11 → 13.0.12
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/CHANGELOG.md +8 -0
- package/package.json +3 -3
- package/src/components/__tests__/data.test.tsx +0 -832
- package/src/components/__tests__/gql-router.test.tsx +0 -63
- package/src/components/__tests__/intercept-requests.test.tsx +0 -57
- package/src/components/__tests__/track-data.test.tsx +0 -56
- package/src/components/data.ts +0 -73
- package/src/components/gql-router.tsx +0 -63
- package/src/components/intercept-context.ts +0 -19
- package/src/components/intercept-requests.tsx +0 -67
- package/src/components/track-data.tsx +0 -28
- package/src/hooks/__tests__/__snapshots__/use-shared-cache.test.ts.snap +0 -17
- package/src/hooks/__tests__/use-cached-effect.test.tsx +0 -789
- package/src/hooks/__tests__/use-gql-router-context.test.tsx +0 -132
- package/src/hooks/__tests__/use-gql.test.tsx +0 -204
- package/src/hooks/__tests__/use-hydratable-effect.test.ts +0 -708
- package/src/hooks/__tests__/use-request-interception.test.tsx +0 -254
- package/src/hooks/__tests__/use-server-effect.test.ts +0 -293
- package/src/hooks/__tests__/use-shared-cache.test.ts +0 -263
- package/src/hooks/use-cached-effect.ts +0 -297
- package/src/hooks/use-gql-router-context.ts +0 -49
- package/src/hooks/use-gql.ts +0 -58
- package/src/hooks/use-hydratable-effect.ts +0 -201
- package/src/hooks/use-request-interception.ts +0 -53
- package/src/hooks/use-server-effect.ts +0 -75
- package/src/hooks/use-shared-cache.ts +0 -107
- package/src/index.ts +0 -46
- package/src/util/__tests__/__snapshots__/scoped-in-memory-cache.test.ts.snap +0 -19
- package/src/util/__tests__/__snapshots__/serializable-in-memory-cache.test.ts.snap +0 -19
- package/src/util/__tests__/get-gql-data-from-response.test.ts +0 -186
- package/src/util/__tests__/get-gql-request-id.test.ts +0 -132
- package/src/util/__tests__/graphql-document-node-parser.test.ts +0 -535
- package/src/util/__tests__/hydration-cache-api.test.ts +0 -34
- package/src/util/__tests__/merge-gql-context.test.ts +0 -73
- package/src/util/__tests__/purge-caches.test.ts +0 -28
- package/src/util/__tests__/request-api.test.ts +0 -176
- package/src/util/__tests__/request-fulfillment.test.ts +0 -146
- package/src/util/__tests__/request-tracking.test.tsx +0 -321
- package/src/util/__tests__/result-from-cache-response.test.ts +0 -79
- package/src/util/__tests__/scoped-in-memory-cache.test.ts +0 -316
- package/src/util/__tests__/serializable-in-memory-cache.test.ts +0 -397
- package/src/util/__tests__/ssr-cache.test.ts +0 -636
- package/src/util/__tests__/to-gql-operation.test.ts +0 -41
- package/src/util/data-error.ts +0 -63
- package/src/util/get-gql-data-from-response.ts +0 -65
- package/src/util/get-gql-request-id.ts +0 -106
- package/src/util/gql-error.ts +0 -43
- package/src/util/gql-router-context.ts +0 -9
- package/src/util/gql-types.ts +0 -64
- package/src/util/graphql-document-node-parser.ts +0 -132
- package/src/util/graphql-types.ts +0 -28
- package/src/util/hydration-cache-api.ts +0 -30
- package/src/util/merge-gql-context.ts +0 -35
- package/src/util/purge-caches.ts +0 -14
- package/src/util/request-api.ts +0 -65
- package/src/util/request-fulfillment.ts +0 -121
- package/src/util/request-tracking.ts +0 -211
- package/src/util/result-from-cache-response.ts +0 -30
- package/src/util/scoped-in-memory-cache.ts +0 -121
- package/src/util/serializable-in-memory-cache.ts +0 -44
- package/src/util/ssr-cache.ts +0 -193
- package/src/util/status.ts +0 -35
- package/src/util/to-gql-operation.ts +0 -43
- package/src/util/types.ts +0 -145
- package/tsconfig-build.json +0 -12
- package/tsconfig-build.tsbuildinfo +0 -1
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
|
|
3
|
-
import {useServerEffect} from "./use-server-effect";
|
|
4
|
-
import {useSharedCache} from "./use-shared-cache";
|
|
5
|
-
import {useCachedEffect} from "./use-cached-effect";
|
|
6
|
-
|
|
7
|
-
import {FetchPolicy} from "../util/types";
|
|
8
|
-
import type {Result, ValidCacheData} from "../util/types";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Policies to define how a hydratable effect should behave client-side.
|
|
12
|
-
*/
|
|
13
|
-
export enum WhenClientSide {
|
|
14
|
-
/**
|
|
15
|
-
* The result from executing the effect server-side will not be hydrated.
|
|
16
|
-
* The effect will always be executed client-side.
|
|
17
|
-
*
|
|
18
|
-
* This should only be used if there is something else that is responsible
|
|
19
|
-
* for properly hydrating this component (for example, the action invokes
|
|
20
|
-
* Apollo which manages its own cache to ensure things render properly).
|
|
21
|
-
*/
|
|
22
|
-
DoNotHydrate = "DoNotHydrate",
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The result from executing the effect server-side will be hydrated.
|
|
26
|
-
* The effect will only execute client-side if there was no result to
|
|
27
|
-
* be hydrated (i.e. both error and success hydration results prevent the
|
|
28
|
-
* effect running client-side).
|
|
29
|
-
*/
|
|
30
|
-
ExecuteWhenNoResult = "ExecuteWhenNoResult",
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The result from executing the effect server-side will be hydrated.
|
|
34
|
-
* If the hydrated result is a success result, the effect will not be
|
|
35
|
-
* executed client-side.
|
|
36
|
-
* If the hydrated result was not a success result, or there was no
|
|
37
|
-
* hydrated result, the effect will not be executed.
|
|
38
|
-
*/
|
|
39
|
-
ExecuteWhenNoSuccessResult = "ExecuteWhenNoSuccessResult",
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The result from executing the effect server-side will be hydrated.
|
|
43
|
-
* The effect will always be executed client-side, regardless of the
|
|
44
|
-
* hydrated result status.
|
|
45
|
-
*/
|
|
46
|
-
AlwaysExecute = "AlwaysExecute",
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
type HydratableEffectOptions<TData extends ValidCacheData> = {
|
|
50
|
-
/**
|
|
51
|
-
* How the hook should behave when rendering client-side for the first time.
|
|
52
|
-
*
|
|
53
|
-
* This controls how the hook hydrates and executes when client-side.
|
|
54
|
-
*
|
|
55
|
-
* Default is `WhenClientSide.ExecuteWhenNoSuccessResult`.
|
|
56
|
-
*
|
|
57
|
-
* Changing this value after the first call is irrelevant as it only
|
|
58
|
-
* affects the initial render behavior.
|
|
59
|
-
*/
|
|
60
|
-
clientBehavior?: typeof WhenClientSide[keyof typeof WhenClientSide];
|
|
61
|
-
/**
|
|
62
|
-
* When `true`, the effect will not be executed; otherwise, the effect will
|
|
63
|
-
* be executed.
|
|
64
|
-
*
|
|
65
|
-
* If this is set to `true` while the effect is still pending, the pending
|
|
66
|
-
* effect will be cancelled.
|
|
67
|
-
*
|
|
68
|
-
* Default is `false`.
|
|
69
|
-
*/
|
|
70
|
-
skip?: boolean;
|
|
71
|
-
/**
|
|
72
|
-
* When `true`, the effect will not reset the result to the loading status
|
|
73
|
-
* while executing if the requestId changes, instead, returning
|
|
74
|
-
* the existing result from before the change; otherwise, the result will
|
|
75
|
-
* be set to loading status.
|
|
76
|
-
*
|
|
77
|
-
* If the status is loading when the changes are made, it will remain as
|
|
78
|
-
* loading; old pending effects are discarded on changes and as such this
|
|
79
|
-
* value has no effect in that case.
|
|
80
|
-
*/
|
|
81
|
-
retainResultOnChange?: boolean;
|
|
82
|
-
/**
|
|
83
|
-
* Callback that is invoked if the result for the given hook has changed.
|
|
84
|
-
*
|
|
85
|
-
* When defined, the hook will invoke this callback whenever it has reason
|
|
86
|
-
* to change the result and will not otherwise affect component rendering
|
|
87
|
-
* directly.
|
|
88
|
-
*
|
|
89
|
-
* When not defined, the hook will ensure the component re-renders to pick
|
|
90
|
-
* up the latest result.
|
|
91
|
-
*/
|
|
92
|
-
onResultChanged?: (result: Result<TData>) => void;
|
|
93
|
-
/**
|
|
94
|
-
* Scope to use with the shared cache.
|
|
95
|
-
*
|
|
96
|
-
* When specified, the given scope will be used to isolate this hook's
|
|
97
|
-
* cached results. Otherwise, a shared default scope will be used.
|
|
98
|
-
*
|
|
99
|
-
* Changing this value after the first call is not supported.
|
|
100
|
-
*/
|
|
101
|
-
scope?: string;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const DefaultScope = "useHydratableEffect";
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Hook to execute an async operation on server and client.
|
|
108
|
-
*
|
|
109
|
-
* This hook executes the given handler on the server and on the client,
|
|
110
|
-
* and, depending on the given options, can hydrate the server-side result.
|
|
111
|
-
*
|
|
112
|
-
* Results are cached on the client so they can be shared between equivalent
|
|
113
|
-
* invocations. Cache changes from one hook instance do not trigger renders
|
|
114
|
-
* in components that use the same requestID.
|
|
115
|
-
*/
|
|
116
|
-
export const useHydratableEffect = <TData extends ValidCacheData>(
|
|
117
|
-
requestId: string,
|
|
118
|
-
handler: () => Promise<TData>,
|
|
119
|
-
options: HydratableEffectOptions<TData> = {} as Partial<
|
|
120
|
-
HydratableEffectOptions<TData>
|
|
121
|
-
>,
|
|
122
|
-
): Result<TData> => {
|
|
123
|
-
const {
|
|
124
|
-
clientBehavior = WhenClientSide.ExecuteWhenNoSuccessResult,
|
|
125
|
-
skip = false,
|
|
126
|
-
retainResultOnChange = false,
|
|
127
|
-
onResultChanged,
|
|
128
|
-
scope = DefaultScope,
|
|
129
|
-
} = options;
|
|
130
|
-
|
|
131
|
-
// Now we instruct the server to perform the operation.
|
|
132
|
-
// When client-side, this will look up any response for hydration; it does
|
|
133
|
-
// not invoke the handler.
|
|
134
|
-
const serverResult = useServerEffect(requestId, handler, {
|
|
135
|
-
// Only hydrate if our behavior isn't telling us not to.
|
|
136
|
-
hydrate: clientBehavior !== WhenClientSide.DoNotHydrate,
|
|
137
|
-
skip,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const getDefaultCacheValue: () => Result<TData> | null | undefined =
|
|
141
|
-
React.useCallback(() => {
|
|
142
|
-
// If we don't have a requestId, it's our first render, the one
|
|
143
|
-
// where we hydrated. So defer to our clientBehavior value.
|
|
144
|
-
switch (clientBehavior) {
|
|
145
|
-
case WhenClientSide.DoNotHydrate:
|
|
146
|
-
case WhenClientSide.AlwaysExecute:
|
|
147
|
-
// Either we weren't hydrating at all, or we don't care
|
|
148
|
-
// if we hydrated something or not, either way, we're
|
|
149
|
-
// doing a request.
|
|
150
|
-
return null;
|
|
151
|
-
|
|
152
|
-
case WhenClientSide.ExecuteWhenNoResult:
|
|
153
|
-
// We only execute if we didn't hydrate something.
|
|
154
|
-
// So, returning the hydration result as default for our
|
|
155
|
-
// cache, will then prevent the cached effect running.
|
|
156
|
-
return serverResult;
|
|
157
|
-
|
|
158
|
-
case WhenClientSide.ExecuteWhenNoSuccessResult:
|
|
159
|
-
// We only execute if we didn't hydrate a success result.
|
|
160
|
-
if (serverResult?.status === "success") {
|
|
161
|
-
// So, returning the hydration result as default for our
|
|
162
|
-
// cache, will then prevent the cached effect running.
|
|
163
|
-
return serverResult;
|
|
164
|
-
}
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
// There is no reason for this to change after the first render,
|
|
168
|
-
// you might think, but the function closes around serverResult and if
|
|
169
|
-
// the requestId changes, it still returns the hydrate result of the
|
|
170
|
-
// first render of the previous requestId. This then means that the
|
|
171
|
-
// hydrate result is still the same, and the effect is not re-executed
|
|
172
|
-
// because the cache gets incorrectly defaulted.
|
|
173
|
-
// However, we don't want to bother doing anything with this on
|
|
174
|
-
// client behavior changing since that truly is irrelevant.
|
|
175
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
176
|
-
}, [serverResult]);
|
|
177
|
-
|
|
178
|
-
// Instead of using state, which would be local to just this hook instance,
|
|
179
|
-
// we use a shared in-memory cache.
|
|
180
|
-
useSharedCache<Result<TData>>( // The key of the cached item
|
|
181
|
-
requestId, // The scope of the cached items
|
|
182
|
-
scope,
|
|
183
|
-
getDefaultCacheValue,
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
// When we're client-side, we ultimately want the result from this call.
|
|
187
|
-
const [clientResult] = useCachedEffect(requestId, handler, {
|
|
188
|
-
skip,
|
|
189
|
-
onResultChanged,
|
|
190
|
-
retainResultOnChange,
|
|
191
|
-
scope,
|
|
192
|
-
// Be explicit about our fetch policy for clarity.
|
|
193
|
-
fetchPolicy: FetchPolicy.CacheBeforeNetwork,
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
// OK, now which result do we return.
|
|
197
|
-
// Well, we return the serverResult on our very first call and then
|
|
198
|
-
// the clientResult thereafter. The great thing is that after the very
|
|
199
|
-
// first call, the serverResult is going to be `null` anyway.
|
|
200
|
-
return serverResult ?? clientResult;
|
|
201
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
|
|
3
|
-
import InterceptContext from "../components/intercept-context";
|
|
4
|
-
import type {ValidCacheData} from "../util/types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Allow request handling to be intercepted.
|
|
8
|
-
*
|
|
9
|
-
* Hook to take a uniquely identified request handler and return a
|
|
10
|
-
* method that will support request interception from the InterceptRequest
|
|
11
|
-
* component.
|
|
12
|
-
*
|
|
13
|
-
* If you want request interception to be supported with `useServerEffect` or
|
|
14
|
-
* any client-side effect that uses the handler, call this first to generate
|
|
15
|
-
* an intercepted handler, and then invoke `useServerEffect` (or other things)
|
|
16
|
-
* with that intercepted handler.
|
|
17
|
-
*/
|
|
18
|
-
export const useRequestInterception = <TData extends ValidCacheData>(
|
|
19
|
-
requestId: string,
|
|
20
|
-
handler: () => Promise<TData>,
|
|
21
|
-
): (() => Promise<TData>) => {
|
|
22
|
-
// Get the interceptors that have been registered.
|
|
23
|
-
const interceptors = React.useContext(InterceptContext);
|
|
24
|
-
|
|
25
|
-
// Now, we need to create a new handler that will check if the
|
|
26
|
-
// request is intercepted before ultimately calling the original handler
|
|
27
|
-
// if nothing intercepted it.
|
|
28
|
-
// We memoize this so that it only changes if something related to it
|
|
29
|
-
// changes.
|
|
30
|
-
const interceptedHandler = React.useCallback((): Promise<TData> => {
|
|
31
|
-
// Call the interceptors from closest to furthest.
|
|
32
|
-
// If one returns a non-null result, then we keep that.
|
|
33
|
-
const interceptResponse: Promise<TData> | null | undefined =
|
|
34
|
-
interceptors.reduceRight(
|
|
35
|
-
(prev: Promise<TData> | null | undefined, interceptor) => {
|
|
36
|
-
if (prev != null) {
|
|
37
|
-
return prev;
|
|
38
|
-
}
|
|
39
|
-
return interceptor(requestId) as
|
|
40
|
-
| Promise<TData>
|
|
41
|
-
| null
|
|
42
|
-
| undefined;
|
|
43
|
-
},
|
|
44
|
-
null,
|
|
45
|
-
);
|
|
46
|
-
// If nothing intercepted this request, invoke the original handler.
|
|
47
|
-
// NOTE: We can't guarantee all interceptors return the same type
|
|
48
|
-
// as our handler, so how can TypeScript know? Let's just suppress that.
|
|
49
|
-
return interceptResponse ?? handler();
|
|
50
|
-
}, [handler, interceptors, requestId]);
|
|
51
|
-
|
|
52
|
-
return interceptedHandler;
|
|
53
|
-
};
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
2
|
-
import {useContext} from "react";
|
|
3
|
-
import {TrackerContext} from "../util/request-tracking";
|
|
4
|
-
import {SsrCache} from "../util/ssr-cache";
|
|
5
|
-
import {resultFromCachedResponse} from "../util/result-from-cache-response";
|
|
6
|
-
import {useRequestInterception} from "./use-request-interception";
|
|
7
|
-
|
|
8
|
-
import type {Result, ValidCacheData} from "../util/types";
|
|
9
|
-
|
|
10
|
-
type ServerEffectOptions = {
|
|
11
|
-
/**
|
|
12
|
-
* When `true`, the result of the effect when fulfilled using Wonder Blocks
|
|
13
|
-
* Data will be stored in the hydration cache for hydrating client-side;
|
|
14
|
-
* otherwise, the result will be stored in the server-side-only cache.
|
|
15
|
-
*
|
|
16
|
-
* This should only be set to `false` if something else will be responsible
|
|
17
|
-
* for hydration of the data on the client-side (for example, if Apollo's
|
|
18
|
-
* hydration support is used).
|
|
19
|
-
*
|
|
20
|
-
* Default is `true`.
|
|
21
|
-
*/
|
|
22
|
-
hydrate?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* When `true`, the effect will not be tracked for fulfillment; otherwise,
|
|
25
|
-
* the effect will be tracked for fulfillment.
|
|
26
|
-
*
|
|
27
|
-
* Default is `false`.
|
|
28
|
-
*/
|
|
29
|
-
skip?: boolean;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Hook to perform an asynchronous action during server-side rendering.
|
|
34
|
-
*
|
|
35
|
-
* This hook registers an asynchronous action to be performed during
|
|
36
|
-
* server-side rendering. The action is performed only once, and the result
|
|
37
|
-
* is cached against the given identifier so that subsequent calls return that
|
|
38
|
-
* cached result allowing components to render more of the component.
|
|
39
|
-
*
|
|
40
|
-
* This hook requires the Wonder Blocks Data functionality for resolving
|
|
41
|
-
* pending requests, as well as support for the hydration cache to be
|
|
42
|
-
* embedded into a page so that the result can by hydrated (if that is a
|
|
43
|
-
* requirement).
|
|
44
|
-
*
|
|
45
|
-
* The asynchronous action is never invoked on the client-side.
|
|
46
|
-
*/
|
|
47
|
-
export const useServerEffect = <TData extends ValidCacheData>(
|
|
48
|
-
requestId: string,
|
|
49
|
-
handler: () => Promise<TData>,
|
|
50
|
-
options: ServerEffectOptions = {} as Partial<ServerEffectOptions>,
|
|
51
|
-
): Result<TData> | null | undefined => {
|
|
52
|
-
const {hydrate = true, skip = false} = options;
|
|
53
|
-
|
|
54
|
-
// Plug in to the request interception framework for code that wants
|
|
55
|
-
// to use that.
|
|
56
|
-
const interceptedHandler = useRequestInterception(requestId, handler);
|
|
57
|
-
|
|
58
|
-
// If we're server-side or hydrating, we'll have a cached entry to use.
|
|
59
|
-
// So we get that and use it to initialize our state.
|
|
60
|
-
// This works in both hydration and SSR because the very first call to
|
|
61
|
-
// this will have cached data in those cases as it will be present on the
|
|
62
|
-
// initial render - and subsequent renders on the client it will be null.
|
|
63
|
-
const cachedResult = SsrCache.Default.getEntry<TData>(requestId);
|
|
64
|
-
|
|
65
|
-
// We only track data requests when we are server-side, we are not skipping
|
|
66
|
-
// the request, and we don't already have a result, as given by the
|
|
67
|
-
// cachedData (which is also the initial value for the result state).
|
|
68
|
-
const maybeTrack = useContext(TrackerContext);
|
|
69
|
-
if (!skip && cachedResult == null && Server.isServerSide()) {
|
|
70
|
-
maybeTrack?.(requestId, interceptedHandler, hydrate);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// A null result means there was no result to hydrate.
|
|
74
|
-
return cachedResult == null ? null : resultFromCachedResponse(cachedResult);
|
|
75
|
-
};
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import {DataError, DataErrors} from "../util/data-error";
|
|
3
|
-
import {ScopedInMemoryCache} from "../util/scoped-in-memory-cache";
|
|
4
|
-
import type {ValidCacheData, ScopedCache} from "../util/types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A function for inserting a value into the cache or clearing it.
|
|
8
|
-
*/
|
|
9
|
-
type CacheValueFn<TValue extends ValidCacheData> = (
|
|
10
|
-
value?: TValue | null | undefined,
|
|
11
|
-
) => void;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* This is the cache.
|
|
15
|
-
* It's incredibly complex.
|
|
16
|
-
* Very in-memory. So cache. Such complex. Wow.
|
|
17
|
-
*/
|
|
18
|
-
const cache = new ScopedInMemoryCache();
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Access to the shared in-memory cache.
|
|
22
|
-
*
|
|
23
|
-
* This is the cache used by `useSharedCache` and related hooks and
|
|
24
|
-
* components.
|
|
25
|
-
*/
|
|
26
|
-
export const SharedCache: ScopedCache = cache;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Hook to retrieve data from and store data in an in-memory cache.
|
|
30
|
-
*
|
|
31
|
-
* @returns {[?ReadOnlyCacheValue, CacheValueFn]}
|
|
32
|
-
* Returns an array containing the current cache entry (or undefined), a
|
|
33
|
-
* function to set the cache entry (passing null or undefined to this function
|
|
34
|
-
* will delete the entry).
|
|
35
|
-
*
|
|
36
|
-
* NOTE: Unlike useState or useReducer, we don't automatically update folks
|
|
37
|
-
* if the value they reference changes. We might add it later (if we need to),
|
|
38
|
-
* but the likelihood here is that things won't be changing in this cache in a
|
|
39
|
-
* way where we would need that. If we do (and likely only in specific
|
|
40
|
-
* circumstances), we should consider adding a simple boolean useState that can
|
|
41
|
-
* be toggled to cause a rerender whenever the referenced cached data changes
|
|
42
|
-
* so that callers can re-render on cache changes. However, we should make
|
|
43
|
-
* sure this toggling is optional - or we could use a callback argument, to
|
|
44
|
-
* achieve this on an as-needed basis.
|
|
45
|
-
*/
|
|
46
|
-
export const useSharedCache = <TValue extends ValidCacheData>(
|
|
47
|
-
id: string,
|
|
48
|
-
scope: string,
|
|
49
|
-
initialValue?:
|
|
50
|
-
| TValue
|
|
51
|
-
| null
|
|
52
|
-
| undefined
|
|
53
|
-
| (() => TValue | null | undefined),
|
|
54
|
-
): [TValue | null | undefined, CacheValueFn<TValue>] => {
|
|
55
|
-
// Verify arguments.
|
|
56
|
-
if (!id || typeof id !== "string") {
|
|
57
|
-
throw new DataError(
|
|
58
|
-
"id must be a non-empty string",
|
|
59
|
-
DataErrors.InvalidInput,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (!scope || typeof scope !== "string") {
|
|
64
|
-
throw new DataError(
|
|
65
|
-
"scope must be a non-empty string",
|
|
66
|
-
DataErrors.InvalidInput,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Memoize our APIs.
|
|
71
|
-
// This one allows callers to set or replace the cached value.
|
|
72
|
-
const cacheValue = React.useCallback(
|
|
73
|
-
(value?: TValue | null) =>
|
|
74
|
-
value == null
|
|
75
|
-
? cache.purge(scope, id)
|
|
76
|
-
: cache.set(scope, id, value),
|
|
77
|
-
[id, scope],
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
// We don't memo-ize the current value, just in case the cache was updated
|
|
81
|
-
// since our last run through. Also, our cache does not know what type it
|
|
82
|
-
// stores, so we have to cast it to the type we're exporting. This is a
|
|
83
|
-
// dev time courtesy, rather than a runtime thing.
|
|
84
|
-
let currentValue: TValue | null | undefined = cache.get(scope, id) as
|
|
85
|
-
| TValue
|
|
86
|
-
| null
|
|
87
|
-
| undefined;
|
|
88
|
-
|
|
89
|
-
// If we have an initial value, we need to add it to the cache
|
|
90
|
-
// and use it as our current value.
|
|
91
|
-
if (currentValue == null && initialValue !== undefined) {
|
|
92
|
-
// Get the initial value.
|
|
93
|
-
const value =
|
|
94
|
-
typeof initialValue === "function" ? initialValue() : initialValue;
|
|
95
|
-
|
|
96
|
-
if (value != null) {
|
|
97
|
-
// Update the cache.
|
|
98
|
-
cacheValue(value);
|
|
99
|
-
|
|
100
|
-
// Make sure we return this value as our current value.
|
|
101
|
-
currentValue = value;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Now we have everything, let's return it.
|
|
106
|
-
return [currentValue, cacheValue];
|
|
107
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
export {FetchPolicy} from "./util/types";
|
|
2
|
-
export type {
|
|
3
|
-
ErrorOptions,
|
|
4
|
-
ResponseCache,
|
|
5
|
-
CachedResponse,
|
|
6
|
-
Result,
|
|
7
|
-
RawScopedCache,
|
|
8
|
-
ValidCacheData,
|
|
9
|
-
ScopedCache,
|
|
10
|
-
} from "./util/types";
|
|
11
|
-
|
|
12
|
-
export * from "./util/hydration-cache-api";
|
|
13
|
-
export * from "./util/request-api";
|
|
14
|
-
export {purgeCaches} from "./util/purge-caches";
|
|
15
|
-
export {default as TrackData} from "./components/track-data";
|
|
16
|
-
export {default as Data} from "./components/data";
|
|
17
|
-
export {default as InterceptRequests} from "./components/intercept-requests";
|
|
18
|
-
export {DataError, DataErrors} from "./util/data-error";
|
|
19
|
-
export {useServerEffect} from "./hooks/use-server-effect";
|
|
20
|
-
export {useCachedEffect} from "./hooks/use-cached-effect";
|
|
21
|
-
export {useSharedCache, SharedCache} from "./hooks/use-shared-cache";
|
|
22
|
-
export {
|
|
23
|
-
useHydratableEffect,
|
|
24
|
-
WhenClientSide,
|
|
25
|
-
} from "./hooks/use-hydratable-effect";
|
|
26
|
-
export {ScopedInMemoryCache} from "./util/scoped-in-memory-cache";
|
|
27
|
-
export {SerializableInMemoryCache} from "./util/serializable-in-memory-cache";
|
|
28
|
-
export {Status} from "./util/status";
|
|
29
|
-
|
|
30
|
-
////////////////////////////////////////////////////////////////////////////////
|
|
31
|
-
// GraphQL
|
|
32
|
-
////////////////////////////////////////////////////////////////////////////////
|
|
33
|
-
export {getGqlRequestId} from "./util/get-gql-request-id";
|
|
34
|
-
export {getGqlDataFromResponse} from "./util/get-gql-data-from-response";
|
|
35
|
-
export {graphQLDocumentNodeParser} from "./util/graphql-document-node-parser";
|
|
36
|
-
export {toGqlOperation} from "./util/to-gql-operation";
|
|
37
|
-
export {GqlRouter} from "./components/gql-router";
|
|
38
|
-
export {useGql} from "./hooks/use-gql";
|
|
39
|
-
export {GqlError, GqlErrors} from "./util/gql-error";
|
|
40
|
-
export type {
|
|
41
|
-
GqlContext,
|
|
42
|
-
GqlOperation,
|
|
43
|
-
GqlOperationType,
|
|
44
|
-
GqlFetchOptions,
|
|
45
|
-
GqlFetchFn,
|
|
46
|
-
} from "./util/gql-types";
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`ScopedInMemoryCache #set should throw if the id is 1`] = `"id must be non-empty string"`;
|
|
4
|
-
|
|
5
|
-
exports[`ScopedInMemoryCache #set should throw if the id is [Function anonymous] 1`] = `"id must be non-empty string"`;
|
|
6
|
-
|
|
7
|
-
exports[`ScopedInMemoryCache #set should throw if the id is 5 1`] = `"id must be non-empty string"`;
|
|
8
|
-
|
|
9
|
-
exports[`ScopedInMemoryCache #set should throw if the id is null 1`] = `"id must be non-empty string"`;
|
|
10
|
-
|
|
11
|
-
exports[`ScopedInMemoryCache #set should throw if the scope is 1`] = `"scope must be non-empty string"`;
|
|
12
|
-
|
|
13
|
-
exports[`ScopedInMemoryCache #set should throw if the scope is [Function anonymous] 1`] = `"scope must be non-empty string"`;
|
|
14
|
-
|
|
15
|
-
exports[`ScopedInMemoryCache #set should throw if the scope is 5 1`] = `"scope must be non-empty string"`;
|
|
16
|
-
|
|
17
|
-
exports[`ScopedInMemoryCache #set should throw if the scope is null 1`] = `"scope must be non-empty string"`;
|
|
18
|
-
|
|
19
|
-
exports[`ScopedInMemoryCache #set should throw if the value is a function 1`] = `"value must be a non-function value"`;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`SerializableInMemoryCache #set should throw if the id is 1`] = `"id must be non-empty string"`;
|
|
4
|
-
|
|
5
|
-
exports[`SerializableInMemoryCache #set should throw if the id is [Function anonymous] 1`] = `"id must be non-empty string"`;
|
|
6
|
-
|
|
7
|
-
exports[`SerializableInMemoryCache #set should throw if the id is 5 1`] = `"id must be non-empty string"`;
|
|
8
|
-
|
|
9
|
-
exports[`SerializableInMemoryCache #set should throw if the id is null 1`] = `"id must be non-empty string"`;
|
|
10
|
-
|
|
11
|
-
exports[`SerializableInMemoryCache #set should throw if the scope is 1`] = `"scope must be non-empty string"`;
|
|
12
|
-
|
|
13
|
-
exports[`SerializableInMemoryCache #set should throw if the scope is [Function anonymous] 1`] = `"scope must be non-empty string"`;
|
|
14
|
-
|
|
15
|
-
exports[`SerializableInMemoryCache #set should throw if the scope is 5 1`] = `"scope must be non-empty string"`;
|
|
16
|
-
|
|
17
|
-
exports[`SerializableInMemoryCache #set should throw if the scope is null 1`] = `"scope must be non-empty string"`;
|
|
18
|
-
|
|
19
|
-
exports[`SerializableInMemoryCache #set should throw if the value is a function 1`] = `"value must be a non-function value"`;
|