rxjs/operators#switchMap TypeScript Examples

The following examples show how to use rxjs/operators#switchMap. 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 rubic-app with GNU General Public License v3.0 7 votes vote down vote up
/**
 * Combination of switchMap and iif.
 * @param condition Condition which Observable should be chosen.
 * @param trueResultFn An Observable that will be subscribed if condition is true.
 * @param falseResultFn An Observable that will be subscribed if condition is false.
 */
export function switchIif<A = void, T = never, F = never>(
  condition: (args: A) => boolean,
  trueResultFn: (args: A) => Observable<T>,
  falseResultFn: (args: A) => Observable<F>
): OperatorFunction<A, T | F> {
  return switchMap((args: A) =>
    iif(
      () => condition(args),
      defer(() => trueResultFn(args)),
      defer(() => falseResultFn(args))
    )
  );
}
Example #2
Source File: gas-settings.component.ts    From gnosis.1inch.exchange with MIT License 6 votes vote down vote up
ngOnInit(): void {

    this.gasPrice$ = this.txSpeedSelect.valueChanges.pipe(
      switchMap((txSpeed: TxSpeed) => {
        console.log(`txSpeed=`, txSpeed);
        if (txSpeed !== 'custom') {
          return of(this.getGasPrice(txSpeed));
        }

        return this.gasPriceInput.valueChanges.pipe(
            startWith(this.gasPriceInput.value),
            filter(() => !this.gasPriceInput.errors),
            map((value) => {
              this.customGasPrice = value;
              return [formatGasPrice(value), value];
            })
          );

      }),
      map(([gasPriceBN, gasPrice]) => {
        this.gasPriceChange.next({
          gasPriceBN,
          gasPrice,
          txSpeed: this.txSpeedSelect.value
        });
        return gasPrice;
      }),
      shareReplay({ bufferSize: 1, refCount: true })
    );

    this.subscription.add(
      this.gasPrice$.subscribe()
    );
  }
Example #3
Source File: app.component.ts    From xBull-Wallet with GNU Affero General Public License v3.0 6 votes vote down vote up
async setTranslation(): Promise<void> {
    this.translateService.setDefaultLang('en');

    const selectedLanguage = await selectPersistStateInit()
      .pipe(switchMap(_ => this.settingsQuery.selectedLanguage$))
      .pipe(take(1))
      .toPromise();

    if (!selectedLanguage) {
      const langToUse = this.translateService
        .getLangs()
        .find(language => language === this.translateService.getBrowserLang());

      this.settingsService.setSelectedLanguage(langToUse || 'en');
    } else {
      this.settingsService.setSelectedLanguage(selectedLanguage);
    }
  }
Example #4
Source File: auth.service.ts    From rubic-app with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Authenticate user on backend.
   * @param address wallet address
   * @param nonce nonce to sign
   * @param signature signed nonce
   * @param walletProvider wallet provider
   * @return Authentication key.
   */
  private sendSignedNonce(
    address: string,
    nonce: string,
    signature: string,
    walletProvider: WALLET_NAME
  ): Promise<void> {
    return this.httpService
      .post('auth/wallets/login/', {
        address,
        message: nonce,
        signedMessage: signature,
        walletProvider,
        type: this.walletConnectorService.provider.walletType.toLowerCase()
      })
      .pipe(switchMap(() => EMPTY))
      .toPromise();
  }
Example #5
Source File: product.service.ts    From Angular-ActionStreams with MIT License 6 votes vote down vote up
product$ = this.productSelectedAction$
    .pipe(
      filter(id => !!id),
      switchMap(selectedProductId =>
        this.http.get<Product>(`${this.productsUrl}/${selectedProductId}`)
          .pipe(
            tap(response => console.log(JSON.stringify(response))),
            map(p => ({ ...p, profit: p.price - p.cost }) as Product),
            catchError(this.handleError)
          )
      ));
Example #6
Source File: product.service.ts    From Angular-HigherOrderMapping with MIT License 6 votes vote down vote up
// Try mergeMap, switchMap, concatMap
  product$ = this.productSelectedAction$
    .pipe(
      filter(id => !!id),
      switchMap(selectedProductId =>
        this.http.get<Product>(`${this.productsUrl}/${selectedProductId}`)
          .pipe(
            tap(response => console.log(JSON.stringify(response))),
            map(p => ({ ...p, profit: p.price - p.cost }) as Product),
            catchError(this.handleError)
          )
      ));
Example #7
Source File: base.adapter.ts    From nx-plugins with MIT License 6 votes vote down vote up
deploy(
    context: BuilderContext,
    cwd: string,
    options: NxDeployItDeployBuilderSchema,
    configuration: string,
    targetOptions: any
  ): Observable<BuilderOutput> {
    const distributationPath = getDistributionPath(context);

    const build$: Observable<BuilderOutput> = from(
      context
        .scheduleTarget({
          target: 'build',
          project: context.target.project,
          configuration: context.target.configuration || ''
        })
        .then(target => target.result)
    );

    return build$.pipe(
      switchMap(() =>
        this.up(
          cwd,
          options,
          configuration,
          targetOptions,
          distributationPath,
          context.target.project
        )
      )
    );
  }