rxjs#isObservable TypeScript Examples

The following examples show how to use rxjs#isObservable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From open-source with MIT License 7 votes vote down vote up
export function clone<T>(input: T): T {
	if (isObservable(input) || typeof input === 'function') {
    return input;

	} else if (Array.isArray(input)) {
    return input.map(clone) as any;

	} else if (isPlainObject(input)) {
    if (input instanceof Map || input instanceof Set) {
      // treated as an abstract data type
      return input;
    }

		const output: any = {};
		for (let index in input) {
			output[index] = clone(input[index]);
    }
		return output as any;

	} else {
		return input;
	}
}
Example #2
Source File: table.ts    From halstack-angular with Apache License 2.0 6 votes vote down vote up
/** Set up a subscription for the data provided by the data source. */
  private _observeRenderChanges() {
    // If no data source has been set, there is nothing to observe for changes.
    if (!this.dataSource) {
      return;
    }

    let dataStream: Observable<T[] | ReadonlyArray<T>> | undefined;

    if (isDataSource(this.dataSource)) {
      dataStream = this.dataSource.connect(this);
    } else if (isObservable(this.dataSource)) {
      dataStream = this.dataSource;
    } else if (Array.isArray(this.dataSource)) {
      dataStream = observableOf(this.dataSource);
    }

    if (dataStream === undefined) {
      throw getTableUnknownDataSourceError();
    }

    this._renderChangeSubscription = dataStream
      .pipe(takeUntil(this._onDestroy))
      .subscribe((data) => {
        this._data = data || [];
        this.renderHeaders();
        this.renderRows();
      });
  }
Example #3
Source File: dyn-control.class.ts    From open-source with MIT License 6 votes vote down vote up
ngOnInit(): void {
    super.ngOnInit();

    // merge any configured paramFns
    if (this.config.paramFns) {
      this.updateParams(undefined, this.config.paramFns);
    }

    // listen parameters changes after the control is ready
    combineLatest([
      isObservable(this.config.params) ? this.config.params : of(this.config.params || {}),
      this.node.paramsUpdates$.pipe(startWith({})),
    ]).pipe(
      scan<any>((params, [config, updates]) => merge(true, params, config, updates)),
      filter(params => !Array.isArray(params)), // filters the first scan
    ).subscribe((params) => {
      // emulates ngOnChanges
      const change = new SimpleChange(this.params, this.completeParams(params), !this.params);
      this.params$.next(change.currentValue);
      this._logger.nodeParamsUpdated(this.node, this.constructor.name, this.params);

      setTimeout(() => {
        // emulates ngOnChanges and async pipe
        this.ngOnChanges({ params: change });
        this.node.markParamsAsLoaded();
        this._ref.markForCheck();
      }, 1);
    });
  }
Example #4
Source File: search.ts    From platyplus with MIT License 6 votes vote down vote up
search$ =
  (
    expression: string | Observable<string>,
    options: SearchOptions = {}
  ): ((data$: Observable<ObjectType>) => Observable<ObjectType>) =>
  (data$) => {
    try {
      const runtime = new Runtime()
      const interpreter = new Intercepter(runtime, options)
      runtime._interpreter = interpreter
      return (isObservable(expression) ? expression : of(expression)).pipe(
        switchMap((exp) => {
          // ? Not ideal. Find a better workaround?
          if (options.circularData && expression === '*')
            throw new Error(
              `Data schema is circular; The expression '*' is not allowed`
            )
          const node = compile(exp)
          return interpreter.visit$(node, data$)
        })
      )
    } catch (error) {
      return throwError(() => new Error(error))
    }
  }
