d80f214e158c3d3f11c2d8e944f2a2aa118507ab29dc2094fd8b531fe972920fa9f3496c5e0e22b944fe0533a4bdedb048ac1127c319e0c605c0a4d6444559 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Operator } from './Operator';
  2. import { Observable } from './Observable';
  3. import { Subscriber } from './Subscriber';
  4. import { Subscription, EMPTY_SUBSCRIPTION } from './Subscription';
  5. import { Observer, SubscriptionLike, TeardownLogic } from './types';
  6. import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
  7. import { arrRemove } from './util/arrRemove';
  8. import { errorContext } from './util/errorContext';
  9. /**
  10. * A Subject is a special type of Observable that allows values to be
  11. * multicasted to many Observers. Subjects are like EventEmitters.
  12. *
  13. * Every Subject is an Observable and an Observer. You can subscribe to a
  14. * Subject, and you can call next to feed values as well as error and complete.
  15. */
  16. export class Subject<T> extends Observable<T> implements SubscriptionLike {
  17. closed = false;
  18. private currentObservers: Observer<T>[] | null = null;
  19. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  20. observers: Observer<T>[] = [];
  21. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  22. isStopped = false;
  23. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  24. hasError = false;
  25. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  26. thrownError: any = null;
  27. /**
  28. * Creates a "subject" by basically gluing an observer to an observable.
  29. *
  30. * @deprecated Recommended you do not use. Will be removed at some point in the future. Plans for replacement still under discussion.
  31. */
  32. static create: (...args: any[]) => any = <T>(destination: Observer<T>, source: Observable<T>): AnonymousSubject<T> => {
  33. return new AnonymousSubject<T>(destination, source);
  34. };
  35. constructor() {
  36. // NOTE: This must be here to obscure Observable's constructor.
  37. super();
  38. }
  39. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  40. lift<R>(operator: Operator<T, R>): Observable<R> {
  41. const subject = new AnonymousSubject(this, this);
  42. subject.operator = operator as any;
  43. return subject as any;
  44. }
  45. /** @internal */
  46. protected _throwIfClosed() {
  47. if (this.closed) {
  48. throw new ObjectUnsubscribedError();
  49. }
  50. }
  51. next(value: T) {
  52. errorContext(() => {
  53. this._throwIfClosed();
  54. if (!this.isStopped) {
  55. if (!this.currentObservers) {
  56. this.currentObservers = Array.from(this.observers);
  57. }
  58. for (const observer of this.currentObservers) {
  59. observer.next(value);
  60. }
  61. }
  62. });
  63. }
  64. error(err: any) {
  65. errorContext(() => {
  66. this._throwIfClosed();
  67. if (!this.isStopped) {
  68. this.hasError = this.isStopped = true;
  69. this.thrownError = err;
  70. const { observers } = this;
  71. while (observers.length) {
  72. observers.shift()!.error(err);
  73. }
  74. }
  75. });
  76. }
  77. complete() {
  78. errorContext(() => {
  79. this._throwIfClosed();
  80. if (!this.isStopped) {
  81. this.isStopped = true;
  82. const { observers } = this;
  83. while (observers.length) {
  84. observers.shift()!.complete();
  85. }
  86. }
  87. });
  88. }
  89. unsubscribe() {
  90. this.isStopped = this.closed = true;
  91. this.observers = this.currentObservers = null!;
  92. }
  93. get observed() {
  94. return this.observers?.length > 0;
  95. }
  96. /** @internal */
  97. protected _trySubscribe(subscriber: Subscriber<T>): TeardownLogic {
  98. this._throwIfClosed();
  99. return super._trySubscribe(subscriber);
  100. }
  101. /** @internal */
  102. protected _subscribe(subscriber: Subscriber<T>): Subscription {
  103. this._throwIfClosed();
  104. this._checkFinalizedStatuses(subscriber);
  105. return this._innerSubscribe(subscriber);
  106. }
  107. /** @internal */
  108. protected _innerSubscribe(subscriber: Subscriber<any>) {
  109. const { hasError, isStopped, observers } = this;
  110. if (hasError || isStopped) {
  111. return EMPTY_SUBSCRIPTION;
  112. }
  113. this.currentObservers = null;
  114. observers.push(subscriber);
  115. return new Subscription(() => {
  116. this.currentObservers = null;
  117. arrRemove(observers, subscriber);
  118. });
  119. }
  120. /** @internal */
  121. protected _checkFinalizedStatuses(subscriber: Subscriber<any>) {
  122. const { hasError, thrownError, isStopped } = this;
  123. if (hasError) {
  124. subscriber.error(thrownError);
  125. } else if (isStopped) {
  126. subscriber.complete();
  127. }
  128. }
  129. /**
  130. * Creates a new Observable with this Subject as the source. You can do this
  131. * to create custom Observer-side logic of the Subject and conceal it from
  132. * code that uses the Observable.
  133. * @return Observable that this Subject casts to.
  134. */
  135. asObservable(): Observable<T> {
  136. const observable: any = new Observable<T>();
  137. observable.source = this;
  138. return observable;
  139. }
  140. }
  141. export class AnonymousSubject<T> extends Subject<T> {
  142. constructor(
  143. /** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
  144. public destination?: Observer<T>,
  145. source?: Observable<T>
  146. ) {
  147. super();
  148. this.source = source;
  149. }
  150. next(value: T) {
  151. this.destination?.next?.(value);
  152. }
  153. error(err: any) {
  154. this.destination?.error?.(err);
  155. }
  156. complete() {
  157. this.destination?.complete?.();
  158. }
  159. /** @internal */
  160. protected _subscribe(subscriber: Subscriber<T>): Subscription {
  161. return this.source?.subscribe(subscriber) ?? EMPTY_SUBSCRIPTION;
  162. }
  163. }