rxjs#defer JavaScript Examples

The following examples show how to use rxjs#defer. 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: staking_payouts.js    From sdk with MIT License 6 votes vote down vote up
signAndSendExtrinsics = curry((dock, initiator, txs$) => {
  // The first nonce to be used will come from the API call
  // To send several extrinsics simultaneously, we need to emulate increasing nonce
  return from(dock.api.rpc.system.accountNextIndex(initiator.address)).pipe(
    switchMap((nonce) => {
      const sendExtrinsic = (tx) =>
        defer(() => {
          dock.setAccount(initiator);
          const sentTx = dock.signAndSend(tx, FinalizeTx, { nonce });
          // Increase nonce by hand
          nonce = nonce.add(new BN(1));

          return from(timeout(TxFinalizationTimeout, sentTx));
        }).pipe(
          mapRx((result) => ({ tx, result })),
          catchError((error, caught) => {
            console.error(` * Transaction failed: ${error}`);
            const stringified = error.toString().toLowerCase();

            // Filter out errors related to balance and double-claim
            if (
              stringified.includes("balance") ||
              stringified.includes("alreadyclaimed") ||
              stringified.includes("invalid transaction") ||
              stringified.includes("election")
            ) {
              return EMPTY;
            } else {
              // Retry an observable after the given timeout
              return timer(RetryTimeout).pipe(concatMapTo(caught));
            }
          })
        );

      return txs$.pipe(mergeMap(sendExtrinsic, ConcurrentTxLimit));
    })
  );
})
Example #2
Source File: KrakenExchangeClient.js    From invizi with GNU General Public License v3.0 6 votes vote down vote up
// fetch deposits AND withdrawal
  getDepositsObs (apiKey, symbolCurrencyPair = undefined, params = {}) {
    let counter = -1
    this.initializeApiKey(apiKey)
    const query = () => {
      counter++
      return this.ccxt.fetchLedger(undefined, undefined, undefined, {ofs: counter * 50})
    }

    const trades$ = defer(query)

    let load$ = new BehaviorSubject('')

    const whenToRefresh$ = of('').pipe(
      delay(5000),
      tap(_ => load$.next('')),
      skip(1))

    const byDepositAndWithdrawal = trade => trade && ['deposit', 'withdrawal'].includes(trade.info.type)

    const poll$ = concat(trades$, whenToRefresh$)

    return load$.pipe(
      concatMap(_ => poll$),
      takeWhile(data => data.length > 0),
      map(data => ({errors: [], data: data.filter(byDepositAndWithdrawal).map(parseLedger)})),
      catchError(error$ => of({errors: [error$.toString()], data: []})))
  }
Example #3
Source File: KrakenExchangeClient.js    From invizi with GNU General Public License v3.0 6 votes vote down vote up
getMyTradesObs (apiKey, symbolCurrencyPair = undefined, params = {}) {
    let counter = -1
    this.initializeApiKey(apiKey)
    const fetchTrades = () => {
      counter++
      return super.getMyTrades(apiKey, undefined, {ofs: counter * 50})
    }

    const trades$ = defer(() => fetchTrades())

    let load$ = new BehaviorSubject('')

    const whenToRefresh$ = of('').pipe(
      delay(5000),
      tap(_ => load$.next('')),
      skip(1))

    const poll$ = concat(trades$, whenToRefresh$)
    let result$ = load$.pipe(
      concatMap(_ => poll$),
      takeWhile(data => data.length > 0),
      map(data => ({errors: [], data})),
      catchError(error$ => {
        return of({errors: [error$.toString()], data: []})
      }))
    return result$
  }