Debian Codegraph @

node/rxjs/src/internal/util/lift.ts

raw
1 import { Observable } from '../Observable';
2 import { Subscriber } from '../Subscriber';
3 import { OperatorFunction } from '../types';
4 import { isFunction } from './isFunction';
5
6 /**
7 * Used to determine if an object is an Observable with a lift function.
8 */
9 export function hasLift(source: any): source is { lift: InstanceType<typeof Observable>['lift'] } {
10 return isFunction(source?.lift);
11 }
12
13 /**
14 * Creates an `OperatorFunction`. Used to define operators throughout the library in a concise way.
15 * @param init The logic to connect the liftedSource to the subscriber at the moment of subscription.
16 */
17 export function operate<T, R>(
18 init: (liftedSource: Observable<T>, subscriber: Subscriber<R>) => (() => void) | void
19 ): OperatorFunction<T, R> {
20 return (source: Observable<T>) => {
21 if (hasLift(source)) {
22 return source.lift(function (this: Subscriber<R>, liftedSource: Observable<T>) {
23 try {
24 return init(liftedSource, this);
25 } catch (err) {
26 this.error(err);
27 }
28 });
29 }
30 throw new TypeError('Unable to lift unknown Observable type');
31 };
32 }