Skip to content

Commit

Permalink
fix: add finalize callback in operator call
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed May 14, 2020
1 parent 0ba30b8 commit daefaa5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
12 changes: 12 additions & 0 deletions spec/operators/finalize-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,16 @@ describe('finalize operator', () => {
subscription.unsubscribe();
expect(order).to.deep.equal(['teardown', 'finalize']);
});

it('should finalize after the teardown with synchronous completion', () => {
const order: string[] = [];
const source = new Observable<void>(subscriber => {
subscriber.complete();
return () => order.push('teardown');
});
source.pipe(
finalize(() => order.push('finalize'))
).subscribe();
expect(order).to.deep.equal(['teardown', 'finalize']);
});
});
23 changes: 3 additions & 20 deletions src/internal/operators/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,8 @@ class FinallyOperator<T> implements Operator<T, T> {
}

call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return subscribeWith(source, new FinallySubscriber(subscriber, this.callback));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class FinallySubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<T>, private callback?: () => void) {
super(destination);
}
unsubscribe() {
super.unsubscribe();
const { callback } = this;
if (callback) {
callback();
this.callback = undefined;
}
const subscription = subscribeWith(source, subscriber);
subscription.add(this.callback);
return subscription;
}
}

0 comments on commit daefaa5

Please sign in to comment.