有效地创建从可观察集合中过滤特定项目的可观察对象

Efficiently creating observables that filter a specific item from an observable collection

我有一个发布集合更改的 RxJS 主题。每次集合发生变化时,主题都会将新内容发布为数组。例如

let collectionSubject = new Rx.BehaviourSubject();

collectionSubject.onNext([{
    id: 1
}]);

我想为客户端代码提供“按 ID”订阅此集合的功能。例如。当集合发生变化时,他们只会收到与查询的 ID 匹配的项(如果存在)。如果该项目不存在,或刚刚被删除,他们收到未定义。

我可以天真地实现如下:

byId(id) {
   return collectionSubject.filter(items => items.find(item => item.id == id));
}

然而,这将每次创建一个新的可观察对象,并导致 items 数组的多次冗余迭代。我可以使用一个由 id 键控的 Map 来缓存特定 id 的可观察对象,但这仍然会导致针对不同 id 的项目数组进行多次迭代。

我能看到的唯一解决方案是编写大量自定义机制来为每个 id 创建、缓存和销毁主题,在集合发生变化时迭代一次集合,并将每个项目发布到任何相应的主题。

是否有更简单、更惯用的方法来使用底层 RxJS 运算符实现此目的?关键要求是只迭代底层集合一次。

不确定通过的 link 是否让您找到了实际的解决方案,因此我在此处提供了有关可能方法的更多详细信息。这个想法是使用运算符 groupBy,cf。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/groupby.md , http://reactivex.io/documentation/operators/groupby.html for the marbles and if you fancy here for the tests specs https://github.com/ReactiveX/RxJS/blob/master/spec/operators/groupBy-spec.js).

假设您有一张按 ID 排列的观察者地图,名为 observers,您可以尝试以下方法:

collectionSubject = new Rx.Subject();
observers = [0, emits("observer for group 1"), emits("observer for group 2")];

collectionSubject
    .concatMap(function ( arr ) {return arr;})
    .tap(emits("tap"))
    .groupBy(function ( item ) {return item.id;})
    .subscribe(function ( groupObs ) {
                 groupObs.subscribe(function ( item ) {
                   observers[item.id](item);
                 });
               });

collectionSubject.onNext([
  {id : 1, value : "first item / group 1"},
  {id : 1, value : "second item / group 1"},
  {id : 2, value : "first item / group 2"},
  {id : 2, value : "second item / group 2"},
  {id : 1, value : "third item / group 1"},
  {id : 2, value : "third item / group 2"},
]);

此处测试结果:

"tap emits first item / group 1"
"observer for group 1 emits first item / group 1"
"tap emits second item / group 1"
"observer for group 1 emits second item / group 1"
"tap emits first item / group 2"
"observer for group 2 emits first item / group 2"
"tap emits second item / group 2"
"observer for group 2 emits second item / group 2"
"tap emits third item / group 1"
"observer for group 1 emits third item / group 1"
"tap emits third item / group 2"
"observer for group 2 emits third item / group 2"

这里是jsbin。 https://jsbin.com/qikamamohi/edit?js,console

注意:

  • concatMap 是一种逐项按顺序发出数组内容的技巧