Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dedup ignoring torn down query operations #281

Merged
merged 1 commit into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/exchanges/dedup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ it('forwards duplicate query operations as usual after they respond', async () =
expect(forwardedOperations.length).toBe(2);
});

it('forwards duplicate query operations after one was torn down', async () => {
shouldRespond = false; // We filter out our mock responses
const [ops$, next, complete] = input;
const exchange = dedupExchange(exchangeArgs)(ops$);

publish(exchange);
next(queryOperation);
next({ ...queryOperation, operationName: 'teardown' });
next(queryOperation);
complete();
expect(forwardedOperations.length).toBe(3);
});

it('always forwards mutation operations without deduplicating them', async () => {
shouldRespond = false; // We filter out our mock responses
const [ops$, next, complete] = input;
Expand Down
48 changes: 26 additions & 22 deletions src/exchanges/dedup.ts
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for not having this inline?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clean it up; I thought having names like afterOperationResult could help when someone's trying to figure out how those work

};

return !hasInFlightOp;
})
)
),
tap(res => {
inFlight.delete(res.operation.key);
})
return ops$ => {
const forward$ = pipe(
ops$,
filter(filterIncomingOperation)
);
return pipe(
forward(forward$),
tap(afterOperationResult)
);
};
};