Sha256: 878d35378ba6bf47c8f64a625dfcc774d133d8ac874301bacdc913b8e83a6567

Contents?: true

Size: 1.84 KB

Versions: 49

Compression:

Stored size: 1.84 KB

Contents

import { Observable, SubscribableOrPromise } from '../Observable';
import { Subscriber } from '../Subscriber';
import { TeardownLogic } from '../Subscription';

import { subscribeToResult } from '../util/subscribeToResult';
import { OuterSubscriber } from '../OuterSubscriber';
/**
 * We need this JSDoc comment for affecting ESDoc.
 * @extends {Ignored}
 * @hide true
 */
export class IfObservable<T, R> extends Observable<T> {

  static create<T, R>(condition: () => boolean | void,
                      thenSource?: SubscribableOrPromise<T> | void,
                      elseSource?: SubscribableOrPromise<R> | void): Observable<T|R> {
    return new IfObservable(condition, thenSource, elseSource);
  }

  constructor(private condition: () => boolean | void,
              private thenSource?: SubscribableOrPromise<T> | void,
              private elseSource?: SubscribableOrPromise<R> | void) {
    super();
  }

  /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<T|R>): TeardownLogic {
    const { condition, thenSource, elseSource } = this;

    return new IfSubscriber(subscriber, condition, thenSource, elseSource);
  }
}

class IfSubscriber<T, R> extends OuterSubscriber<T, T> {
  constructor(destination: Subscriber<T>,
              private condition: () => boolean | void,
              private thenSource?: SubscribableOrPromise<T> | void,
              private elseSource?: SubscribableOrPromise<R> | void) {
    super(destination);
    this.tryIf();
  }

  private tryIf(): void {
    const { condition, thenSource, elseSource } = this;

    let result: boolean;
    try {
      result = <boolean>condition();
      const source = result ? thenSource : elseSource;

      if (source) {
        this.add(subscribeToResult(this, source));
      } else {
        this._complete();
      }
    } catch (err) {
      this._error(err);
    }
  }
}

Version data entries

49 entries across 49 versions & 4 rubygems

Version Path
ilog-0.4.1 node_modules/rxjs/src/observable/IfObservable.ts
ilog-0.4.0 node_modules/rxjs/src/observable/IfObservable.ts
ilog-0.3.3 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-18.0.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.21.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.20.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.19.1 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.19.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.18.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.17.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.16.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.15.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.14.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.13.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.12.2 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.12.1 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.12.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.11.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.10.0 node_modules/rxjs/src/observable/IfObservable.ts
govuk_publishing_components-17.9.0 node_modules/rxjs/src/observable/IfObservable.ts