Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 2x 2x 2x 35x 35x 35x 35x 35x 35x 35x 31x 31x 5x 4x 1x 1x 1x 1x 31x 5x 5x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 4x 27x 27x 12x 12x 12x 4x 15x 15x 3x 3x 3x 3x 27x 2x 2x 2x 2x 2x 27x 2x 2x 27x 7x 7x 20x 20x 20x 5x 5x 1x 1x 1x | import { useEffect, useState } from 'react';
import {
AccessorQueryCacheStore,
AccessorQueryCacheStoreSet,
AccessorQueryConfiguration,
AccessorQueryContext,
AccessorQueryResult,
AccessorQueryResultCacheSetResult,
ErrorTimedOut,
} from './types';
/**
* Use a accessor query hook.
*
* This hook utilizes `React.Suspense`, `React.ErrorBoundary`, and proxies to make data querying easier on the client.
*
* A "accessor request", which is what this hook is, enables the following:
* 1. The client invokes the hook.
* 2. The result cache is read to see if the client has already completed this request, if so provide the accessor result.
* 3. Otherwise provide the accessor result with a proxy that will "execute" (throw for suspense) the query request when accessed.
* 4. The proxy can be invoked by client code.
* 5. Trigger suspense to handle executing the query request. Wait until the query request is completed.
* 6. The data cache is then set with the query request response.
* 7. Suspense will allow rendering to continue again.
* 8. The client will invoke the hook again.
* 9. Where the request cache is now populated and read.
*
* A "query request", also known as a "query" to clients, is where our data comes from for the accessor.
* The API is a promise but the expectation is that it's data access. So it should **only** retreive data and manage it's data cache.
*
* A "accessor clear" is provided in the accessor result and will do the following:
* 1. Clear the current accessor request cache by calling `configuration.cache.clear`.
*
* A "accessor invalidate" is provided in the accessor result and will do the following:
* 1. Clear the current accessor request cache by calling `configuration.cache.clear` with the accessor request arguments.
* 2. Run the accessor request again with the cache now cleared.
*
* @param.configuration.cache.duration - How long, in milliseconds, the accessor cache should be used for before being invalidated.
* @param.configuration.cache.isPrimableFromCache <true> - Determines if the accessor can be primed from the cache of other data accessors.
* Once primed the `configuration.cacheDuration` will be respected.
* @param.configuration.cache.id - Synchrounous get function used to reference the accessor cache.
* @param.configuration.cache.set - Synchrounous set function used to assign the query's response to cache.
* @param.configuration.cache.get - Synchrounous get function used to construct the accessor response from cache.
*
* @param configuration.cache.invalidate.auto.enable <undefined> - Determine if the accessor should invalidate automatically.
* @param configuration.cache.invalidate.auto.interval <configuration.cache.duration> - How long, in milliseconds, the cache should be invalidated.
*
* @param.configuration.constraints.enforce <undefined> - Determine if the accessor should throw when the constraints aren't met.
* @param.configuration.constraints.maxDelay <undefined> - When provided the accessor will be timed.
* To see timings you must have `debug` enabled.
* If `enforce` is enabled then the accessor will
* throw if the accessor can't resolve data within the
* `maxDelay`.
*
* @param.configuration.debug <false> - Turns logging on or off.
* @param configuration.query - Asynchrounous get function used to populate the accessor cache.
*
* @returns (cache: TCache, args: TQueryRequest) => AccessorQueryResult<TData>
* @param cache - Where to store cache data for data access.
* @param args - Arguments to send to the query.
*
* @example
* ```
* // use_get_book.tsx
* const accessor = createAccessor({ ... });
* const useGetBook = useQuery(accessor, { ... });
*
* // use_get_book_reports.tsx
* const accessor = createAccessor({ ... });
* const useGetBookReports = useQuery(accessor, { ... });
*
* // BookReports.tsx
* const BookReports = ({ id }) => {
* const { data: book } = useGetBook({ id });
* const { data: bookReports } = useGetBookReports({ pageId: page.id });
* return <>{bookReports}</>;
* };
* ```
*/
export const useQuery = <
TCache extends AccessorQueryCacheStore,
TQueryRequest,
TQueryResponse,
TData,
>(
accessor: {
configuration: AccessorQueryConfiguration<
TCache,
TQueryRequest,
TQueryResponse,
TData
>;
context: AccessorQueryContext<TCache, TQueryRequest, TQueryResponse, TData>;
},
getCacheStore: () => TCache,
args: TQueryRequest,
): AccessorQueryResult<TData> => {
// Configure the accessor.
const configuration = accessor.configuration;
const context = accessor.context;
// Configuration.
const debug = configuration.debug || false;
const constraints = configuration.constraints || null;
const cache = configuration.cache;
// Context.
const { queryName, requestQueryPromise, suspenseRequestQuery } = context;
// Trigger a requery/rerender - Used when invalidating the accessor request.
const [_, setTriggerRender] = useState<boolean>(false);
const requery = () => setTriggerRender(v => !v);
// Cache auto invalidation.
useEffect(() => {
if (
!cache.invalidate ||
!cache.invalidate.auto ||
!cache.invalidate.auto.enable
) {
return;
}
const interval = setInterval(() => {
invalidate();
}, cache.invalidate.auto.interval || cache.duration);
return () => {
clearInterval(interval);
};
}, [cache.invalidate]);
// Are we profiling?
let finishProfiling: undefined | (() => void);
if (constraints && typeof constraints.maxDelay === 'number') {
// Start profiling.
const timeStart = window.performance.now();
finishProfiling = () => {
const timeEnd = window.performance.now();
const duration = timeEnd - timeStart;
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request took ${duration}ms`,
);
if (
typeof constraints.maxDelay === 'number' &&
duration > constraints.maxDelay
) {
debug &&
console.error(
`[data-access:${queryName}:${cacheId}] request timed out! Took ${
duration - constraints.maxDelay
}ms longer than expected.`,
);
if (constraints.enforce) {
throw new ErrorTimedOut(queryName, cacheId);
}
}
};
}
// Do we have a cached result?
const {
dataAccess: { set, query: cacheQuery },
} = getCacheStore();
const cacheId = cache.id({ args });
debug &&
console.log(`[data-access:${queryName}:${cacheId}] execute request.`);
debug &&
console.log(`[data-access:${queryName}:${cacheId}] read data cache.`);
const cacheQueryRequest = cacheQuery.get(cacheId);
// Do we have the data for this request already?
const cacheResult = cache.get({
cacheId: cacheId,
cache: getCacheStore,
args,
request: (args: TQueryRequest) => requestQueryPromise(getCacheStore, args),
});
// Does our library have any errors that occurred?
if (cacheQueryRequest instanceof Error) {
throw cacheQueryRequest;
}
// Did we make a request for this data already?
let result: null | AccessorQueryResultCacheSetResult<TData> = null;
if (
cacheQueryRequest &&
(cacheQueryRequest.promise as any).state.isQueryFinished
) {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request query cache found.`,
);
// Do we need to invalidate the cache and request?
Iif (Date.now() > cacheQueryRequest.cacheTimeToRefresh) {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request cache expired.`,
);
// Cache reset for query & suspense.
set(({ dataAccess: { suspense: cacheSuspense, query: cacheQuery } }) => {
cacheQuery.delete(cacheId);
cacheSuspense.delete(cacheId);
});
} else {
if (cacheResult) {
result = cacheResult;
}
}
} else {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request query cache *not* found.`,
);
// We haven't made this request before but can we prime it from cache?
if (cache.isPrimableFromCache && cacheResult) {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request query cache primed from data cache.`,
);
// Cache with resolved cache result.
set(({ dataAccess: { query: cacheQuery } }) => {
cacheQuery.set(cacheId, {
cacheTimeToRefresh: Date.now() + cache.duration,
// NOTE: This request cache promise is never checked/used.
// Only it's time to live property is relevant.
promise: null,
});
});
result = cacheResult;
}
}
// Allow accessor result to clear.
const clear = () => {
const {
dataAccess: { set },
} = getCacheStore();
// Clear accessor cache for requests.
set(({ dataAccess: { suspense: cacheSuspense, query: cacheQuery } }) => {
cacheQuery.delete(cacheId);
cacheSuspense.delete(cacheId);
});
// Clear accessor cache for data.
cache.clear({
cacheId,
args,
// Stored as correct setter, cast semi-safely.
set: set as (store: AccessorQueryCacheStoreSet<TCache>) => any,
});
};
// Allow accessor result to invalidate.
const invalidate = () => {
clear();
requery();
};
if (result) {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] request data cache used.`,
);
return {
...result,
clear,
invalidate,
};
} else {
// Tell suspense about promise to request backend data.
// Since this is a proxy designed to request data on access treat this as the real thing.
return {
data: new Proxy(
{},
{
get() {
debug &&
console.log(
`[data-access:${queryName}:${cacheId}] execute suspense!`,
);
// Wrap promise in suspense format.
return suspenseRequestQuery({
getCacheStore,
onDone: () => {
// NOTE: When an error occurs after we've assigned the cache the cache result would be returned.
// But here we make sure that the cache returns an error.
try {
finishProfiling && finishProfiling();
} catch (e) {
if (e instanceof Error) {
set(({ dataAccess: { query: cacheQuery } }) => {
cacheQuery.set(cacheId, e as Error);
});
} else E{
// Should never happen.
console.error(
'[data-access]: A error object was not an error.',
);
}
}
},
args,
})();
},
},
) as TData,
clear,
invalidate,
};
}
};
|