Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

(request-policy) - Fix conditions and time of upgrading operations (#1377)

* (request-policy) - Fix conditions and time of upgrading operations

* Add changeset

* Fix linting issues

authored by

Phil Pluckthun and committed by
GitHub
a861de8d 4f259bb6

+27 -12
+5
.changeset/small-apricots-exercise.md
··· 1 + --- 2 + '@urql/exchange-request-policy': patch 3 + --- 4 + 5 + Fix non-query operations being upgraded by `requestPolicyExchange` and time being stored by last issuance rather than last result.
+22 -12
exchanges/request-policy/src/requestPolicyExchange.ts
··· 1 - import { makeOperation, Operation, Exchange } from '@urql/core'; 2 - import { pipe, map } from 'wonka'; 1 + import { 2 + makeOperation, 3 + Operation, 4 + OperationResult, 5 + Exchange, 6 + } from '@urql/core'; 7 + import { pipe, tap, map } from 'wonka'; 3 8 4 9 const defaultTTL = 5 * 60 * 1000; 5 10 ··· 12 17 forward, 13 18 }) => { 14 19 const operations = new Map(); 15 - 16 20 const TTL = (options || {}).ttl || defaultTTL; 17 21 18 22 const processIncomingOperation = (operation: Operation): Operation => { 19 23 if ( 20 - operation.context.requestPolicy !== 'cache-first' && 21 - operation.context.requestPolicy !== 'cache-only' 22 - ) 23 - return operation; 24 - if (!operations.has(operation.key)) { 25 - operations.set(operation.key, new Date()); 24 + operation.kind !== 'query' || 25 + (operation.context.requestPolicy !== 'cache-first' && 26 + operation.context.requestPolicy !== 'cache-only') || 27 + !operations.has(operation.key) 28 + ) { 26 29 return operation; 27 30 } 28 31 29 32 const lastOccurrence = operations.get(operation.key); 30 33 const currentTime = new Date().getTime(); 31 34 if ( 32 - currentTime - lastOccurrence.getTime() > TTL && 35 + currentTime - lastOccurrence > TTL && 33 36 (options.shouldUpgrade ? options.shouldUpgrade(operation) : true) 34 37 ) { 35 - operations.set(operation.key, new Date()); 38 + operations.delete(operation.key); 36 39 return makeOperation(operation.kind, operation, { 37 40 ...operation.context, 38 41 requestPolicy: 'cache-and-network', ··· 42 45 return operation; 43 46 }; 44 47 48 + const processIncomingResults = (result: OperationResult): void => { 49 + operations.set(result.operation.key, new Date().getTime()); 50 + }; 51 + 45 52 return ops$ => { 46 - return forward(pipe(ops$, map(processIncomingOperation))); 53 + return pipe( 54 + forward(pipe(ops$, map(processIncomingOperation))), 55 + tap(processIncomingResults) 56 + ); 47 57 }; 48 58 };