Skip to content

Commit

Permalink
Fix cascading cancellations from components (#503)
Browse files Browse the repository at this point in the history
* Fix cascading cancellations from components

wonka@^4.0.0 has some improved behaviour that fixed
some edge cases and bugs. As it turned out urql was
relying on one of them.
The switchMap in the hooks would cancel the last
query and start the next. However, this cancellation
cascades not only up to executeQuery to end and
teardown the operation, it also flows up through the
exchange pipeline, ending the entire thing like an
electrical surge.

This can be prevented by publishing the results
chain and making sure it can never be interrupted.

* Fix pollInterval test

* Add scheduler dependency to silence peer dependency warning
  • Loading branch information
kitten authored Jan 11, 2020
1 parent c380448 commit 21ed773
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^5.1.3",
"rollup-plugin-typescript2": "^0.25.3",
"scheduler": "^0.18.0",
"terser": "^4.6.2",
"ts-jest": "^24.3.0",
"typescript": "^3.7.4"
Expand All @@ -137,6 +136,7 @@
"react": ">= 16.8.0"
},
"dependencies": {
"scheduler": ">= 0.16.0",
"react-wonka": "^2.0.0",
"wonka": "^4.0.5"
}
Expand Down
6 changes: 4 additions & 2 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ describe('executeQuery', () => {

expect(receivedOps.length).toEqual(1);
jest.advanceTimersByTime(200);
expect(receivedOps.length).toEqual(3);
expect(receivedOps.length).toEqual(5);
expect(receivedOps[0].operationName).toEqual('query');
expect(receivedOps[1].operationName).toEqual('query');
expect(receivedOps[1].operationName).toEqual('teardown');
expect(receivedOps[2].operationName).toEqual('query');
expect(receivedOps[3].operationName).toEqual('teardown');
expect(receivedOps[4].operationName).toEqual('query');
unsubscribe();
});
});
Expand Down
5 changes: 5 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
interval,
fromValue,
switchMap,
publish,
} from 'wonka';

import {
Expand Down Expand Up @@ -135,6 +136,10 @@ export class Client {
forward: fallbackExchangeIO,
})(this.operations$)
);

// Prevent the `results$` exchange pipeline from being closed by active
// cancellations cascading up from components
pipe(this.results$, publish);
}

private createOperationContext = (
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5137,7 +5137,7 @@ sax@^1.2.4:
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==

scheduler@^0.18.0:
"scheduler@>= 0.16.0", scheduler@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.18.0.tgz#5901ad6659bc1d8f3fdaf36eb7a67b0d6746b1c4"
integrity sha512-agTSHR1Nbfi6ulI0kYNK0203joW2Y5W4po4l+v03tOoiJKpTBbxpNhWDvqc/4IcOw+KLmSiQLTasZ4cab2/UWQ==
Expand Down

0 comments on commit 21ed773

Please sign in to comment.