@roc-db/indexed-db 0.2.0-pre.100 → 0.2.0-pre.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ __export(exports_functions, {
26
26
  pageEntitiesByIndex: () => pageEntitiesByIndex,
27
27
  pageEntities: () => pageEntities,
28
28
  getChangeSetMutations: () => getChangeSetMutations,
29
- findDebounceMutation: () => findDebounceMutation,
29
+ findRecentMutations: () => findRecentMutations,
30
30
  end: () => end,
31
31
  commit: () => commit,
32
32
  begin: () => begin,
@@ -314,35 +314,27 @@ var getChangeSetMutations = (txn, changeSetRef) => {
314
314
  };
315
315
  });
316
316
  };
317
- // src/functions/findDebounceMutation.ts
318
- var findDebounceMutation = (request, engine, now, mutationName, identityRef) => {
317
+ // src/functions/findRecentMutations.ts
318
+ var collect = (idbRequest, results) => new Promise((resolve, reject) => {
319
+ idbRequest.onsuccess = () => {
320
+ const cursor = idbRequest.result;
321
+ if (cursor) {
322
+ results.set(cursor.value.ref, cursor.value);
323
+ cursor.continue();
324
+ } else {
325
+ resolve();
326
+ }
327
+ };
328
+ idbRequest.onerror = () => reject(idbRequest.error);
329
+ });
330
+ var findRecentMutations = async (engine, { changeSetRef, identityRef, since }) => {
319
331
  const objectStore = engine.txn.objectStore("mutations");
320
- const index = objectStore.index("timestamp");
321
- const timestamp = new Date(now.getTime() - request.operation.debounce * 1000).toISOString();
322
- const range = IDBKeyRange.lowerBound(timestamp, true);
323
- const idbRequest = index.openCursor(range);
324
- return new Promise((resolve, reject) => {
325
- const results = [];
326
- idbRequest.onsuccess = () => {
327
- const cursor = idbRequest.result;
328
- if (cursor) {
329
- const doc = cursor.value;
330
- if (doc.operation.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef && doc.identityRef === identityRef) {
331
- results.push(doc);
332
- }
333
- cursor.continue();
334
- } else {
335
- if (results.length > 1) {
336
- reject(new Error("Unhandled multiple debounced mutations"));
337
- return;
338
- }
339
- resolve(results[0]);
340
- }
341
- };
342
- idbRequest.onerror = () => {
343
- reject(idbRequest.error);
344
- };
345
- });
332
+ const results = new Map;
333
+ await Promise.all([
334
+ collect(objectStore.index("timestamp").openCursor(IDBKeyRange.lowerBound(since, true)), results),
335
+ changeSetRef && collect(objectStore.index("byChangeSetRef").openCursor(IDBKeyRange.only(changeSetRef)), results)
336
+ ]);
337
+ return [...results.values()].filter((mutation) => (mutation.timestamp > since || !!mutation.persistedAt && mutation.persistedAt > since) && ((mutation.changeSetRef ?? null) === changeSetRef || mutation.identityRef === identityRef));
346
338
  };
347
339
  // src/functions/pageEntities.ts
348
340
  var pageEntities = async (txn, args) => {
@@ -0,0 +1,2 @@
1
+ import type { FindRecentMutationsFunction } from "roc-db";
2
+ export declare const findRecentMutations: FindRecentMutationsFunction;
@@ -3,7 +3,7 @@ export { begin } from "./begin";
3
3
  export { commit } from "./commit";
4
4
  export { end } from "./end";
5
5
  export { getChangeSetMutations } from "./getChangeSetMutations";
6
- export { findDebounceMutation } from "./findDebounceMutation";
6
+ export { findRecentMutations } from "./findRecentMutations";
7
7
  export { pageEntities } from "./pageEntities";
8
8
  export { pageEntitiesByIndex } from "./pageEntitiesByIndex";
9
9
  export { pageMutations } from "./pageMutations";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/indexed-db",
3
- "version": "0.2.0-pre.100",
3
+ "version": "0.2.0-pre.101",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -1,2 +0,0 @@
1
- import type { FindDebounceMutationFunction } from "roc-db";
2
- export declare const findDebounceMutation: FindDebounceMutationFunction;