有效创建可观察对象,以从可观察集合中筛选特定项目
我有一个RxJS主题,用于发布对集合的更改。每次收集更改时,主题都会将新内容作为数组发布。例如。有效创建可观察对象,以从可观察集合中筛选特定项目
let collectionSubject = new Rx.BehaviourSubject();
collectionSubject.onNext([{
id: 1
}]);
我想提供客户端代码在'按ID'的基础上订阅此集合的功能。例如。当集合更改时,它们只会收到与查询ID匹配的项目(如果存在)。如果该项目不存在或刚刚被删除,则它们会收到未定义的项目。
我天真地实现这个如下:
byId(id) {
return collectionSubject.filter(items => items.find(item => item.id == id));
}
然而,这将创建一个新的观察到的每一次,并导致项目阵列的多个冗余迭代。我可以使用一个由ID键控的Map来为特定的ID缓存observables,但是这仍然会导致对不同id的items数组进行多次迭代。
我能看到的唯一解决方案是编写大量自定义机制来为每个ID创建和缓存和销毁主题,在发生更改时迭代集合一次,并将每个项目发布到任何相应的主题。
有没有更简单,更习惯的方式来实现这一点,使用底层的RxJS操作符?关键要求是一次只迭代基础集合。
不确定传递的链接是否会将您引导至实际的解决方案,因此我在此提供有关可能方法的更多详细信息。这个想法是使用运营商groupBy
,比较。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/groupby.md,http://reactivex.io/documentation/operators/groupby.html为弹子,如果你喜欢这里的测试规格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
是由项目并且为了
比照发射阵列内容项的特技http://xgrommx.github.io/rx-book/content/observable/observable_instance_methods/groupby.html – user3743222