@converse/skeletor 0.0.7 → 0.0.9

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.
Files changed (44) hide show
  1. package/README.md +38 -25
  2. package/dist/skeletor.js +10413 -6875
  3. package/dist/skeletor.js.map +1 -1
  4. package/dist/skeletor.min.js +74 -3
  5. package/dist/skeletor.min.js.map +1 -1
  6. package/package.json +23 -12
  7. package/src/collection.js +540 -395
  8. package/src/drivers/sessionStorage.js +226 -232
  9. package/src/element.js +132 -82
  10. package/src/eventemitter.js +187 -0
  11. package/src/helpers.js +148 -114
  12. package/src/{main.js → index.js} +21 -17
  13. package/src/listening.js +81 -0
  14. package/src/model.js +340 -240
  15. package/src/storage.js +223 -241
  16. package/src/types/collection.d.ts +366 -0
  17. package/src/types/collection.d.ts.map +1 -0
  18. package/src/types/drivers/sessionStorage.d.ts +37 -0
  19. package/src/types/drivers/sessionStorage.d.ts.map +1 -0
  20. package/src/types/element.d.ts +115 -0
  21. package/src/types/element.d.ts.map +1 -0
  22. package/src/types/eventemitter.d.ts +93 -0
  23. package/src/types/eventemitter.d.ts.map +1 -0
  24. package/src/types/helpers.d.ts +62 -0
  25. package/src/types/helpers.d.ts.map +1 -0
  26. package/src/types/index.d.ts +19 -0
  27. package/src/types/index.d.ts.map +1 -0
  28. package/src/types/listening.d.ts +40 -0
  29. package/src/types/listening.d.ts.map +1 -0
  30. package/src/types/model.d.ts +230 -0
  31. package/src/types/model.d.ts.map +1 -0
  32. package/src/types/storage.d.ts +35 -0
  33. package/src/types/storage.d.ts.map +1 -0
  34. package/src/types/utils/events.d.ts +28 -0
  35. package/src/types/utils/events.d.ts.map +1 -0
  36. package/src/utils/events.js +174 -0
  37. package/src/drivers/indexeddb-getitems.js~ +0 -1116
  38. package/src/drivers/indexeddb.js~ +0 -1115
  39. package/src/events.js +0 -328
  40. package/src/history.js +0 -301
  41. package/src/overview.js +0 -191
  42. package/src/router.js +0 -119
  43. package/src/storage.js~ +0 -269
  44. package/src/view.js +0 -267
package/src/storage.js CHANGED
@@ -4,263 +4,245 @@
4
4
  import * as memoryDriver from 'localforage-driver-memory';
5
5
  import cloneDeep from 'lodash-es/cloneDeep.js';
6
6
  import isString from 'lodash-es/isString.js';
7
- import localForage from "localforage/src/localforage";
7
+ import localForage from 'localforage';
8
8
  import mergebounce from 'mergebounce';
9
- import sessionStorageWrapper from "./drivers/sessionStorage.js";
9
+ import sessionStorageWrapper from './drivers/sessionStorage.js';
10
10
  import { extendPrototype as extendPrototypeWithSetItems } from 'localforage-setitems';
11
- import { extendPrototype as extendPrototypeWithGetItems } from 'localforage-getitems';
11
+ import { extendPrototype as extendPrototypeWithGetItems } from '@converse/localforage-getitems/dist/localforage-getitems.es6';
12
+ import { guid } from './helpers.js';
12
13
 
13
- const IN_MEMORY = memoryDriver._driver
14
+ const IN_MEMORY = memoryDriver._driver;
14
15
  localForage.defineDriver(memoryDriver);
15
16
  extendPrototypeWithSetItems(localForage);
16
17
  extendPrototypeWithGetItems(localForage);
17
18
 
