rxjs/operators#combineLatestAll TypeScript Examples

The following examples show how to use rxjs/operators#combineLatestAll. 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: PoolInfoTask.ts    From guardian with Apache License 2.0 5 votes vote down vote up
setup = async (apiRx: ApiRx, tokens: string[]) => {
  const getPools = (poolId: number | number[] | 'all') => {
    return apiRx.query.baseLiquidityPoolsForMargin.pools.entries().pipe(
      mergeMap((entries) => {
        return entries.filter(([storageKey]) => {
          if (poolId === 'all') return true;
          const [id] = storageKey.args;
          return castArray(poolId).includes(id.toNumber());
        });
      })
    );
  };

  const getTradingPairOptions = (poolId: LiquidityPoolId, getPairId: Function): Observable<TraderPairOptions[]> => {
    return apiRx.query.marginLiquidityPools.poolTradingPairOptions.entries(poolId).pipe(
      mergeMap((x) => x),
      map(([storageKey, options]) => {
        const [, tradingPair] = storageKey.args;
        return apiRx.query.marginLiquidityPools.tradingPairOptions(tradingPair).pipe(
          map((tradingPairOptions) => {
            const maxSpread = tradingPairOptions.maxSpread;
            let askSpread = options.askSpread.unwrapOrDefault();
            let bidSpread = options.bidSpread.unwrapOrDefault();

            if (maxSpread.isSome) {
              askSpread = apiRx.createType('FixedU128', BN.max(maxSpread.unwrap(), askSpread));
              bidSpread = apiRx.createType('FixedU128', BN.max(maxSpread.unwrap(), bidSpread));
            }

            return {
              pair: {
                base: tradingPair.base.toString(),
                quote: tradingPair.quote.toString()
              },
              pairId: getPairId(tradingPair),
              enabledTrades: options.enabledTrades.toJSON(),
              askSpread: askSpread.toString(),
              bidSpread: bidSpread.toString()
            };
          })
        );
      }),
      combineLatestAll()
    );
  };

  const getPairId = (pair: { base: string; quote: string }): string => {
    const baseToken = tokens.find((x) => pair.base === x);
    const quoteToken = tokens.find((x) => pair.quote === x);
    return `${baseToken || pair.base}${quoteToken || pair.quote}`;
  };

  return { getPools, getTradingPairOptions, getPairId };
}