Skip to content

Commit

Permalink
♻️ refactor(api/makeFilteredCollection): Leverage observeSetChanges.
Browse files Browse the repository at this point in the history
Replaces `meteor/mongo.Cursor#observeChanges`.
  • Loading branch information
make-github-pseudonymous-again committed Jul 7, 2024
1 parent 8810601 commit f89eafb
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions imports/api/makeFilteredCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import useSubscription from './publication/useSubscription';
import {AuthenticationLoggedIn} from './Authentication';
import {userFilter} from './query/UserFilter';
import type UserFilter from './query/UserFilter';
import observeSetChanges from './query/observeSetChanges';
import type Filter from './query/Filter';

const makeFilteredCollection = <
S extends schema.ZodTypeAny,
Expand All @@ -32,15 +34,15 @@ const makeFilteredCollection = <
userFilter(tSchema).nullable(),
options(tSchema).nullable(),
]),
handle(
async handle(
publicationFilter: UserFilter<schema.infer<S>> | null,
publicationOptions: Options<schema.infer<S>> | null,
) {
const selector = {
const scopedFilter = {

Check warning on line 41 in imports/api/makeFilteredCollection.ts

View check run for this annotation

Codecov / codecov/patch

imports/api/makeFilteredCollection.ts#L41

Added line #L41 was not covered by tests
...filterSelector,
...publicationFilter,
owner: this.userId,
} as Selector<schema.infer<S>>;
} as Filter<schema.infer<S>>;

const options = {
...filterOptions,
Expand All @@ -49,22 +51,27 @@ const makeFilteredCollection = <
limit: 0,
};

const handle = collection.find(selector, options).observeChanges({
added: (_id, fields) => {
this.added(name, _id, fields);
},
const handle = await observeSetChanges(
collection,
scopedFilter,
options,
{
added: (_id, fields) => {
this.added(name, _id, fields);
},

changed: (_id, fields) => {
this.changed(name, _id, fields);
},
changed: (_id, fields) => {
this.changed(name, _id, fields);
},

removed: (_id) => {
this.removed(name, _id);
removed: (_id) => {
this.removed(name, _id);
},
},
});
);

this.onStop(() => {
handle.stop();
this.onStop(async () => {
await handle.stop();
});
this.ready();
},
Expand Down

0 comments on commit f89eafb

Please sign in to comment.