具有并发工作者的 RxJS 并行队列?

RxJS parallel queue with concurrent workers?

假设我要下载 10,000 个文件。我可以轻松地建立一个包含 10,000 个文件的队列(如果可以做得更好,我很乐意听取建议),

import request from 'request-promise-native';
import {from} from 'rxjs';

let reqs = [];
for ( let i = 0; i < 10000; i++ ) {
  reqs.push(
    from(request(`http://bleh.com/${i}`))
  )
};

现在我有一个 Rx.JS observable 数组,我是根据代表我的队列的承诺创建的。现在对于我想要的行为,我想发出

我可以为这个问题创建一个解决方案,但是考虑到像 Rxjs queue 这样我从未使用过的东西,我想知道最正确的 Rxjs 方法是什么。

听起来您想要 forkJoin 的等价物,它支持调用者指定的最大并发订阅数。

可以使用 mergeMap 重新实现 forkJoin 并公开 concurrent 参数,like this:

import { from, Observable } from "rxjs";
import { last, map, mergeMap, toArray } from "rxjs/operators";

export function forkJoinConcurrent<T>(
  observables: Observable<T>[],
  concurrent: number
): Observable<T[]> {
  // Convert the array of observables to a higher-order observable:
  return from(observables).pipe(
    // Merge each of the observables in the higher-order observable
    // into a single stream:
    mergeMap((observable, observableIndex) => observable.pipe(
      // Like forkJoin, we're interested only in the last value:
      last(),
      // Combine the value with the index so that the stream of merged
      // values - which could be in any order - can be sorted to match
      // the order of the source observables:
      map(value => ({ index: observableIndex, value }))
    ), concurrent),
    // Convert the stream of last values to an array:
    toArray(),
    // Sort the array of value/index pairs by index - so the value
    // indices correspond to the source observable indices and then
    // map the pair to the value:
    map(pairs => pairs.sort((l, r) => l.index - r.index).map(pair => pair.value))
  );
}

我在 2021 年遇到了同样的问题并且能够利用 所以我想我会分享:

index.ts

import request from 'request-promise-native';
import { from, defer } from "rxjs";
import { forkJoinConcurrent } from './forkJoinConcurrent';

const handleRequest = async (id: string) => await request.get(`http://bleh.com/${id}`, { json: true });

const ids: string[] = [...Array(10000).keys()].map((k: number) => k.toString());

const concurrent: number = 3;

/* use `defer` instead of `from` to generate the Observables. 
 `defer` uses a factory to generate the promise and it will execute 
 the factory only when it is subscribed to */

const observables = ids.map((id: string) => defer(() => from(handleRequest(id))))

forkJoinConcurrent<any>(observables, concurrent).subscribe(value => console.log(value));

forkJoinConcurrent.ts

import { from, Observable } from "rxjs";
import { last, map, mergeMap, toArray } from "rxjs/operators";

export function forkJoinConcurrent<T>(
  observables: Observable<T>[],
  concurrent: number
): Observable<T[]> {
  // Convert the array of observables to a higher-order observable:
  return from(observables).pipe(
    // Merge each of the observables in the higher-order observable
    // into a single stream:
    mergeMap((observable, observableIndex) => observable.pipe(
      // Like forkJoin, we're interested only in the last value:
      last(),
      // Combine the value with the index so that the stream of merged
      // values - which could be in any order - can be sorted to match
      // the order of the source observables:
      map(value => ({ index: observableIndex, value }))
    ), concurrent),
    // Convert the stream of last values to an array:
    toArray(),
    // Sort the array of value/index pairs by index - so the value
    // indices correspond to the source observable indices and then
    // map the pair to the value:
    map(pairs => pairs.sort((l, r) => l.index - r.index).map(pair => pair.value))
  );
}