@@ -100,6 +100,11 @@ const verbose = debug('rrfVerbose:cache');
100
100
*/
101
101
102
102
const isTimestamp = ( a ) => a instanceof Object && a . seconds !== undefined ;
103
+ const formatTimestamp = ( { seconds } = { } ) =>
104
+ seconds &&
105
+ new Intl . DateTimeFormat ( 'en-US' , { dateStyle : 'short' } ) . format (
106
+ new Date ( seconds * 1000 ) ,
107
+ ) ;
103
108
104
109
const PROCESSES = {
105
110
'<' : ( a , b ) => a < b ,
@@ -272,16 +277,37 @@ const xfLimit = ({ limit, endAt, endBefore }) => {
272
277
* @returns {xFormFilter } - transducer
273
278
*/
274
279
const xfPaginate = ( query , getDoc ) => {
275
- const { orderBy : order , startAt, startAfter, endAt, endBefore, via } = query ;
280
+ const {
281
+ orderBy : order ,
282
+ limit,
283
+ startAt,
284
+ startAfter,
285
+ endAt,
286
+ endBefore,
287
+ via,
288
+ } = query ;
276
289
277
- const isOptimisticRead = ! via || via === 'memory' ;
278
290
const start = startAt || startAfter ;
279
291
const end = endAt || endBefore ;
280
292
const isAfter = startAfter !== undefined ;
281
293
const isBefore = endBefore !== undefined ;
282
294
const needsPagination = start || end || false ;
283
295
284
- if ( ! needsPagination || ! order || ! isOptimisticRead ) return identity ;
296
+ if ( ! needsPagination || ! order ) return identity ;
297
+
298
+ let prop = null ;
299
+ if ( verbose . enabled ) {
300
+ if ( startAt ) prop = 'startAt' ;
301
+ else if ( startAfter ) prop = 'startAfter' ;
302
+ else if ( endAt ) prop = 'endAt' ;
303
+ else if ( endBefore ) prop = 'endBefore' ;
304
+
305
+ verbose (
306
+ `paginate ${ prop } :${ formatTimestamp ( needsPagination ) } ` +
307
+ `order:[${ query ?. orderBy ?. [ 0 ] } , ${ query ?. orderBy ?. [ 1 ] } ] ` +
308
+ `via:${ via } ` ,
309
+ ) ;
310
+ }
285
311
286
312
const isFlat = typeof order [ 0 ] === 'string' ;
287
313
const orders = isFlat ? [ order ] : order ;
@@ -291,15 +317,20 @@ const xfPaginate = (query, getDoc) => {
291
317
if ( value === undefined ) return false ;
292
318
293
319
// TODO: add support for document refs
294
- const proc = isTimestamp ( document [ field ] )
295
- ? PROCESSES_TIMESTAMP
296
- : PROCESSES ;
320
+ const isTime = isTimestamp ( document [ field ] ) ;
321
+ const proc = isTime ? PROCESSES_TIMESTAMP : PROCESSES ;
297
322
let compare = process [ '==' ] ;
298
323
if ( startAt || endAt ) compare = proc [ sort === 'desc' ? '<=' : '>=' ] ;
299
324
if ( startAfter || endBefore ) compare = proc [ sort === 'desc' ? '<' : '>' ] ;
300
325
301
326
const isMatched = compare ( document [ field ] , value ) ;
302
327
if ( isMatched ) {
328
+ if ( verbose . enabled ) {
329
+ const val = isTime
330
+ ? formatTimestamp ( document [ field ] )
331
+ : document [ field ] ;
332
+ verbose ( `${ prop } : ${ document . id } .${ field } = ${ val } ` ) ;
333
+ }
303
334
return true ;
304
335
}
305
336
} ) !== undefined ;
@@ -309,6 +340,8 @@ const xfPaginate = (query, getDoc) => {
309
340
let started = start === undefined ;
310
341
311
342
tuples . forEach ( ( [ path , id ] ) => {
343
+ if ( limit && results . length >= limit ) return ;
344
+
312
345
if ( ! started && start ) {
313
346
if ( isPaginateMatched ( getDoc ( path , id ) , start , undefined , isAfter ) ) {
314
347
started = true ;
0 commit comments