@converse/skeletor 3.1.3 → 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.
- package/CHANGES.md +18 -0
- package/README.md +12 -5
- package/dist/skeletor.d.ts +11 -5
- package/dist/skeletor.esm.js +263 -250
- package/dist/skeletor.esm.js.map +1 -1
- package/dist/skeletor.js +263 -250
- package/dist/skeletor.js.map +1 -1
- package/dist/skeletor.min.js +4 -4
- package/dist/skeletor.node.js +264 -251
- package/dist/skeletor.node.mjs +264 -251
- package/package.json +3 -1
- package/src/collection.ts +5 -5
- package/src/declarations.d.ts +11 -0
- package/src/drivers/nodeSQLiteStorage.ts +2 -7
- package/src/drivers/types.ts +8 -2
- package/src/model.ts +6 -6
- package/src/storage.ts +11 -26
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"browser": "dist/skeletor.js",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"types": "./dist/skeletor.d.ts",
|
|
12
13
|
"node": {
|
|
13
14
|
"require": "./dist/skeletor.node.js",
|
|
14
15
|
"import": "./dist/skeletor.node.mjs"
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
}
|
|
20
21
|
},
|
|
21
22
|
"./node": {
|
|
23
|
+
"types": "./dist/skeletor.d.ts",
|
|
22
24
|
"require": "./dist/skeletor.node.js",
|
|
23
25
|
"import": "./dist/skeletor.node.mjs"
|
|
24
26
|
}
|
|
@@ -36,7 +38,7 @@
|
|
|
36
38
|
"contributors": [
|
|
37
39
|
"JC Brand"
|
|
38
40
|
],
|
|
39
|
-
"version": "3.1.
|
|
41
|
+
"version": "3.1.5",
|
|
40
42
|
"engines": {
|
|
41
43
|
"node": ">=22.0.0"
|
|
42
44
|
},
|
package/src/collection.ts
CHANGED
|
@@ -40,7 +40,7 @@ export class Collection<T extends Model = Model> extends EventEmitterObject {
|
|
|
40
40
|
_storage?: PersistentStorage;
|
|
41
41
|
_comparator?: Comparator<T>;
|
|
42
42
|
_url: string = '';
|
|
43
|
-
|
|
43
|
+
hydrated?: Promise<void>;
|
|
44
44
|
models: T[];
|
|
45
45
|
protected _byId: Record<string, T>;
|
|
46
46
|
protected _model?: new (attributes?: Partial<ModelAttributes>, options?: ModelOptions) => T;
|
|
@@ -61,11 +61,11 @@ export class Collection<T extends Model = Model> extends EventEmitterObject {
|
|
|
61
61
|
if (models) this.reset(models, Object.assign({ silent: true }, options));
|
|
62
62
|
|
|
63
63
|
this[Symbol.iterator] = this.values;
|
|
64
|
-
// When autoSync is on, `
|
|
65
|
-
// uniformly `await collection.
|
|
64
|
+
// When autoSync is on, `hydrated` is always a Promise so callers can
|
|
65
|
+
// uniformly `await collection.hydrated`; it resolves immediately when
|
|
66
66
|
// there's no storage to hydrate from.
|
|
67
67
|
if (this.autoSync) {
|
|
68
|
-
this.
|
|
68
|
+
this.hydrated = getStorage(this) ? this.#hydrate() : Promise.resolve();
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
@@ -104,7 +104,7 @@ export class Collection<T extends Model = Model> extends EventEmitterObject {
|
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
106
|
* Override to return `true` to automatically load the collection from
|
|
107
|
-
* storage on construction. Await `
|
|
107
|
+
* storage on construction. Await `hydrated` to know when hydration
|
|
108
108
|
* is complete.
|
|
109
109
|
*
|
|
110
110
|
* Note: collection `autoSync` is hydrate-only. Unlike `Model`, it does
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@converse/localforage-getitems` ships its types as an ambient script that
|
|
3
|
+
* declares the *unscoped* module name (`localforage-getitems`), left over from
|
|
4
|
+
* the package it was forked from. TypeScript therefore resolves the scoped
|
|
5
|
+
* specifier to a `typings/index.d.ts` that isn't a module (TS2306), so declare
|
|
6
|
+
* the shape we use here instead.
|
|
7
|
+
*/
|
|
8
|
+
declare module '@converse/localforage-getitems' {
|
|
9
|
+
export function extendPrototype<T>(localforage: T): T;
|
|
10
|
+
export const extendPrototypeResult: boolean;
|
|
11
|
+
}
|
|
@@ -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<
|
|
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>> {
|
package/src/drivers/types.ts
CHANGED
|
@@ -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
|
-
|
|
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/model.ts
CHANGED
|
@@ -58,7 +58,7 @@ export class Model<T extends ModelAttributes = ModelAttributes> extends EventEmi
|
|
|
58
58
|
cid: string;
|
|
59
59
|
collection?: Collection;
|
|
60
60
|
id: string | number;
|
|
61
|
-
|
|
61
|
+
hydrated?: Promise<void>;
|
|
62
62
|
validationError: string | number | null = null;
|
|
63
63
|
|
|
64
64
|
/**
|
|
@@ -102,17 +102,17 @@ export class Model<T extends ModelAttributes = ModelAttributes> extends EventEmi
|
|
|
102
102
|
// Reset changed after initial set
|
|
103
103
|
this.changed = {};
|
|
104
104
|
|
|
105
|
-
// When autoSync is on, `
|
|
106
|
-
// uniformly `await model.
|
|
105
|
+
// When autoSync is on, `hydrated` is always a Promise so callers can
|
|
106
|
+
// uniformly `await model.hydrated`. It only hydrates from storage when
|
|
107
107
|
// there's something to load (storage configured and the model isn't new);
|
|
108
108
|
// otherwise it's already ready and resolves immediately.
|
|
109
109
|
if (this.autoSync) {
|
|
110
110
|
if (getStorage(this) && !this.isNew()) {
|
|
111
111
|
this.#state = 'hydrating';
|
|
112
|
-
this.
|
|
112
|
+
this.hydrated = this.#hydrate();
|
|
113
113
|
} else {
|
|
114
114
|
this.#state = 'ready';
|
|
115
|
-
this.
|
|
115
|
+
this.hydrated = Promise.resolve();
|
|
116
116
|
}
|
|
117
117
|
} else {
|
|
118
118
|
this.#state = 'ready';
|
|
@@ -347,7 +347,7 @@ export class Model<T extends ModelAttributes = ModelAttributes> extends EventEmi
|
|
|
347
347
|
// fromStorage lives in `val` (second arg), not `options` (third arg).
|
|
348
348
|
if (this.autoSync && this.#state === 'hydrating' && !options.fromStorage) {
|
|
349
349
|
throw new Error(
|
|
350
|
-
`Skeletor: set() called on an autoSync model before
|
|
350
|
+
`Skeletor: set() called on an autoSync model before hydration resolved. Await model.hydrated first.`,
|
|
351
351
|
);
|
|
352
352
|
}
|
|
353
353
|
|
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
|
|
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 (
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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';
|