18
- function S4() {
19
- // Generate four random hex digits.
20
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
21
- }
22
-
23
- function guid() {
24
- // Generate a pseudo-GUID by concatenating random hexadecimal.
25
- return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
26
- }
27
-
28
-
29
19
  class Storage {
30
-
31
- constructor (id, type, batchedWrites=false) {
32
- if (type === 'local' && !window.localStorage ) {
33
- throw new Error("Skeletor.storage: Environment does not support localStorage.");
34
- } else if (type === 'session' && !window.sessionStorage ) {
35
- throw new Error("Skeletor.storage: Environment does not support sessionStorage.");
36
- }
37
- if (isString(type)) {
38
- this.storeInitialized = this.initStore(type, batchedWrites);
39
- } else {
40
- this.store = type;
41
- if (batchedWrites) {
42
- this.store.debouncedSetItems = mergebounce(
43
- items => this.store.setItems(items),
44
- 50,
45
- {'promise': true}
46
- );
47
- }
48
- this.storeInitialized = Promise.resolve();
49
- }
50
- this.name = id;
51
- }
52
-
53
- async initStore (type, batchedWrites) {
54
- if (type === 'session') {
55
- localForage.setDriver(sessionStorageWrapper._driver);
56
- } else if (type === 'local') {
57
- await localForage.config({'driver': localForage.LOCALSTORAGE});
58
- } else if (type === 'in_memory') {
59
- localForage.config({'driver': IN_MEMORY});
60
- } else if (type !== 'indexed') {
61
- throw new Error("Skeletor.storage: No storage type was specified");
62
- }
63
- this.store = localForage;
64
- if (batchedWrites) {
65
- this.store.debouncedSetItems = mergebounce(
66
- items => this.store.setItems(items),
67
- 50,
68
- {'promise': true}
69
- );
70
- }
71
- }
72
-
73
- flush () {
74
- return this.store.debouncedSetItems?.flush();
75
- }
76
-
77
- async clear () {
78
- await this.store.removeItem(this.name).catch(e => console.error(e));
79
- const re = new RegExp(`^${this.name}-`);
80
- const keys = await this.store.keys();
81
- const removed_keys = keys.filter(k => re.test(k));
82
- await Promise.all(removed_keys.map(k => this.store.removeItem(k).catch(e => console.error(e))));
83
- }
84
-
85
- sync () {
86
- const that = this;
87
-
88
- async function localSync (method, model, options) {
89
- let resp, errorMessage, promise, new_attributes;
90
-
91
- // We get the collection (and if necessary the model attribute.
92
- // Waiting for storeInitialized will cause another iteration of
93
- // the event loop, after which the collection reference will
94
- // be removed from the model.
95
- const collection = model.collection;
96
- if (['patch', 'update'].includes(method)) {
97
- new_attributes = cloneDeep(model.attributes);
20
+ constructor(id, type, batchedWrites = false) {
21
+ if (type === 'local' && !window.localStorage) {
22
+ throw new Error('Skeletor.storage: Environment does not support localStorage.');
23
+ } else if (type === 'session' && !window.sessionStorage) {
24
+ throw new Error('Skeletor.storage: Environment does not support sessionStorage.');
25
+ }
26
+ if (isString(type)) {
27
+ this.storeInitialized = this.initStore(type, batchedWrites);
28
+ } else {
29
+ this.store = type;
30
+ if (batchedWrites) {
31
+ this.store.debouncedSetItems = mergebounce((items) => this.store.setItems(items), 50, { 'promise': true });
32
+ }
33
+ this.storeInitialized = Promise.resolve();
34
+ }
35
+ this.name = id;
36
+ }
37
+
38
+ /**
39
+ * @param {'local'|'session'|'indexed'|'in_memory'} type
40
+ * @param {boolean} batchedWrites
41
+ */
42
+ async initStore(type, batchedWrites) {
43
+ if (type === 'session') {
44
+ await localForage.setDriver(sessionStorageWrapper._driver);
45
+ } else if (type === 'local') {
46
+ await localForage.config({ 'driver': localForage.LOCALSTORAGE });
47
+ } else if (type === 'in_memory') {
48
+ await localForage.config({ 'driver': IN_MEMORY });
49
+ } else if (type !== 'indexed') {
50
+ throw new Error('Skeletor.storage: No storage type was specified');
51
+ }
52
+ this.store = localForage;
53
+ if (batchedWrites) {
54
+ this.store.debouncedSetItems = mergebounce((items) => this.store.setItems(items), 50, { 'promise': true });
55
+ }
56
+ }
57
+
58
+ flush() {
59
+ return this.store.debouncedSetItems?.flush();
60
+ }
61
+
62
+ async clear() {
63
+ await this.store.removeItem(this.name).catch((e) => console.error(e));
64
+ const re = new RegExp(`^${this.name}-`);
65
+ const keys = await this.store.keys();
66
+ const removed_keys = keys.filter((k) => re.test(k));
67
+ await Promise.all(removed_keys.map((k) => this.store.removeItem(k).catch((e) => console.error(e))));
68
+ }
69
+
70
+ sync() {
71
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
72
+ const that = this;
73
+
74
+ async function localSync(method, model, options) {
75
+ let resp, errorMessage, promise, new_attributes;
76
+
77
+ // We get the collection (and if necessary the model attribute.
78
+ // Waiting for storeInitialized will cause another iteration of
79
+ // the event loop, after which the collection reference will
80
+ // be removed from the model.
81
+ const collection = model.collection;
82
+ if (['patch', 'update'].includes(method)) {
83
+ new_attributes = cloneDeep(model.attributes);
84
+ }
85
+ await that.storeInitialized;
86
+ try {
87
+ const original_attributes = model.attributes;
88
+ switch (method) {
89
+ case 'read':
90
+ if (model.id !== undefined) {
91
+ resp = await that.find(model);
92
+ } else {
93
+ resp = await that.findAll();
98
94
  }
99
- await that.storeInitialized;
100
- try {
101
- const original_attributes = model.attributes;
102
- switch (method) {
103
- case "read":
104
- if (model.id !== undefined) {
105
- resp = await that.find(model);
106
- } else {
107
- resp = await that.findAll();
108
- }
109
- break;
110
- case "create":
111
- resp = await that.create(model, options);
112
- break;
113
- case 'patch':
114
- case "update":
115
- if (options.wait) {
116
- // When `wait` is set to true, Skeletor waits until
117
- // confirmation of storage before setting the values on
118
- // the model.
119
- // However, the new attributes needs to be sent, so it
120
- // sets them manually on the model and then removes
121
- // them after calling `sync`.
122
- // Because our `sync` method is asynchronous and we
123
- // wait for `storeInitialized`, the attributes are
124
- // already restored once we get here, so we need to do
125
- // the attributes dance again.
126
- model.attributes = new_attributes;
127
- }
128
- promise = that.update(model, options);
129
- if (options.wait) {
130
- model.attributes = original_attributes;
131
- }
132
- resp = await promise;
133
- break;
134
- case "delete":
135
- resp = await that.destroy(model, collection);
136
- break;
137
- }
138
- } catch (error) {
139
- if (error.code === 22 && that.getStorageSize() === 0) {
140
- errorMessage = "Private browsing is unsupported";
141
- } else {
142
- errorMessage = error.message;
143
- }
95
+ break;
96
+ case 'create':
97
+ resp = await that.create(model, options);
98
+ break;
99
+ case 'patch':
100
+ case 'update':
101
+ if (options.wait) {
102
+ // When `wait` is set to true, Skeletor waits until
103
+ // confirmation of storage before setting the values on
104
+ // the model.
105
+ // However, the new attributes needs to be sent, so it
106
+ // sets them manually on the model and then removes
107
+ // them after calling `sync`.
108
+ // Because our `sync` method is asynchronous and we
109
+ // wait for `storeInitialized`, the attributes are
110
+ // already restored once we get here, so we need to do
111
+ // the attributes dance again.
112
+ model.attributes = new_attributes;
144
113
  }
145
-
146
- if (resp) {
147
- if (options && options.success) {
148
- // When storing, we don't pass back the response (which is
149
- // the set attributes returned from localforage because
150
- // Skeletor sets them again on the model and due to the async
151
- // nature of localforage it can cause stale attributes to be
152
- // set on a model after it's been updated in the meantime.
153
- const data = (method === "read") ? resp : null;
154
- options.success(data, options);
155
- }
156
- } else {
157
- errorMessage = errorMessage ? errorMessage : "Record Not Found";
158
- if (options && options.error) {
159
- options.error(errorMessage);
160
- }
114
+ promise = that.update(model);
115
+ if (options.wait) {
116
+ model.attributes = original_attributes;
161
117
  }
118
+ resp = await promise;
119
+ break;
120
+ case 'delete':
121
+ resp = await that.destroy(model, collection);
122
+ break;
162
123
  }
163
- localSync.__name__ = 'localSync';
164
- return localSync;
165
- }
166
-
167
- removeCollectionReference (model, collection) {
168
- if (!collection) {
169
- return;
170
- }
171
- const ids = collection
172
- .filter(m => (m.id !== model.id))
173
- .map(m => this.getItemName(m.id));
174
-
175
- return this.store.setItem(this.name, ids);
176
- }
177
-
178
- addCollectionReference (model, collection) {
179
- if (!collection) {
180
- return;
181
- }
182
- const ids = collection.map(m => this.getItemName(m.id));
183
- const new_id = this.getItemName(model.id);
184
- if (!ids.includes(new_id)) {
185
- ids.push(new_id);
186
- }
187
- return this.store.setItem(this.name, ids);
188
- }
189
-
190
- getCollectionReferenceData (model) {
191
- if (!model.collection) {
192
- return {};
193
- }
194
- const ids = model.collection.map(m => this.getItemName(m.id));
195
- const new_id = this.getItemName(model.id);
196
- if (!ids.includes(new_id)) {
197
- ids.push(new_id);
198
- }
199
- const result = {};
200
- result[this.name] = ids;
201
- return result;
202
- }
203
-
204
- async save (model) {
205
- if (this.store.setItems) {
206
- const items = {}
207
- items[this.getItemName(model.id)] = model.toJSON();
208
- Object.assign(items, this.getCollectionReferenceData(model));
209
- return (this.store.debouncedSetItems) ?
210
- this.store.debouncedSetItems(items) :
211
- this.store.setItems(items);
124
+ } catch (error) {
125
+ if (error.code === 22 && that.getStorageSize() === 0) {
126
+ errorMessage = 'Private browsing is unsupported';
212
127
  } else {
213
- const key = this.getItemName(model.id);
214
- const data = await this.store.setItem(key, model.toJSON());
215
- await this.addCollectionReference(model, model.collection);
216
- return data;
128
+ errorMessage = error.message;
217
129
  }
218
- }
219
-
220
- create (model, options) {
221
- /* Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
222
- * have an id of it's own.
223
- */
224
- if (!model.id) {
225
- model.id = guid();
226
- model.set(model.idAttribute, model.id, options);
130
+ }
131
+
132
+ if (resp) {
133
+ if (options && options.success) {
134
+ // When storing, we don't pass back the response (which is
135
+ // the set attributes returned from localforage because
136
+ // Skeletor sets them again on the model and due to the async
137
+ // nature of localforage it can cause stale attributes to be
138
+ // set on a model after it's been updated in the meantime.
139
+ const data = method === 'read' ? resp : null;
140
+ options.success(data, options);
227
141
  }
228
- return this.save(model);
229
- }
230
-
231
- update (model) {
232
- return this.save(model);
233
- }
234
-
235
- find (model) {
236
- return this.store.getItem(this.getItemName(model.id));
237
- }
238
-
239
- async findAll () {
240
- /* Return the array of all models currently in storage.
241
- */
242
- const keys = await this.store.getItem(this.name);
243
- if (keys?.length) {
244
- const items = await this.store.getItems(keys);
245
- return Object.values(items);
142
+ } else {
143
+ errorMessage = errorMessage ? errorMessage : 'Record Not Found';
144
+ if (options && options.error) {
145
+ options.error(errorMessage);
246
146
  }
247
- return [];
248
- }
249
-
250
- async destroy (model, collection) {
251
- await this.flush();
252
- await this.store.removeItem(this.getItemName(model.id));
253
- await this.removeCollectionReference(model, collection);
254
- return model;
255
- }
256
-
257
- getStorageSize () {
258
- return this.store.length;
259
- }
260
-
261
- getItemName (id) {
262
- return this.name+"-"+id;
263
- }
147
+ }
148
+ }
149
+ localSync.__name__ = 'localSync';
150
+ return localSync;
151
+ }
152
+
153
+ removeCollectionReference(model, collection) {
154
+ if (!collection) {
155
+ return;
156
+ }
157
+ const ids = collection.filter((m) => m.id !== model.id).map((m) => this.getItemName(m.id));
158
+
159
+ return this.store.setItem(this.name, ids);
160
+ }
161
+
162
+ addCollectionReference(model, collection) {
163
+ if (!collection) {
164
+ return;
165
+ }
166
+ const ids = collection.map((m) => this.getItemName(m.id));
167
+ const new_id = this.getItemName(model.id);
168
+ if (!ids.includes(new_id)) {
169
+ ids.push(new_id);
170
+ }
171
+ return this.store.setItem(this.name, ids);
172
+ }
173
+
174
+ getCollectionReferenceData(model) {
175
+ if (!model.collection) {
176
+ return {};
177
+ }
178
+ const ids = model.collection.map((m) => this.getItemName(m.id));
179
+ const new_id = this.getItemName(model.id);
180
+ if (!ids.includes(new_id)) {
181
+ ids.push(new_id);
182
+ }
183
+ const result = {};
184
+ result[this.name] = ids;
185
+ return result;
186
+ }
187
+
188
+ async save(model) {
189
+ if (this.store.setItems) {
190
+ const items = {};
191
+ items[this.getItemName(model.id)] = model.toJSON();
192
+ Object.assign(items, this.getCollectionReferenceData(model));
193
+ return this.store.debouncedSetItems ? this.store.debouncedSetItems(items) : this.store.setItems(items);
194
+ } else {
195
+ const key = this.getItemName(model.id);
196
+ const data = await this.store.setItem(key, model.toJSON());
197
+ await this.addCollectionReference(model, model.collection);
198
+ return data;
199
+ }
200
+ }
201
+
202
+ create(model, options) {
203
+ /* Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
204
+ * have an id of it's own.
205
+ */
206
+ if (!model.id) {
207
+ model.id = guid();
208
+ model.set(model.idAttribute, model.id, options);
209
+ }
210
+ return this.save(model);
211
+ }
212
+
213
+ update(model) {
214
+ return this.save(model);
215
+ }
216
+
217
+ find(model) {
218
+ return this.store.getItem(this.getItemName(model.id));
219
+ }
220
+
221
+ async findAll() {
222
+ /* Return the array of all models currently in storage.
223
+ */
224
+ const keys = await this.store.getItem(this.name);
225
+ if (keys?.length) {
226
+ const items = await this.store.getItems(keys);
227
+ return Object.values(items);
228
+ }
229
+ return [];
230
+ }
231
+
232
+ async destroy(model, collection) {
233
+ await this.flush();
234
+ await this.store.removeItem(this.getItemName(model.id));
235
+ await this.removeCollectionReference(model, collection);
236
+ return model;
237
+ }
238
+
239
+ getStorageSize() {
240
+ return this.store.length;
241
+ }
242
+
243
+ getItemName(id) {
244
+ return this.name + '-' + id;
245
+ }
264
246
  }
265
247
 
266
248
  Storage.sessionStorageInitialized = localForage.defineDriver(sessionStorageWrapper);