SubjectSubscription.ts 849 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { Subject } from './Subject';
  2. import { Observer } from './Observer';
  3. import { Subscription } from './Subscription';
  4. /**
  5. * We need this JSDoc comment for affecting ESDoc.
  6. * @ignore
  7. * @extends {Ignored}
  8. */
  9. export class SubjectSubscription<T> extends Subscription {
  10. closed: boolean = false;
  11. constructor(public subject: Subject<T>, public subscriber: Observer<T>) {
  12. super();
  13. }
  14. unsubscribe() {
  15. if (this.closed) {
  16. return;
  17. }
  18. this.closed = true;
  19. const subject = this.subject;
  20. const observers = subject.observers;
  21. this.subject = null;
  22. if (!observers || observers.length === 0 || subject.isStopped || subject.closed) {
  23. return;
  24. }
  25. const subscriberIndex = observers.indexOf(this.subscriber);
  26. if (subscriberIndex !== -1) {
  27. observers.splice(subscriberIndex, 1);
  28. }
  29. }
  30. }