| 1 |
import { Subscriber } from '../Subscriber'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Creates an instance of an `OperatorSubscriber`. |
| 5 |
* @param destination The downstream subscriber. |
| 6 |
* @param onNext Handles next values, only called if this subscriber is not stopped or closed. Any |
| 7 |
* error that occurs in this function is caught and sent to the `error` method of this subscriber. |
| 8 |
* @param onError Handles errors from the subscription, any errors that occur in this handler are caught |
| 9 |
* and send to the `destination` error handler. |
| 10 |
* @param onComplete Handles completion notification from the subscription. Any errors that occur in |
| 11 |
* this handler are sent to the `destination` error handler. |
| 12 |
* @param onFinalize Additional teardown logic here. This will only be called on teardown if the |
| 13 |
* subscriber itself is not already closed. This is called after all other teardown logic is executed. |
| 14 |
*/ |
| 15 |
export function createOperatorSubscriber<T>( |
| 16 |
destination: Subscriber<any>, |
| 17 |
onNext?: (value: T) => void, |
| 18 |
onComplete?: () => void, |
| 19 |
onError?: (err: any) => void, |
| 20 |
onFinalize?: () => void |
| 21 |
): Subscriber<T> { |
| 22 |
return new OperatorSubscriber(destination, onNext, onComplete, onError, onFinalize); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* A generic helper for allowing operators to be created with a Subscriber and |
| 27 |
* use closures to capture necessary state from the operator function itself. |
| 28 |
*/ |
| 29 |
export class OperatorSubscriber<T> extends Subscriber<T> { |
| 30 |
/** |
| 31 |
* Creates an instance of an `OperatorSubscriber`. |
| 32 |
* @param destination The downstream subscriber. |
| 33 |
* @param onNext Handles next values, only called if this subscriber is not stopped or closed. Any |
| 34 |
* error that occurs in this function is caught and sent to the `error` method of this subscriber. |
| 35 |
* @param onError Handles errors from the subscription, any errors that occur in this handler are caught |
| 36 |
* and send to the `destination` error handler. |
| 37 |
* @param onComplete Handles completion notification from the subscription. Any errors that occur in |
| 38 |
* this handler are sent to the `destination` error handler. |
| 39 |
* @param onFinalize Additional finalization logic here. This will only be called on finalization if the |
| 40 |
* subscriber itself is not already closed. This is called after all other finalization logic is executed. |
| 41 |
* @param shouldUnsubscribe An optional check to see if an unsubscribe call should truly unsubscribe. |
| 42 |
* NOTE: This currently **ONLY** exists to support the strange behavior of {@link groupBy}, where unsubscription |
| 43 |
* to the resulting observable does not actually disconnect from the source if there are active subscriptions |
| 44 |
* to any grouped observable. (DO NOT EXPOSE OR USE EXTERNALLY!!!) |
| 45 |
*/ |
| 46 |
constructor( |
| 47 |
destination: Subscriber<any>, |
| 48 |
onNext?: (value: T) => void, |
| 49 |
onComplete?: () => void, |
| 50 |
onError?: (err: any) => void, |
| 51 |
private onFinalize?: () => void, |
| 52 |
private shouldUnsubscribe?: () => boolean |
| 53 |
) { |
| 54 |
// It's important - for performance reasons - that all of this class's |
| 55 |
// members are initialized and that they are always initialized in the same |
| 56 |
// order. This will ensure that all OperatorSubscriber instances have the |
| 57 |
// same hidden class in V8. This, in turn, will help keep the number of |
| 58 |
// hidden classes involved in property accesses within the base class as |
| 59 |
// low as possible. If the number of hidden classes involved exceeds four, |
| 60 |
// the property accesses will become megamorphic and performance penalties |
| 61 |
// will be incurred - i.e. inline caches won't be used. |
| 62 |
// |
| 63 |
// The reasons for ensuring all instances have the same hidden class are |
| 64 |
// further discussed in this blog post from Benedikt Meurer: |
| 65 |
// https://benediktmeurer.de/2018/03/23/impact-of-polymorphism-on-component-based-frameworks-like-react/ |
| 66 |
super(destination); |
| 67 |
this._next = onNext |
| 68 |
? function (this: OperatorSubscriber<T>, value: T) { |
| 69 |
try { |
| 70 |
onNext(value); |
| 71 |
} catch (err) { |
| 72 |
destination.error(err); |
| 73 |
} |
| 74 |
} |
| 75 |
: super._next; |
| 76 |
this._error = onError |
| 77 |
? function (this: OperatorSubscriber<T>, err: any) { |
| 78 |
try { |
| 79 |
onError(err); |
| 80 |
} catch (err) { |
| 81 |
// Send any errors that occur down stream. |
| 82 |
destination.error(err); |
| 83 |
} finally { |
| 84 |
// Ensure finalization. |
| 85 |
this.unsubscribe(); |
| 86 |
} |
| 87 |
} |
| 88 |
: super._error; |
| 89 |
this._complete = onComplete |
| 90 |
? function (this: OperatorSubscriber<T>) { |
| 91 |
try { |
| 92 |
onComplete(); |
| 93 |
} catch (err) { |
| 94 |
// Send any errors that occur down stream. |
| 95 |
destination.error(err); |
| 96 |
} finally { |
| 97 |
// Ensure finalization. |
| 98 |
this.unsubscribe(); |
| 99 |
} |
| 100 |
} |
| 101 |
: super._complete; |
| 102 |
} |
| 103 |
|
| 104 |
unsubscribe() { |
| 105 |
if (!this.shouldUnsubscribe || this.shouldUnsubscribe()) { |
| 106 |
const { closed } = this; |
| 107 |
super.unsubscribe(); |
| 108 |
// Execute additional teardown if we have any and we didn't already do so. |
| 109 |
!closed && this.onFinalize?.(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |