Skip to content

Commit 8d8f4e7

Browse files
committed
Replace the Sorter class
1 parent ccba149 commit 8d8f4e7

File tree

6 files changed

+40
-298
lines changed

6 files changed

+40
-298
lines changed

apps/meteor/client/lib/cachedCollections/Cursor.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { createComparatorFromSort, type Filter, type Sort } from '@rocket.chat/mongo-adapter';
12
import { Tracker } from 'meteor/tracker';
2-
import type { Filter, Sort } from 'mongodb';
33

44
import { DiffSequence } from './DiffSequence';
55
import { IdMap } from './IdMap';
@@ -9,7 +9,6 @@ import { MinimongoError } from './MinimongoError';
99
import { ObserveHandle, ReactiveObserveHandle } from './ObserveHandle';
1010
import { OrderedDict } from './OrderedDict';
1111
import type { Query, OrderedQuery, IncompleteQuery, UnorderedQuery } from './Query';
12-
import { Sorter } from './Sorter';
1312
import { _isPlainObject, clone, hasOwn, isEqual, isPromiseLike } from './common';
1413

1514
type DispatchTransform<TTransform, T, TProjection> = TTransform extends (...args: any) => any
@@ -22,7 +21,7 @@ type DispatchTransform<TTransform, T, TProjection> = TTransform extends (...args
2221
export class Cursor<T extends { _id: string }, TOptions extends Options<T>, TProjection extends T = T> {
2322
private readonly matcher: Matcher<T>;
2423

25-
private readonly sorter: Sorter<T> | null;
24+
private readonly comparator: ((a: T, b: T) => number) | null;
2625

2726
readonly skip: number;
2827

@@ -42,7 +41,7 @@ export class Cursor<T extends { _id: string }, TOptions extends Options<T>, TPro
4241
options?: TOptions,
4342
) {
4443
this.matcher = new Matcher(selector);
45-
this.sorter = options?.sort ? new Sorter(options.sort) : null;
44+
this.comparator = options?.sort ? createComparatorFromSort(options.sort) : null;
4645
this.skip = options?.skip ?? 0;
4746
this.limit = options?.limit;
4847
this.fields = options?.projection ?? options?.fields;
@@ -364,7 +363,7 @@ export class Cursor<T extends { _id: string }, TOptions extends Options<T>, TPro
364363
ordered,
365364
projectionFn: this._projectionFn,
366365
resultsSnapshot: null,
367-
sorter: this.sorter,
366+
comparator: this.comparator,
368367
}
369368
: {
370369
cursor: this,
@@ -373,7 +372,7 @@ export class Cursor<T extends { _id: string }, TOptions extends Options<T>, TPro
373372
ordered,
374373
projectionFn: this._projectionFn,
375374
resultsSnapshot: null,
376-
sorter: null,
375+
comparator: null,
377376
};
378377

379378
query.results = this._getRawObjects({ ordered });
@@ -500,15 +499,15 @@ export class Cursor<T extends { _id: string }, TOptions extends Options<T>, TPro
500499
continue;
501500
}
502501

503-
if ((!this.limit || !!this.skip || !!this.sorter || (results as T[]).length !== this.limit) === false) break;
502+
if ((!this.limit || !!this.skip || !!this.comparator || (results as T[]).length !== this.limit) === false) break;
504503
}
505504

506505
if (!options.ordered) {
507506
return results;
508507
}
509508

