Skip to content

Commit

Permalink
Use setTimeout instead of microtask to delay Concast cancellation.
Browse files Browse the repository at this point in the history
Inspired by @cornedor's comment:
#7608 (comment)

Should help with some instances of issue #7608, specifically those with
global unhandled promise rejection errors. I don't know if this is the
full solution, but I believe it is helpful and safe to introduce a delay
before unsubscribing, giving other subscribers a chance to subscribe,
thereby saving the Concast from needing to be cancelled.
  • Loading branch information
benjamn committed Aug 20, 2021
1 parent d8cbcd6 commit a130153
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,9 +1127,8 @@ export class QueryManager<TStore> {
// This cancel function needs to be set before the concast is created,
// in case concast creation synchronously cancels the request.
this.fetchCancelFns.set(queryId, reason => {
// Delaying the cancellation using a Promise ensures that the
// concast variable has been initialized.
Promise.resolve().then(() => concast.cancel(reason));
// This delay ensures the concast variable has been initialized.
setTimeout(() => concast.cancel(reason), 10);
});

// A Concast<T> can be created either from an Iterable<Observable<T>>
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/observables/Concast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class Concast<T> extends Observable<T> {
// Delay unsubscribing from the underlying subscription slightly,
// so that immediately subscribing another observer can keep the
// subscription active.
if (sub) Promise.resolve().then(() => sub.unsubscribe());
if (sub) setTimeout(() => sub.unsubscribe(), 10);
this.sub = null;
this.latest = ["error", error];
this.reject(error);
Expand Down

0 comments on commit a130153

Please sign in to comment.