1
1
import { ensureIndexForExpression } from "../indexes/auto-index.js"
2
- import { and } from "../query/builder/functions.js"
2
+ import { and , gt , lt } from "../query/builder/functions.js"
3
+ import { Value } from "../query/ir.js"
3
4
import {
4
5
createFilterFunctionFromExpression ,
5
6
createFilteredCallback ,
6
7
} from "./change-events.js"
7
- import type { BasicExpression } from "../query/ir.js"
8
+ import type { BasicExpression , OrderBy } from "../query/ir.js"
8
9
import type { IndexInterface } from "../indexes/base-index.js"
9
10
import type { ChangeMessage } from "../types.js"
10
11
import type { CollectionImpl } from "./index.js"
@@ -15,8 +16,9 @@ type RequestSnapshotOptions = {
15
16
}
16
17
17
18
type RequestLimitedSnapshotOptions = {
18
- minValue ?: any
19
+ orderBy : OrderBy
19
20
limit : number
21
+ minValue ?: any
20
22
}
21
23
22
24
type CollectionSubscriptionOptions = {
@@ -117,6 +119,13 @@ export class CollectionSubscription {
117
119
this . loadedInitialState = true
118
120
}
119
121
122
+ // Request the sync layer to load more data
123
+ // don't await it, we will load the data into the collection when it comes in
124
+ this . collection . syncMore ( {
125
+ where : stateOpts . where ,
126
+ } )
127
+
128
+ // Also load data immediately from the collection
120
129
const snapshot = this . collection . currentStateAsChanges ( stateOpts )
121
130
122
131
if ( snapshot === undefined ) {
@@ -140,7 +149,11 @@ export class CollectionSubscription {
140
149
* It uses that range index to load the items in the order of the index.
141
150
* Note: it does not send keys that have already been sent before.
142
151
*/
143
- requestLimitedSnapshot ( { limit, minValue } : RequestLimitedSnapshotOptions ) {
152
+ requestLimitedSnapshot ( {
153
+ orderBy,
154
+ limit,
155
+ minValue,
156
+ } : RequestLimitedSnapshotOptions ) {
144
157
if ( ! limit ) throw new Error ( `limit is required` )
145
158
146
159
if ( ! this . orderByIndex ) {
@@ -190,6 +203,23 @@ export class CollectionSubscription {
190
203
}
191
204
192
205
this . callback ( changes )
206
+
207
+ let whereWithValueFilter = where
208
+ if ( typeof minValue !== `undefined` ) {
209
+ // Only request data that we haven't seen yet (i.e. is bigger than the minValue)
210
+ const { expression, compareOptions } = orderBy [ 0 ] !
211
+ const operator = compareOptions . direction === `asc` ? gt : lt
212
+ const valueFilter = operator ( expression , new Value ( minValue ) )
213
+ whereWithValueFilter = where ? and ( where , valueFilter ) : valueFilter
214
+ }
215
+
216
+ // Request the sync layer to load more data
217
+ // don't await it, we will load the data into the collection when it comes in
218
+ this . collection . syncMore ( {
219
+ where : whereWithValueFilter ,
220
+ limit,
221
+ orderBy,
222
+ } )
193
223
}
194
224
195
225
/**
0 commit comments