510-
if (this.sorter) {
511-
(results as T[]).sort(this.sorter.getComparator());
509+
if (this.comparator) {
510+
(results as T[]).sort(this.comparator);
512511
}
513512

514513
if (!applySkipLimit || (!this.limit && !this.skip)) {

apps/meteor/client/lib/cachedCollections/LocalCollection.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { createComparatorFromSort, type Filter } from '@rocket.chat/mongo-adapter';
12
import { Meteor } from 'meteor/meteor';
2-
import type { CountDocumentsOptions, Filter, UpdateFilter } from 'mongodb';
3+
import type { CountDocumentsOptions, UpdateFilter } from 'mongodb';
34
import type { StoreApi, UseBoundStore } from 'zustand';
45

56
import { Cursor } from './Cursor';
@@ -9,7 +10,6 @@ import type { IdMap } from './IdMap';
910
import { Matcher } from './Matcher';
1011
import { MinimongoError } from './MinimongoError';
1112
import type { Query } from './Query';
12-
import { Sorter } from './Sorter';
1313
import { SynchronousQueue } from './SynchronousQueue';
1414
import {
1515
hasOwn,
@@ -140,11 +140,11 @@ export class LocalCollection<T extends { _id: string }> {
140140
delete fields._id;
141141

142142
if (query.ordered) {
143-
if (!query.sorter) {
143+
if (!query.comparator) {
144144
query.addedBefore(doc._id, query.projectionFn(fields), null);
145145
query.results.push(doc);
146146
} else {
147-
const i = this._insertInSortedList(query.sorter.getComparator(), query.results, doc);
147+
const i = this._insertInSortedList(query.comparator, query.results, doc);
148148

149149
const next = query.results[i + 1]?._id ?? null;
150150

@@ -164,11 +164,11 @@ export class LocalCollection<T extends { _id: string }> {
164164
delete fields._id;
165165

166166
if (query.ordered) {
167-
if (!query.sorter) {
167+
if (!query.comparator) {
168168
await query.addedBefore(doc._id, query.projectionFn(fields), null);
169169
query.results.push(doc);
170170
} else {
171-
const i = this._insertInSortedList(query.sorter.getComparator(), query.results, doc);
171+
const i = this._insertInSortedList(query.comparator, query.results, doc);
172172

173173
const next = query.results[i + 1]?._id ?? null;
174174

@@ -1043,13 +1043,13 @@ export class LocalCollection<T extends { _id: string }> {
10431043
query.changed(doc._id, changedFields);
10441044
}
10451045

1046-
if (!query.sorter) {
1046+
if (!query.comparator) {
10471047
return;
10481048
}
10491049

10501050
query.results.splice(oldIdx, 1);
10511051

1052-
const newIdx = this._insertInSortedList(query.sorter.getComparator(), query.results, doc);
1052+
const newIdx = this._insertInSortedList(query.comparator, query.results, doc);
10531053

10541054
if (oldIdx !== newIdx) {
10551055
const next = query.results[newIdx + 1]?._id ?? null;
@@ -1081,13 +1081,13 @@ export class LocalCollection<T extends { _id: string }> {
10811081
await query.changed(doc._id, changedFields);
10821082
}
10831083

1084-
if (!query.sorter) {
1084+
if (!query.comparator) {
10851085
return;
10861086
}
10871087

10881088
query.results.splice(oldIdx, 1);
10891089

1090-
const newIdx = this._insertInSortedList(query.sorter.getComparator(), query.results, doc);
1090+
const newIdx = this._insertInSortedList(query.comparator, query.results, doc);
10911091

10921092
if (oldIdx !== newIdx) {
10931093
const next = query.results[newIdx + 1]?._id ?? null;
@@ -1290,7 +1290,7 @@ const MODIFIERS = {
12901290
throw new MinimongoError('$sort requires $slice to be present', { field });
12911291
}
12921292

1293-
sortFunction = new Sorter(arg.$sort).getComparator();
1293+
sortFunction = createComparatorFromSort(arg.$sort);
12941294

12951295
for (const element of toPush) {
12961296
if (_f._type(element) !== 3) {

apps/meteor/client/lib/cachedCollections/Matcher.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import type { Filter } from 'mongodb';
1+
import type { Filter } from '@rocket.chat/mongo-adapter';
22

33
import { _selectorIsId, compileDocumentSelector, hasOwn, isBinary, isEqual, nothingMatcher } from './common';
44

55
/** @deprecated internal use only */
66
export class Matcher<T extends { _id: string }> {
7-
private _paths: Record<string, boolean>;
7+
private readonly _paths: Record<string, boolean>;
88

99
_hasWhere: boolean;
1010

1111
_isSimple: boolean;
1212

13-
private _docMatcher: (doc: T) => { result: boolean; arrayIndices?: (number | 'x')[] };
13+
private readonly _docMatcher: (doc: T) => { result: boolean; arrayIndices?: (number | 'x')[] };
1414

1515
constructor(selector: Filter<T> | T['_id'] | ((this: T) => boolean)) {
1616
this._paths = {};
@@ -27,15 +27,7 @@ export class Matcher<T extends { _id: string }> {
2727
return this._docMatcher(doc);
2828
}
2929

30-
hasWhere(): boolean {
31-
return this._hasWhere;
32-
}
33-
34-
isSimple(): boolean {
35-
return this._isSimple;
36-
}
37-
38-
_compileSelector(
30+
private _compileSelector(
3931
selector: ((this: T) => boolean) | string | { _id: undefined | null | false } | Record<string, unknown>,
4032
): (doc: T) => { result: boolean } {
4133
if (typeof selector === 'function') {
@@ -63,10 +55,6 @@ export class Matcher<T extends { _id: string }> {
6355
return compileDocumentSelector(selector, this);
6456
}
6557

66-
_getPaths(): string[] {
67-
return Object.keys(this._paths);
68-
}
69-
7058
_recordPathUsed(path: string): void {
7159
this._paths[path] = true;
7260
}

apps/meteor/client/lib/cachedCollections/Query.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import type { Cursor, Options } from './Cursor';
33
import type { IdMap } from './IdMap';
44
import type { Matcher } from './Matcher';
5-
import type { Sorter } from './Sorter';
65

76
interface BaseQuery<T extends { _id: string }, TOptions extends Options<T> = Options<T>, TProjection extends T = any> {
8-
cursor: Cursor<T, TOptions, TProjection>;
7+
readonly cursor: Cursor<T, TOptions, TProjection>;
98
dirty: boolean;
10-
matcher: Matcher<T>;
11-
projectionFn: (doc: T | Omit<T, '_id'>) => TProjection;
9+
readonly matcher: Matcher<T>;
10+
readonly projectionFn: (doc: T | Omit<T, '_id'>) => TProjection;
1211
}
1312

1413
type AddedCallback<T extends { _id: string }, TProjection> = (id: T['_id'], fields: TProjection) => void | Promise<void>;
@@ -24,8 +23,8 @@ type MovedBeforeCallback<T extends { _id: string }> = (id: T['_id'], before: T['
2423
/** @deprecated internal use only */
2524
export interface IncompleteUnorderedQuery<T extends { _id: string }, TOptions extends Options<T> = Options<T>, TProjection extends T = any>
2625
extends BaseQuery<T, TOptions, TProjection> {
27-
sorter: null;
28-
ordered: false;
26+
readonly comparator: null;
27+
readonly ordered: false;
2928
results?: IdMap<T['_id'], T>;
3029
resultsSnapshot?: IdMap<T['_id'], T> | null;
3130
added?: AddedCallback<T, TProjection>;
@@ -38,16 +37,16 @@ export interface UnorderedQuery<T extends { _id: string }, TOptions extends Opti
3837
extends IncompleteUnorderedQuery<T, TOptions, TProjection> {
3938
results: IdMap<T['_id'], T>;
4039
resultsSnapshot: IdMap<T['_id'], T> | null;
41-
added: AddedCallback<T, TProjection>;
42-
changed: ChangedCallback<T, TProjection>;
43-
removed: RemovedCallback<T>;
40+
readonly added: AddedCallback<T, TProjection>;
41+
readonly changed: ChangedCallback<T, TProjection>;
42+
readonly removed: RemovedCallback<T>;
4443
}
4544

4645
/** @deprecated internal use only */
4746
export interface IncompleteOrderedQuery<T extends { _id: string }, TOptions extends Options<T> = Options<T>, TProjection extends T = any>
4847
extends BaseQuery<T, TOptions, TProjection> {
49-
ordered: true;
50-
sorter: Sorter<T> | null;
48+
readonly ordered: true;
49+
readonly comparator: ((a: T, b: T) => number) | null;
5150
results?: T[];
5251
resultsSnapshot?: T[] | null;
5352
added?: AddedCallback<T, TProjection>;
@@ -62,11 +61,11 @@ export interface OrderedQuery<T extends { _id: string }, TOptions extends Option
6261
extends IncompleteOrderedQuery<T, TOptions, TProjection> {
6362
results: T[];
6463
resultsSnapshot: T[] | null;
65-
added: AddedCallback<T, TProjection>;
66-
changed: ChangedCallback<T, TProjection>;
67-
removed: RemovedCallback<T>;
68-
addedBefore: AddedBeforeCallback<T, TProjection>;
69-
movedBefore: MovedBeforeCallback<T>;
64+
readonly added: AddedCallback<T, TProjection>;
65+
readonly changed: ChangedCallback<T, TProjection>;
66+
readonly removed: RemovedCallback<T>;
67+
readonly addedBefore: AddedBeforeCallback<T, TProjection>;
68+
readonly movedBefore: MovedBeforeCallback<T>;
7069
}
7170

7271
/** @deprecated internal use only */

0 commit comments

Comments
 (0)