@roc-db/indexed-db 0.2.0-pre.97 → 0.2.0-pre.99

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
@@ -69,11 +69,14 @@ var batchReadEntities = (txn, refs) => {
69
69
  return Promise.all(refs.map((ref) => readEntity(txn, ref)));
70
70
  };
71
71
  // src/functions/begin.ts
72
- var begin = async (engineOpts, callback) => {
73
- return new Promise((resolve, reject) => {
74
- let db;
75
- const idbRequest = indexedDB.open(engineOpts.dbName, 1);
76
- idbRequest.onupgradeneeded = (event) => {
72
+ var DB_CACHE = new WeakMap;
73
+ var openDatabase = (engineOpts) => {
74
+ let cached = DB_CACHE.get(engineOpts);
75
+ if (cached)
76
+ return cached;
77
+ const promise = new Promise((resolve, reject) => {
78
+ const idbRequest = indexedDB.open(engineOpts.dbName, engineOpts.version);
79
+ idbRequest.onupgradeneeded = () => {
77
80
  const entitiesObjectStore = idbRequest.result.createObjectStore("entities", { keyPath: "ref" });
78
81
  const mutationsObjectStore = idbRequest.result.createObjectStore("mutations", { keyPath: "ref" });
79
82
  entitiesObjectStore.createIndex("unique_constraint_0", ["entity", "unique_constraint_0"], { unique: true });
@@ -88,15 +91,72 @@ var begin = async (engineOpts, callback) => {
88
91
  unique: false
89
92
  });
90
93
  };
91
- idbRequest.onsuccess = (event) => {
92
- db = idbRequest.result;
93
- resolve(callback({
94
- ...engineOpts,
95
- db,
96
- txn: db.transaction(["entities", "mutations"], "readwrite")
97
- }));
94
+ idbRequest.onsuccess = () => {
95
+ const db = idbRequest.result;
96
+ db.onversionchange = () => {
97
+ db.close();
98
+ DB_CACHE.delete(engineOpts);
99
+ };
100
+ resolve(db);
101
+ };
102
+ idbRequest.onerror = () => {
103
+ DB_CACHE.delete(engineOpts);
104
+ reject(idbRequest.error);
98
105
  };
99
106
  });
107
+ DB_CACHE.set(engineOpts, promise);
108
+ return promise;
109
+ };
110
+ var begin = async (engineOpts, callback) => {
111
+ const db = await openDatabase(engineOpts);
112
+ return new Promise((resolve, reject) => {
113
+ const txn = db.transaction(["entities", "mutations"], "readwrite");
114
+ let callbackResult;
115
+ let callbackDone = false;
116
+ let txnComplete = false;
117
+ let settled = false;
118
+ const maybeResolve = () => {
119
+ if (settled)
120
+ return;
121
+ if (callbackDone && txnComplete) {
122
+ settled = true;
123
+ resolve(callbackResult);
124
+ }
125
+ };
126
+ txn.oncomplete = () => {
127
+ txnComplete = true;
128
+ maybeResolve();
129
+ };
130
+ txn.onerror = (event) => {
131
+ if (settled)
132
+ return;
133
+ settled = true;
134
+ reject(txn.error ?? event.target?.error);
135
+ };
136
+ txn.onabort = () => {
137
+ if (settled)
138
+ return;
139
+ settled = true;
140
+ reject(txn.error ?? new Error("Transaction aborted"));
141
+ };
142
+ Promise.resolve(callback({
143
+ ...engineOpts,
144
+ db,
145
+ txn
146
+ })).then((result) => {
147
+ callbackResult = result;
148
+ callbackDone = true;
149
+ maybeResolve();
150
+ }).catch((error) => {
151
+ if (settled)
152
+ return;
153
+ settled = true;
154
+ try {
155
+ txn.abort();
156
+ } catch {}
157
+ reject(error);
158
+ });
159
+ });
100
160
  };
101
161
  // src/functions/commitCreate.ts
102
162
  import {
@@ -111,6 +171,8 @@ var commitCreate = (txn, document) => {
111
171
  };
112
172
  request.onerror = (event) => {
113
173
  if (event?.target?.error?.name === "ConstraintError") {
174
+ event.stopPropagation();
175
+ event.preventDefault();
114
176
  reject(createUniqueConstraintConflictError(document.entity));
115
177
  } else {
116
178
  console.error("Error creating entity:", document, event?.target?.error);
@@ -250,7 +312,7 @@ var getChangeSetMutations = async (txn, changeSetRef) => {
250
312
  });
251
313
  };
252
314
  // src/functions/findDebounceMutation.ts
253
- var findDebounceMutation = async (request, engine, now, mutationName) => {
315
+ var findDebounceMutation = async (request, engine, now, mutationName, identityRef) => {
254
316
  const objectStore = engine.txn.objectStore("mutations");
255
317
  const index = objectStore.index("timestamp");
256
318
  const timestamp = new Date(now - request.operation.debounce * 1000).toISOString();
@@ -262,21 +324,21 @@ var findDebounceMutation = async (request, engine, now, mutationName) => {
262
324
  const cursor = idbRequest.result;
263
325
  if (cursor) {
264
326
  const doc = cursor.value;
265
- if (doc.operation.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef) {
327
+ if (doc.operation.name === mutationName && doc.payload.ref === request.payload.ref && doc.changeSetRef === request.changeSetRef && doc.identityRef === identityRef) {
266
328
  results.push(doc);
267
329
  }
268
330
  cursor.continue();
269
331
  } else {
270
- if (results.length) {
271
- if (results.length > 1) {
272
- throw new Error("Unhandled multiple debounced mutations");
273
- }
274
- resolve(results[0]);
275
- } else {
276
- resolve(undefined);
332
+ if (results.length > 1) {
333
+ reject(new Error("Unhandled multiple debounced mutations"));
334
+ return;
277
335
  }
336
+ resolve(results[0]);
278
337
  }
279
338
  };
339
+ idbRequest.onerror = () => {
340
+ reject(idbRequest.error);
341
+ };
280
342
  });
281
343
  };
282
344
  // src/functions/pageEntities.ts
@@ -392,7 +454,8 @@ var createIndexedDBAdapter = ({
392
454
  entities,
393
455
  session,
394
456
  dbName = "roc-db",
395
- optimistic = false
457
+ optimistic = false,
458
+ snowflake = new Snowflake(1, 1)
396
459
  }) => {
397
460
  return createAdapter({
398
461
  name: "indexed-db",
@@ -401,7 +464,7 @@ var createIndexedDBAdapter = ({
401
464
  functions: exports_functions,
402
465
  optimistic,
403
466
  session,
404
- snowflake: new Snowflake(1, 1),
467
+ snowflake,
405
468
  async: true
406
469
  }, {
407
470
  dbName,
@@ -1,8 +1,9 @@
1
- import { type Adapter } from "roc-db";
2
- export declare const createIndexedDBAdapter: ({ operations, entities, session, dbName, optimistic, }: {
1
+ import { Snowflake, type Adapter } from "roc-db";
2
+ export declare const createIndexedDBAdapter: ({ operations, entities, session, dbName, optimistic, snowflake, }: {
3
3
  operations: any;
4
4
  entities: any;
5
5
  session: any;
6
6
  dbName?: string | undefined;
7
7
  optimistic?: boolean | undefined;
8
+ snowflake?: Snowflake | undefined;
8
9
  }) => Adapter;
@@ -1,3 +1,3 @@
1
1
  import type { WriteRequest } from "roc-db";
2
2
  import type { IndexedDBEngine } from "../types/IndexedDBEngine";
3
- export declare const findDebounceMutation: (request: WriteRequest, engine: IndexedDBEngine, now: number, mutationName: string) => Promise<unknown>;
3
+ export declare const findDebounceMutation: (request: WriteRequest, engine: IndexedDBEngine, now: number, mutationName: string, identityRef: string) => Promise<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roc-db/indexed-db",
3
- "version": "0.2.0-pre.97",
3
+ "version": "0.2.0-pre.99",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -14,7 +14,7 @@
14
14
  "sideEffects": false,
15
15
  "dependencies": {},
16
16
  "peerDependencies": {
17
- "roc-db": "0.2.0-pre.97",
17
+ "roc-db": ">=0.2.0-pre.98 <1.0.0",
18
18
  "zod": ">=4.0.0"
19
19
  },
20
20
  "keywords": [],
@@ -23,5 +23,9 @@
23
23
  "type": "git",
24
24
  "url": "git+https://github.com/eigilsagafos/roc-db.git"
25
25
  },
26
- "license": "MIT"
26
+ "license": "MIT",
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ }
27
31
  }