@converse/skeletor 3.1.4 → 3.1.5

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.
@@ -27,11 +27,6 @@ export class NodeSQLiteStorage implements StorageDriver {
27
27
  return Promise.resolve();
28
28
  }
29
29
 
30
- debouncedSetItems?: {
31
- (items: Record<string, any>): Promise<void>;
32
- flush?: () => void;
33
- };
34
-
35
30
  constructor(name: string, storageDir?: string, inMemory?: boolean) {
36
31
  if (inMemory) {
37
32
  this.db = new DatabaseSync(':memory:');
@@ -116,7 +111,7 @@ export class NodeSQLiteStorage implements StorageDriver {
116
111
  return Promise.resolve(rows.map((r) => r.key));
117
112
  }
118
113
 
119
- setItems(items: Record<string, any>): Promise<void> {
114
+ setItems(items: Record<string, any>): Promise<Record<string, any>> {
120
115
  this.db.exec('BEGIN IMMEDIATE');
121
116
  try {
122
117
  for (const [key, value] of Object.entries(items)) {
@@ -137,7 +132,7 @@ export class NodeSQLiteStorage implements StorageDriver {
137
132
  }
138
133
  throw e;
139
134
  }
140
- return Promise.resolve();
135
+ return Promise.resolve(items);
141
136
  }
142
137
 
143
138
  getItems(keys: string[]): Promise<Record<string, any>> {
@@ -11,10 +11,16 @@ export interface StorageDriver {
11
11
  length(): Promise<number>;
12
12
  key(keyIndex: number): Promise<string | null>;
13
13
  keys(): Promise<string[]>;
14
- setItems?(items: Record<string, any>): Promise<void>;
14
+ /**
15
+ * Write several items in one go. A successful write is signalled by the
16
+ * promise settling, not by what it resolves with: drivers may resolve with
17
+ * the written items (localForage does) or with nothing, and callers must
18
+ * treat both as success.
19
+ */
20
+ setItems?(items: Record<string, any>): Promise<Record<string, any> | void>;
15
21
  getItems?(keys: string[]): Promise<Record<string, any>>;
16
22
  debouncedSetItems?: {
17
- (items: Record<string, any>): Promise<void>;
23
+ (items: Record<string, any>): Promise<Record<string, any> | void>;
18
24
  flush?: () => void;
19
25
  };
20
26
  }
package/src/storage.ts CHANGED
@@ -8,7 +8,7 @@ import localForage from 'localforage';
8
8
  import mergebounce from 'mergebounce';
9
9
  import sessionStorageWrapper from './drivers/sessionStorage';
10
10
  import { extendPrototype as extendPrototypeWithSetItems } from 'localforage-setitems';
11
- import { extendPrototype as extendPrototypeWithGetItems } from '@converse/localforage-getitems/dist/localforage-getitems.es6';
11
+ import { extendPrototype as extendPrototypeWithGetItems } from '@converse/localforage-getitems';
12
12
  import { guid } from './helpers';
13
13
  import type { Model } from './model';
14
14
  import type { Collection } from './collection';
@@ -231,31 +231,16 @@ class PersistentStorage {
231
231
  }
232
232
  }
233
233
 
234
- if (resp) {
235
- if (options && options.success) {
236
- // When storing, we don't pass back the response (which is
237
- // the set attributes returned from localforage because
238
- // Skeletor sets them again on the model and due to the async
239
- // nature of localforage it can cause stale attributes to be
240
- // set on a model after it's been updated in the meantime.
241
- const data = method === 'read' ? resp : null;
242
- options.success(data, options);
243
- }
244
- } else if (method === 'read' && !caughtError) {
245
- // A read that finds no record is a normal empty result (first run /
246
- // empty storage), not an error. Resolve with `null` so the callback
247
- // and promise forms of fetch() agree and a first-run read never
248
- // rejects.
249
- if (options && options.success) {
250
- options.success(null, options);
251
- }
252
- } else {
253
- // A genuine failure (storage threw, or a write produced no result):
254
- // surface a real Error so consumers get a stack and `instanceof Error`
255
- // holds.
256
- if (options && options.error) {
257
- options.error(caughtError ?? new Error('Record Not Found'));
258
- }
234
+ if (caughtError) {
235
+ options?.error?.(caughtError);
236
+ } else if (options?.success) {
237
+ // When storing, we don't pass back the response (which is the set
238
+ // attributes returned from localforage) because Skeletor sets them
239
+ // again on the model and due to the async nature of localforage it can
240
+ // cause stale attributes to be set on a model after it's been updated
241
+ // in the meantime.
242
+ const data = method === 'read' ? (resp ?? null) : null;
243
+ options.success(data, options);
259
244
  }
260
245
  }
261
246
  localSync.__name__ = 'localSync';