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.

fix(request-policy): avoid relying on cache internals to assert misses (#3521)

authored by

Jovi De Croock and committed by
GitHub
ef5d0093 d2c74417

+20 -3
+5
.changeset/plenty-elephants-lay.md
··· 1 + --- 2 + '@urql/exchange-request-policy': minor 3 + --- 4 + 5 + Change the request-policy exchange not to rely on OperationMeta set by the cache exchanges
+15 -3
exchanges/request-policy/src/requestPolicyExchange.ts
··· 57 57 ({ forward }) => { 58 58 const operations = new Map(); 59 59 const TTL = (options || {}).ttl || defaultTTL; 60 + const dispatched = new Map<number, number>(); 61 + let counter = 0; 60 62 61 63 const processIncomingOperation = (operation: Operation): Operation => { 62 64 if ( ··· 68 70 } 69 71 70 72 const currentTime = new Date().getTime(); 73 + // When an operation passes by we track the current time 74 + dispatched.set(operation.key, counter); 75 + queueMicrotask(() => { 76 + counter = (counter + 1) | 0; 77 + }); 71 78 const lastOccurrence = operations.get(operation.key) || 0; 72 79 if ( 73 80 currentTime - lastOccurrence > TTL && ··· 83 90 }; 84 91 85 92 const processIncomingResults = (result: OperationResult): void => { 86 - const meta = result.operation.context.meta; 87 - const isMiss = !meta || meta.cacheOutcome === 'miss'; 88 - if (isMiss) { 93 + // When we get a result for the operation we check whether it resolved 94 + // synchronously by checking whether the counter is different from the 95 + // dispatched counter. 96 + const lastDispatched = dispatched.get(result.operation.key) || 0; 97 + if (counter !== lastDispatched) { 98 + // We only delete in the case of a miss to ensure that cache-and-network 99 + // is properly taken care of 100 + dispatched.delete(result.operation.key); 89 101 operations.set(result.operation.key, new Date().getTime()); 90 102 } 91 103 };