@converse/skeletor 0.0.8 → 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.
- package/README.md +38 -25
- package/dist/skeletor.js +9232 -5096
- package/dist/skeletor.js.map +1 -1
- package/dist/skeletor.min.js +74 -3
- package/dist/skeletor.min.js.map +1 -1
- package/package.json +21 -11
- package/src/collection.js +540 -395
- package/src/drivers/sessionStorage.js +222 -219
- package/src/element.js +132 -82
- package/src/eventemitter.js +187 -0
- package/src/helpers.js +143 -118
- package/src/{main.js → index.js} +21 -17
- package/src/listening.js +81 -0
- package/src/model.js +340 -238
- package/src/storage.js +222 -230
- package/src/types/collection.d.ts +366 -0
- package/src/types/collection.d.ts.map +1 -0
- package/src/types/drivers/sessionStorage.d.ts +37 -0
- package/src/types/drivers/sessionStorage.d.ts.map +1 -0
- package/src/types/element.d.ts +115 -0
- package/src/types/element.d.ts.map +1 -0
- package/src/types/eventemitter.d.ts +93 -0
- package/src/types/eventemitter.d.ts.map +1 -0
- package/src/types/helpers.d.ts +62 -0
- package/src/types/helpers.d.ts.map +1 -0
- package/src/types/index.d.ts +19 -0
- package/src/types/index.d.ts.map +1 -0
- package/src/types/listening.d.ts +40 -0
- package/src/types/listening.d.ts.map +1 -0
- package/src/types/model.d.ts +230 -0
- package/src/types/model.d.ts.map +1 -0
- package/src/types/storage.d.ts +35 -0
- package/src/types/storage.d.ts.map +1 -0
- package/src/types/utils/events.d.ts +28 -0
- package/src/types/utils/events.d.ts.map +1 -0
- package/src/utils/events.js +174 -0
- package/src/drivers/sessionStorage.js~ +0 -307
- package/src/events.js +0 -328
- package/src/history.js +0 -301
- package/src/overview.js +0 -191
- package/src/router.js +0 -119
- package/src/view.js +0 -267
|
@@ -19,288 +19,291 @@ import getCallback from 'localforage/src/utils/getCallback';
|
|
|
19
19
|
import normalizeKey from 'localforage/src/utils/normalizeKey';
|
|
20
20
|
import serializer from 'localforage/src/utils/serializer';
|
|
21
21
|
|
|
22
|
-
const serialize = serializer[
|
|
23
|
-
const deserialize = serializer[
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
} catch (e) {
|
|
39
|
-
console.log(e);
|
|
22
|
+
const serialize = serializer['serialize'];
|
|
23
|
+
const deserialize = serializer['deserialize'];
|
|
24
|
+
|
|
25
|
+
function isSessionStorageValid() {
|
|
26
|
+
// If the app is running inside a Google Chrome packaged webapp, or some
|
|
27
|
+
// other context where sessionStorage isn't available, we don't use
|
|
28
|
+
// sessionStorage. This feature detection is preferred over the old
|
|
29
|
+
// `if (window.chrome && window.chrome.runtime)` code.
|
|
30
|
+
// See: https://github.com/mozilla/localForage/issues/68
|
|
31
|
+
try {
|
|
32
|
+
// If sessionStorage isn't available, we get outta here!
|
|
33
|
+
// This should be inside a try catch
|
|
34
|
+
if (sessionStorage && 'setItem' in sessionStorage) {
|
|
35
|
+
return true;
|
|
40
36
|
}
|
|
41
|
-
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.log(e);
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
function _getKeyPrefix(options, defaultConfig) {
|
|
45
|
-
|
|
44
|
+
let keyPrefix = options.name + '/';
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
if (options.storeName !== defaultConfig.storeName) {
|
|
47
|
+
keyPrefix += options.storeName + '/';
|
|
48
|
+
}
|
|
49
|
+
return keyPrefix;
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
const dbInfo = {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
'serializer': {
|
|
54
|
+
'serialize': serialize,
|
|
55
|
+
'deserialize': deserialize,
|
|
56
|
+
},
|
|
58
57
|
};
|
|
59
58
|
|
|
60
59
|
function _initStorage(options) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
dbInfo.keyPrefix = _getKeyPrefix(options, this._defaultConfig);
|
|
61
|
+
if (options) {
|
|
62
|
+
for (const i in options) {
|
|
63
|
+
// eslint-disable-line guard-for-in
|
|
64
|
+
dbInfo[i] = options[i];
|
|
66
65
|
}
|
|
66
|
+
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// Remove all keys from the datastore, effectively destroying all data in
|
|
70
70
|
// the app's key/value store!
|
|
71
71
|
function clear(callback) {
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
const promise = this.ready().then(function () {
|
|
73
|
+
const keyPrefix = dbInfo.keyPrefix;
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
for (let i = sessionStorage.length - 1; i >= 0; i--) {
|
|
76
|
+
const key = sessionStorage.key(i);
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
if (key.indexOf(keyPrefix) === 0) {
|
|
79
|
+
sessionStorage.removeItem(key);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
executeCallback(promise, callback);
|
|
85
|
+
return promise;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
// Retrieve an item from the store. Unlike the original async_storage
|
|
89
89
|
// library in Gaia, we don't modify return values at all. If a key's value
|
|
90
90
|
// is `undefined`, we pass that value to the callback function.
|
|
91
91
|
function getItem(key, callback) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
92
|
+
key = normalizeKey(key);
|
|
93
|
+
|
|
94
|
+
const promise = this.ready().then(function () {
|
|
95
|
+
let result = sessionStorage.getItem(dbInfo.keyPrefix + key);
|
|
96
|
+
// If a result was found, parse it from the serialized
|
|
97
|
+
// string into a JS object. If result isn't truthy, the key
|
|
98
|
+
// is likely undefined and we'll pass it straight to the
|
|
99
|
+
// callback.
|
|
100
|
+
if (result) {
|
|
101
|
+
result = dbInfo.serializer.deserialize(result);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
});
|
|
105
|
+
executeCallback(promise, callback);
|
|
106
|
+
return promise;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
// Iterate over all items in the store.
|
|
110
110
|
function iterate(iterator, callback) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
});
|
|
111
|
+
const self = this;
|
|
112
|
+
|
|
113
|
+
const promise = self.ready().then(function () {
|
|
114
|
+
const keyPrefix = dbInfo.keyPrefix;
|
|
115
|
+
const keyPrefixLength = keyPrefix.length;
|
|
116
|
+
const length = sessionStorage.length;
|
|
117
|
+
|
|
118
|
+
// We use a dedicated iterator instead of the `i` variable below
|
|
119
|
+
// so other keys we fetch in sessionStorage aren't counted in
|
|
120
|
+
// the `iterationNumber` argument passed to the `iterate()`
|
|
121
|
+
// callback.
|
|
122
|
+
//
|
|
123
|
+
// See: github.com/mozilla/localForage/pull/435#discussion_r38061530
|
|
124
|
+
let iterationNumber = 1;
|
|
125
|
+
|
|
126
|
+
for (let i = 0; i < length; i++) {
|
|
127
|
+
const key = sessionStorage.key(i);
|
|
128
|
+
if (key.indexOf(keyPrefix) !== 0) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
let value = sessionStorage.getItem(key);
|
|
132
|
+
|
|
133
|
+
// If a result was found, parse it from the serialized
|
|
134
|
+
// string into a JS object. If result isn't truthy, the
|
|
135
|
+
// key is likely undefined and we'll pass it straight
|
|
136
|
+
// to the iterator.
|
|
137
|
+
if (value) {
|
|
138
|
+
value = dbInfo.serializer.deserialize(value);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
value = iterator(value, key.substring(keyPrefixLength), iterationNumber++);
|
|
142
|
+
|
|
143
|
+
if (value !== void 0) {
|
|
144
|
+
// eslint-disable-line no-void
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
152
149
|
|
|
153
|
-
|
|
154
|
-
|
|
150
|
+
executeCallback(promise, callback);
|
|
151
|
+
return promise;
|
|
155
152
|
}
|
|
156
153
|
|
|
157
154
|
// Same as sessionStorage's key() method, except takes a callback.
|
|
158
155
|
function key(n, callback) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
156
|
+
const self = this;
|
|
157
|
+
const promise = self.ready().then(function () {
|
|
158
|
+
let result;
|
|
159
|
+
try {
|
|
160
|
+
result = sessionStorage.key(n);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
result = null;
|
|
163
|
+
}
|
|
167
164
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
// Remove the prefix from the key, if a key is found.
|
|
166
|
+
if (result) {
|
|
167
|
+
result = result.substring(dbInfo.keyPrefix.length);
|
|
168
|
+
}
|
|
172
169
|
|
|
173
|
-
|
|
174
|
-
|
|
170
|
+
return result;
|
|
171
|
+
});
|
|
175
172
|
|
|
176
|
-
|
|
177
|
-
|
|
173
|
+
executeCallback(promise, callback);
|
|
174
|
+
return promise;
|
|
178
175
|
}
|
|
179
176
|
|
|
180
177
|
function keys(callback) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
178
|
+
const self = this;
|
|
179
|
+
const promise = self.ready().then(function () {
|
|
180
|
+
const length = sessionStorage.length;
|
|
181
|
+
const keys = [];
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < length; i++) {
|
|
184
|
+
const itemKey = sessionStorage.key(i);
|
|
185
|
+
if (itemKey.indexOf(dbInfo.keyPrefix) === 0) {
|
|
186
|
+
keys.push(itemKey.substring(dbInfo.keyPrefix.length));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return keys;
|
|
190
|
+
});
|
|
194
191
|
|
|
195
|
-
|
|
196
|
-
|
|
192
|
+
executeCallback(promise, callback);
|
|
193
|
+
return promise;
|
|
197
194
|
}
|
|
198
195
|
|
|
199
196
|
// Supply the number of keys in the datastore to the callback function.
|
|
200
197
|
function length(callback) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
const self = this;
|
|
199
|
+
const promise = self.keys().then(function (keys) {
|
|
200
|
+
return keys.length;
|
|
201
|
+
});
|
|
205
202
|
|
|
206
|
-
|
|
207
|
-
|
|
203
|
+
executeCallback(promise, callback);
|
|
204
|
+
return promise;
|
|
208
205
|
}
|
|
209
206
|
|
|
210
207
|
// Remove an item from the store, nice and simple.
|
|
211
208
|
function removeItem(key, callback) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
209
|
+
key = normalizeKey(key);
|
|
210
|
+
const promise = this.ready().then(function () {
|
|
211
|
+
sessionStorage.removeItem(dbInfo.keyPrefix + key);
|
|
212
|
+
});
|
|
213
|
+
executeCallback(promise, callback);
|
|
214
|
+
return promise;
|
|
218
215
|
}
|
|
219
216
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Set a key's value and run an optional callback once the value is set.
|
|
219
|
+
* Unlike Gaia's implementation, the callback function is passed the value,
|
|
220
|
+
* in case you want to operate on that value only after you're sure it
|
|
221
|
+
* saved, or something like that.
|
|
222
|
+
* @template T
|
|
223
|
+
* @param {string} key - The key under which the value is stored.
|
|
224
|
+
* @param {T} value - The value to be stored, can be of any type.
|
|
225
|
+
* @param {(err: any, value: T) => void} [callback] - Optional callback function to be called after the value is set.
|
|
226
|
+
* @returns {Promise<T>}
|
|
227
|
+
*/
|
|
224
228
|
async function setItem(key, value, callback) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// Convert undefined values to null.
|
|
229
|
-
// https://github.com/mozilla/localForage/pull/42
|
|
230
|
-
value = value ?? null;
|
|
231
|
-
|
|
232
|
-
// Save the original value to pass to the callback.
|
|
233
|
-
const originalValue = value;
|
|
234
|
-
|
|
235
|
-
dbInfo.serializer.serialize(value, (value, error) => {
|
|
236
|
-
if (error) {
|
|
237
|
-
throw error;
|
|
238
|
-
} else {
|
|
239
|
-
try {
|
|
240
|
-
sessionStorage.setItem(dbInfo.keyPrefix + key, value);
|
|
241
|
-
executeCallback(Promise.resolve(originalValue), callback);
|
|
242
|
-
} catch (e) {
|
|
243
|
-
if (
|
|
244
|
-
e.name === 'QuotaExceededError' ||
|
|
245
|
-
e.name === 'NS_ERROR_DOM_QUOTA_REACHED'
|
|
246
|
-
) {
|
|
247
|
-
console.error("Your sesionStorage capacity is used up.");
|
|
248
|
-
throw e;
|
|
249
|
-
}
|
|
250
|
-
throw e;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
229
|
+
key = normalizeKey(key);
|
|
230
|
+
await this.ready();
|
|
255
231
|
|
|
256
|
-
|
|
257
|
-
|
|
232
|
+
// Convert undefined values to null.
|
|
233
|
+
// https://github.com/mozilla/localForage/pull/42
|
|
234
|
+
value = value ?? null;
|
|
258
235
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const currentConfig = this.config();
|
|
262
|
-
options.name = options.name || currentConfig.name;
|
|
263
|
-
options.storeName = options.storeName || currentConfig.storeName;
|
|
264
|
-
}
|
|
236
|
+
// Save the original value to pass to the callback.
|
|
237
|
+
const originalValue = value;
|
|
265
238
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
promise = Promise.reject(new Error('Invalid arguments'));
|
|
239
|
+
dbInfo.serializer.serialize(value, (value, error) => {
|
|
240
|
+
if (error) {
|
|
241
|
+
throw error;
|
|
270
242
|
} else {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
sessionStorage.removeItem(key);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
});
|
|
243
|
+
try {
|
|
244
|
+
sessionStorage.setItem(dbInfo.keyPrefix + key, value);
|
|
245
|
+
executeCallback(Promise.resolve(originalValue), callback);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
|
|
248
|
+
console.error('Your sesionStorage capacity is used up.');
|
|
249
|
+
throw e;
|
|
250
|
+
}
|
|
251
|
+
throw e;
|
|
252
|
+
}
|
|
285
253
|
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
return value;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function dropInstance(options, callback) {
|
|
260
|
+
callback = getCallback.apply(this, arguments);
|
|
261
|
+
|
|
262
|
+
options = (typeof options !== 'function' && options) || {};
|
|
263
|
+
if (!options.name) {
|
|
264
|
+
const currentConfig = this.config();
|
|
265
|
+
options.name = options.name || currentConfig.name;
|
|
266
|
+
options.storeName = options.storeName || currentConfig.storeName;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const self = this;
|
|
270
|
+
let promise;
|
|
271
|
+
if (!options.name) {
|
|
272
|
+
promise = Promise.reject(new Error('Invalid arguments'));
|
|
273
|
+
} else {
|
|
274
|
+
promise = new Promise(function (resolve) {
|
|
275
|
+
if (!options.storeName) {
|
|
276
|
+
resolve(`${options.name}/`);
|
|
277
|
+
} else {
|
|
278
|
+
resolve(_getKeyPrefix(options, self._defaultConfig));
|
|
279
|
+
}
|
|
280
|
+
}).then(function (keyPrefix) {
|
|
281
|
+
for (let i = sessionStorage.length - 1; i >= 0; i--) {
|
|
282
|
+
const key = sessionStorage.key(i);
|
|
283
|
+
if (key.indexOf(keyPrefix) === 0) {
|
|
284
|
+
sessionStorage.removeItem(key);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
286
289
|
|
|
287
|
-
|
|
288
|
-
|
|
290
|
+
executeCallback(promise, callback);
|
|
291
|
+
return promise;
|
|
289
292
|
}
|
|
290
293
|
|
|
291
294
|
const sessionStorageWrapper = {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
295
|
+
_driver: 'sessionStorageWrapper',
|
|
296
|
+
_initStorage: _initStorage,
|
|
297
|
+
_support: isSessionStorageValid(),
|
|
298
|
+
iterate,
|
|
299
|
+
getItem,
|
|
300
|
+
setItem,
|
|
301
|
+
removeItem,
|
|
302
|
+
clear,
|
|
303
|
+
length,
|
|
304
|
+
key,
|
|
305
|
+
keys,
|
|
306
|
+
dropInstance,
|
|
304
307
|
};
|
|
305
308
|
|
|
306
309
|
export default sessionStorageWrapper;
|