Skip to content

Commit

Permalink
feat(recs): allow multiple subscribers per query update$
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 29, 2022
1 parent 98b0861 commit 6d13531
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/recs/src/Query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { filterNullish } from "@latticexyz/utils";
import { observable, ObservableSet } from "mobx";
import { concat, concatMap, filter, from, map, merge, Observable, of } from "rxjs";
import { concat, concatMap, filter, from, map, merge, Observable, of, Subject } from "rxjs";
import {
componentValueEquals,
getComponentEntities,
Expand Down Expand Up @@ -431,7 +431,8 @@ export function defineQuery(
const containsProxy =
fragments.findIndex((v) => [QueryFragmentType.ProxyExpand, QueryFragmentType.ProxyRead].includes(v.type)) !== -1;

const update$ = concat(
const update$ = new Subject<ComponentUpdate & { type: UpdateType }>();
const subscription = concat(
initial$,
merge(...fragments.map((f) => f.component.update$)) // Combine all component update streams accessed accessed in this query
.pipe(
Expand Down Expand Up @@ -510,11 +511,14 @@ export function defineQuery(
}),
filterNullish()
)
);
).subscribe(update$);

const world = fragments[0].component.world;
world.registerDisposer(() => subscription?.unsubscribe());

return {
matching,
update$,
update$: update$.asObservable(),
};
}

Expand Down

0 comments on commit 6d13531

Please sign in to comment.