rxjs#EMPTY JavaScript Examples

The following examples show how to use rxjs#EMPTY. 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: date-keeper.js    From real-time-map with MIT License 4 votes vote down vote up
dateKeeper = {

  init(startDate, period = 1000) {

    this.startDate = startDate;
    this.newTime = startDate;
    this.period = period;
    this.paused = false;

    this.emitterSubject = new Subject();
    this.inputThrottler = new Subject();

    this.currentSecond$ = this.createDateKeeper();
    this.createInputThrottler();

    this.subscriberList = [];
  },

  get state() {
    // console.trace(new Date(this.newTime));
    return {
      period: this.period,
      newTimeSet: this.newTime,
      pause: this.paused
    };
  },

  get observable() {
    return this.currentSecond$;
  },

  getPeriod() {
    return this.period;
  },

  createDateKeeper() {
    const res = this.emitterSubject.pipe(

      startWith(this.state),

      switchMap(next => {

        if (next.pause) {
          return empty();
        }

        const res = interval(next.period).pipe(mapTo(false));
        if (next.newTimeSet) {
          this.newTime = false;
          return res.pipe(startWith(next.newTimeSet));
        }
        return res;

      }),

      scan((currentTime, newTimeSet) =>
        newTimeSet ? newTimeSet : currentTime + 1000,
        0
      ),

      publish()
    );

    res.connect();
    this.newTime = false;
    return res;
  },

  createInputThrottler() {

    this.inputThrottler.pipe(
      throttleTime(360)
    ).subscribe(state => {
      this.emitterSubject.next(state);
    });

  },

  emitNext() {
    this.inputThrottler.next(this.state);
  },

  subscribe(callback) {
    const newSub = this.currentSecond$.subscribe(callback);
    this.subscriberList.push(newSub);
    return newSub;
  },

  pause() {
    this.paused = true;
    this.emitNext();
  },

  resume() {
    this.paused = false;
    this.emitNext();
  },

  setPeriod(period) {
    this.period = period;
    this.paused = false;
    updatePeriodChangeFunctions(period);
    this.emitNext();
  },

  setNewTime(newTimeInput) {

    this.newTime = Math.round(newTimeInput / 1000) * 1000;

    if (this.newTime < storage.dayStart.getTime() || this.newTime > storage.dayEnd.getTime()) {
      this.pause();
      differentDateSet(this.newTime);
    }

    if (checkIfLive(this.newTime) > 600) {
      this.newTime = getLiveTime();
    }

    storage.currentTime = this.newTime;
    updateTimeChangeFunctions(this.newTime);

    this.paused = false;
    this.emitNext();
    resetTransitionAnimation();
  },

  update() {

    storage.currentTime += 1000;

    if (checkIfLive(storage.currentTime) > 600) {
      storage.currentTime = getLiveTime();
    }

    this.setNewTime(storage.currentTime);
  }

}