-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dedup ignoring torn down query operations (#281)
When a query was torn down, the dedup exchange wasn't removing its key from the inFlight keys. This was causing us to ignore torn down queries forever.
- Loading branch information
Showing
2 changed files
with
39 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
import { filter, pipe, tap } from 'wonka'; | ||
import { Exchange } from '../types'; | ||
import { Exchange, Operation, OperationResult } from '../types'; | ||
|
||
/** A default exchange for debouncing GraphQL requests. */ | ||
export const dedupExchange: Exchange = ({ forward }) => { | ||
const inFlight = new Set<number>(); | ||
const inFlightKeys = new Set<number>(); | ||
|
||
return ops$ => | ||
pipe( | ||
forward( | ||
pipe( | ||
ops$, | ||
filter(({ operationName, key }) => { | ||
if (operationName !== 'query') { | ||
return true; | ||
} | ||
const filterIncomingOperation = (operation: Operation) => { | ||
const { key, operationName } = operation; | ||
if (operationName === 'teardown') { | ||
inFlightKeys.delete(key); | ||
return true; | ||
} else if (operationName !== 'query') { | ||
return true; | ||
} | ||
|
||
const hasInFlightOp = inFlight.has(key); | ||
const isInFlight = inFlightKeys.has(key); | ||
inFlightKeys.add(key); | ||
return !isInFlight; | ||
}; | ||
|
||
if (!hasInFlightOp) { | ||
inFlight.add(key); | ||
} | ||
const afterOperationResult = ({ operation }: OperationResult) => { | ||
inFlightKeys.delete(operation.key); | ||
}; | ||
|
||
return !hasInFlightOp; | ||
}) | ||
) | ||
), | ||
tap(res => { | ||
inFlight.delete(res.operation.key); | ||
}) | ||
return ops$ => { | ||
const forward$ = pipe( | ||
ops$, | ||
filter(filterIncomingOperation) | ||
); | ||
return pipe( | ||
forward(forward$), | ||
tap(afterOperationResult) | ||
); | ||
}; | ||
}; |