Example #5
Source File: subscriber-route-handler.ts    From pebula-node with MIT License 6 votes vote down vote up
async function safeResolveResult(result: Observable<any> | Promise<any> | any) {
  if (isObservable(result)) {
    await result.toPromise();
  } else if (result && typeof (result as Promise<any>).then === 'function') {
    await result;
  } else {
    await Promise.resolve(result);
  }
}
Example #6
Source File: rxjs-resource.spec.ts    From slickgrid-universal with MIT License 5 votes vote down vote up
describe('RxJs Resource', () => {
  let service: RxJsResource;

  beforeEach(() => {
    service = new RxJsResource();
  });

  it('should create the service', () => {
    expect(service).toBeTruthy();
    expect(service.className).toBe('RxJsResource');
  });

  it('should be able to create an RxJS Observable', () => {
    const observable1 = service.createObservable();
    const observable2 = service.of(1, 2, 3);
    expect(observable1 instanceof Observable).toBeTruthy();
    expect(observable2 instanceof Observable).toBeTruthy();
    expect(service.isObservable(observable1)).toBeTruthy();
    expect(service.isObservable(observable2)).toBeTruthy();
  });

  it('should be able to create an RxJS Subject', () => {
    const subject = service.createSubject();
    expect(subject instanceof Subject).toBeTruthy();
  });

  it('should be able to retrieve the RxJS EMPTY constant from the service getter', () => {
    expect(service.EMPTY).toEqual(EMPTY);
  });

  it('should be able to execute the "iif" method and expect an Observable returned', () => {
    const observable = service.createObservable();
    const iifOutput = service.iif(() => isObservable(observable));
    expect(iifOutput instanceof Observable).toBeTruthy();
  });

  it('should be able to execute the "firstValueFrom" method and expect an Observable returned', () => {
    const observable = service.createObservable();
    const output = service.firstValueFrom(observable);
    expect(output instanceof Promise).toBeTruthy();
  });

  it('should be able to execute the "switchMap" method and expect an Observable returned', () => {
    const observable = service.createObservable();
    const output = observable.pipe(service.switchMap(() => service.createObservable()));
    expect(output instanceof Observable).toBeTruthy();
  });

  it('should be able to execute the "takeUntil" method and expect an Observable returned', () => {
    const observable = service.createObservable();
    const iifOutput = service.iif(() => isObservable(observable));
    const output = observable.pipe(service.takeUntil(iifOutput));
    expect(output instanceof Observable).toBeTruthy();
  });
});
Example #7
Source File: rxjs.resource.ts    From slickgrid-universal with MIT License 5 votes vote down vote up
/** Tests to see if the object is an RxJS Observable */
  isObservable(obj: any): boolean {
    return isObservable(obj);
  }
Example #8
Source File: rxjsResourceStub.ts    From slickgrid-universal with MIT License 5 votes vote down vote up
/** Tests to see if the object is an RxJS Observable */
  isObservable(obj: any): boolean {
    return isObservable(obj);
  }
Example #9
Source File: form-mode.service.ts    From open-source with MIT License 5 votes vote down vote up
private mergeConfigs(config: DynBaseConfig, mode: Partial<DynControlConfig>): DynBaseConfig {
    // custom merge strategy for DynControlConfig
    if (mode.control) {
      config.control = mode.control;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'default')) {
      config.default = mode.default;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'validators')) {
      config.validators = mode.validators;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'asyncValidators')) {
      config.asyncValidators = mode.asyncValidators;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'updateOn')) {
      config.updateOn = mode.updateOn;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'match')) {
      config.match = mode.match;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'cssClass')) {
      config.cssClass = mode.cssClass;
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'errorMsg')) {
      config.errorMsg = mode.errorMsg;
    }
    if (mode.params) {
      if (isObservable(config.params) && isObservable(mode.params)) {
        config.params = combineLatest([config.params, mode.params]).pipe(
          map(([params, modeParams]) => merge(params, modeParams))
        );
      } else if (isObservable(config.params)) {
        config.params = config.params.pipe(
          map(params => merge(params, mode.params))
        );
      } else if (isObservable(mode.params)) {
        const params = config.params;
        config.params = mode.params.pipe(
          map(modeParams => merge(params, modeParams))
        );
      } else {
        config.params = merge(true, config.params, mode.params);
      }
    }
    if (Object.prototype.hasOwnProperty.call(mode, 'paramFns')) {
      config.paramFns = merge(true, config.paramFns, mode.paramFns);
    }

    return config;
  }