Skip to content

Commit

Permalink
Fix cleanup observer blocking unsubscribe (#7165)
Browse files Browse the repository at this point in the history
Fixes #6985.
  • Loading branch information
javier-garcia-meteologica authored Oct 14, 2020
1 parent f45e0f6 commit 2b8ee34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
[@hwillson](https://github.com/hwillson) in [#7110](https://github.com/apollographql/apollo-client/pull/7110)
> Returning mock configuration errors through the Link's `Observable` was the default behavior in Apollo Client 2.x. We changed it for 3, but the change has been problematic for those looking to migrate from 2.x to 3. We've decided to change this back with the understanding that not many people want or are relying on `MockLink`'s throwing exception approach. If you want to change this functionality, you can define custom error handling through `MockLink.setOnError`.

- Unsubscribing the last observer from an `ObservableQuery` will once again unsubscribe from the underlying network `Observable` in all cases, as in Apollo Client 2.x, allowing network requests to be cancelled by unsubscribing. <br/>
[@javier-garcia-meteologica](https://github.com/javier-garcia-meteologica) in [#7165](https://github.com/apollographql/apollo-client/pull/7165)

## Improvements

- Support inheritance of type and field policies, according to `possibleTypes`. <br/>
Expand Down
51 changes: 35 additions & 16 deletions src/utilities/observables/Concast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,34 @@ export class Concast<T> extends Observable<T> {
this.handlers.complete!();
}

private deliverLastMessage(observer: Observer<T>) {
if (this.latest) {
const nextOrError = this.latest[0];
const method = observer[nextOrError];
if (method) {
method.call(observer, this.latest[1]);
}
// If the subscription is already closed, and the last message was
// a 'next' message, simulate delivery of the final 'complete'
// message again.
if (this.sub === null &&
nextOrError === "next" &&
observer.complete) {
observer.complete();
}
}
}

// Note: cleanup observers do not count towards this total.
private addCount = 0;

public addObserver(observer: Observer<T>) {
if (!this.observers.has(observer)) {
// Immediately deliver the most recent message, so we can always
// be sure all observers have the latest information.
if (this.latest) {
const nextOrError = this.latest[0];
const method = observer[nextOrError];
if (method) {
method.call(observer, this.latest[1]);
}
// If the subscription is already closed, and the last message was
// a 'next' message, simulate delivery of the final 'complete'
// message again.
if (this.sub === null &&
nextOrError === "next" &&
observer.complete) {
observer.complete();
}
}
this.deliverLastMessage(observer);
this.observers.add(observer);
++this.addCount;
}
}

Expand Down Expand Up @@ -201,7 +209,13 @@ export class Concast<T> extends Observable<T> {
const once = () => {
if (!called) {
called = true;
this.observers.delete(observer);
// If there have been no other (non-cleanup) observers added, pass
// true for the quietly argument, so the removal of the cleanup
// observer does not call this.sub.unsubscribe. If a cleanup
// observer is added and removed before any other observers
// subscribe, we do not want to prevent other observers from
// subscribing later.
this.removeObserver(observer, !this.addCount);
callback();
}
}
Expand All @@ -210,7 +224,12 @@ export class Concast<T> extends Observable<T> {
error: once,
complete: once,
};
const count = this.addCount;
this.addObserver(observer);
// Normally addObserver increments this.addCount, but we can "hide"
// cleanup observers by restoring this.addCount to its previous value
// after adding any cleanup observer.
this.addCount = count;
}

// A public way to abort observation and broadcast.
Expand Down

0 comments on commit 2b8ee34

Please sign in to comment.