ddded384a508d7d11f928cbb68999c925680ecffe6bec7106f04f21eecd30f7cd11f15ca6bd2de139a92978347c80797ba6da4f15bc1392a5cf4d22c0031b6 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /** @prettier */
  2. import { Subscription } from './Subscription';
  3. import { Subscriber } from './Subscriber';
  4. import { Observable } from './Observable';
  5. import { subscribeTo } from './util/subscribeTo';
  6. interface SimpleOuterSubscriberLike<T> {
  7. /**
  8. * A handler for inner next notifications from the inner subscription
  9. * @param innerValue the value nexted by the inner producer
  10. */
  11. notifyNext(innerValue: T): void;
  12. /**
  13. * A handler for inner error notifications from the inner subscription
  14. * @param err the error from the inner producer
  15. */
  16. notifyError(err: any): void;
  17. /**
  18. * A handler for inner complete notifications from the inner subscription.
  19. */
  20. notifyComplete(): void;
  21. }
  22. export class SimpleInnerSubscriber<T> extends Subscriber<T> {
  23. constructor(private parent: SimpleOuterSubscriberLike<any>) {
  24. super();
  25. }
  26. protected _next(value: T): void {
  27. this.parent.notifyNext(value);
  28. }
  29. protected _error(error: any): void {
  30. this.parent.notifyError(error);
  31. this.unsubscribe();
  32. }
  33. protected _complete(): void {
  34. this.parent.notifyComplete();
  35. this.unsubscribe();
  36. }
  37. }
  38. export class ComplexInnerSubscriber<T, R> extends Subscriber<R> {
  39. constructor(private parent: ComplexOuterSubscriber<T, R>, public outerValue: T, public outerIndex: number) {
  40. super();
  41. }
  42. protected _next(value: R): void {
  43. this.parent.notifyNext(this.outerValue, value, this.outerIndex, this);
  44. }
  45. protected _error(error: any): void {
  46. this.parent.notifyError(error);
  47. this.unsubscribe();
  48. }
  49. protected _complete(): void {
  50. this.parent.notifyComplete(this);
  51. this.unsubscribe();
  52. }
  53. }
  54. export class SimpleOuterSubscriber<T, R> extends Subscriber<T> implements SimpleOuterSubscriberLike<R> {
  55. notifyNext(innerValue: R): void {
  56. this.destination.next(innerValue);
  57. }
  58. notifyError(err: any): void {
  59. this.destination.error(err);
  60. }
  61. notifyComplete(): void {
  62. this.destination.complete();
  63. }
  64. }
  65. /**
  66. * DO NOT USE (formerly "OuterSubscriber")
  67. * TODO: We want to refactor this and remove it. It is retaining values it shouldn't for long
  68. * periods of time.
  69. */
  70. export class ComplexOuterSubscriber<T, R> extends Subscriber<T> {
  71. /**
  72. * @param _outerValue Used by: bufferToggle, delayWhen, windowToggle
  73. * @param innerValue Used by: subclass default, combineLatest, race, bufferToggle, windowToggle, withLatestFrom
  74. * @param _outerIndex Used by: combineLatest, race, withLatestFrom
  75. * @param _innerSub Used by: delayWhen
  76. */
  77. notifyNext(_outerValue: T, innerValue: R, _outerIndex: number, _innerSub: ComplexInnerSubscriber<T, R>): void {
  78. this.destination.next(innerValue);
  79. }
  80. notifyError(error: any): void {
  81. this.destination.error(error);
  82. }
  83. /**
  84. * @param _innerSub Used by: race, bufferToggle, delayWhen, windowToggle, windowWhen
  85. */
  86. notifyComplete(_innerSub: ComplexInnerSubscriber<T, R>): void {
  87. this.destination.complete();
  88. }
  89. }
  90. export function innerSubscribe(result: any, innerSubscriber: Subscriber<any>): Subscription | undefined {
  91. if (innerSubscriber.closed) {
  92. return undefined;
  93. }
  94. if (result instanceof Observable) {
  95. return result.subscribe(innerSubscriber);
  96. }
  97. let subscription: Subscription;
  98. try {
  99. subscription = subscribeTo(result)(innerSubscriber) as Subscription;
  100. } catch (error) {
  101. innerSubscriber.error(error);
  102. }
  103. return subscription;
  104. }