Skip to content

Commit 8d74020

Browse files
author
Bobby
authored
fix: run pagination in optimistic writes (#23)
fix: run pagination in optimistic writes
2 parents b4f52d9 + ea17e97 commit 8d74020

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/reducers/cacheReducer.js

+39-6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ const verbose = debug('rrfVerbose:cache');
100100
*/
101101

102102
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+
);
103108

104109
const PROCESSES = {
105110
'<': (a, b) => a < b,
@@ -272,16 +277,37 @@ const xfLimit = ({ limit, endAt, endBefore }) => {
272277
* @returns {xFormFilter} - transducer
273278
*/
274279
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;
276289

277-
const isOptimisticRead = !via || via === 'memory';
278290
const start = startAt || startAfter;
279291
const end = endAt || endBefore;
280292
const isAfter = startAfter !== undefined;
281293
const isBefore = endBefore !== undefined;
282294
const needsPagination = start || end || false;
283295

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+
}
285311

286312
const isFlat = typeof order[0] === 'string';
287313
const orders = isFlat ? [order] : order;
@@ -291,15 +317,20 @@ const xfPaginate = (query, getDoc) => {
291317
if (value === undefined) return false;
292318

293319
// 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;
297322
let compare = process['=='];
298323
if (startAt || endAt) compare = proc[sort === 'desc' ? '<=' : '>='];
299324
if (startAfter || endBefore) compare = proc[sort === 'desc' ? '<' : '>'];
300325

301326
const isMatched = compare(document[field], value);
302327
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+
}
303334
return true;
304335
}
305336
}) !== undefined;
@@ -309,6 +340,8 @@ const xfPaginate = (query, getDoc) => {
309340
let started = start === undefined;
310341

311342
tuples.forEach(([path, id]) => {
343+
if (limit && results.length >= limit) return;
344+
312345
if (!started && start) {
313346
if (isPaginateMatched(getDoc(path, id), start, undefined, isAfter)) {
314347
started = true;

0 commit comments

Comments
 (0)