@converse/skeletor 0.0.8 → 1.0.0

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.
@@ -1,306 +0,0 @@
1
- // Copyright 2014 Mozilla
2
- // Copyright 2015 Thodoris Greasidis
3
- // Copyright 2018 JC Brand
4
- //
5
- // Licensed under the Apache License, Version 2.0 (the "License");
6
- // you may not use this file except in compliance with the License.
7
- // You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing, software
12
- // distributed under the License is distributed on an "AS IS" BASIS,
13
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- // See the License for the specific language governing permissions and
15
- // limitations under the License.
16
-
17
- import executeCallback from 'localforage/src/utils/executeCallback';
18
- import getCallback from 'localforage/src/utils/getCallback';
19
- import normalizeKey from 'localforage/src/utils/normalizeKey';
20
- import serializer from 'localforage/src/utils/serializer';
21
-
22
- const serialize = serializer["serialize"];
23
- const deserialize = serializer["deserialize"];
24
-
25
-
26
- function isSessionStorageValid () {
27
- // If the app is running inside a Google Chrome packaged webapp, or some
28
- // other context where sessionStorage isn't available, we don't use
29
- // sessionStorage. This feature detection is preferred over the old
30
- // `if (window.chrome && window.chrome.runtime)` code.
31
- // See: https://github.com/mozilla/localForage/issues/68
32
- try {
33
- // If sessionStorage isn't available, we get outta here!
34
- // This should be inside a try catch
35
- if (sessionStorage && ('setItem' in sessionStorage)) {
36
- return true;
37
- }
38
- } catch (e) {
39
- console.log(e);
40
- }
41
- return false;
42
- }
43
-
44
- function _getKeyPrefix(options, defaultConfig) {
45
- let keyPrefix = options.name + '/';
46
-
47
- if (options.storeName !== defaultConfig.storeName) {
48
- keyPrefix += options.storeName + '/';
49
- }
50
- return keyPrefix;
51
- }
52
-
53
- const dbInfo = {
54
- 'serializer': {
55
- 'serialize': serialize,
56
- 'deserialize': deserialize
57
- }
58
- };
59
-
60
- function _initStorage(options) {
61
- dbInfo.keyPrefix = _getKeyPrefix(options, this._defaultConfig);
62
- if (options) {
63
- for (const i in options) { // eslint-disable-line guard-for-in
64
- dbInfo[i] = options[i];
65
- }
66
- }
67
- }
68
-
69
- // Remove all keys from the datastore, effectively destroying all data in
70
- // the app's key/value store!
71
- function clear(callback) {
72
- const promise = this.ready().then(function() {
73
- const keyPrefix = dbInfo.keyPrefix;
74
-
75
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
76
- const key = sessionStorage.key(i);
77
-
78
- if (key.indexOf(keyPrefix) === 0) {
79
- sessionStorage.removeItem(key);
80
- }
81
- }
82
- });
83
-
84
- executeCallback(promise, callback);
85
- return promise;
86
- }
87
-
88
- // Retrieve an item from the store. Unlike the original async_storage
89
- // library in Gaia, we don't modify return values at all. If a key's value
90
- // is `undefined`, we pass that value to the callback function.
91
- function getItem(key, callback) {
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
- }
108
-
109
- // Iterate over all items in the store.
110
- function iterate(iterator, callback) {
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(
142
- value,
143
- key.substring(keyPrefixLength),
144
- iterationNumber++
145
- );
146
-
147
- if (value !== void 0) { // eslint-disable-line no-void
148
- return value;
149
- }
150
- }
151
- });
152
-
153
- executeCallback(promise, callback);
154
- return promise;
155
- }
156
-
157
- // Same as sessionStorage's key() method, except takes a callback.
158
- function key(n, callback) {
159
- const self = this;
160
- const promise = self.ready().then(function() {
161
- let result;
162
- try {
163
- result = sessionStorage.key(n);
164
- } catch (error) {
165
- result = null;
166
- }
167
-
168
- // Remove the prefix from the key, if a key is found.
169
- if (result) {
170
- result = result.substring(dbInfo.keyPrefix.length);
171
- }
172
-
173
- return result;
174
- });
175
-
176
- executeCallback(promise, callback);
177
- return promise;
178
- }
179
-
180
- function keys(callback) {
181
- const self = this;
182
- const promise = self.ready().then(function() {
183
- const length = sessionStorage.length;
184
- const keys = [];
185
-
186
- for (let i = 0; i < length; i++) {
187
- const itemKey = sessionStorage.key(i);
188
- if (itemKey.indexOf(dbInfo.keyPrefix) === 0) {
189
- keys.push(itemKey.substring(dbInfo.keyPrefix.length));
190
- }
191
- }
192
- return keys;
193
- });
194
-
195
- executeCallback(promise, callback);
196
- return promise;
197
- }
198
-
199
- // Supply the number of keys in the datastore to the callback function.
200
- function length(callback) {
201
- const self = this;
202
- const promise = self.keys().then(function(keys) {
203
- return keys.length;
204
- });
205
-
206
- executeCallback(promise, callback);
207
- return promise;
208
- }
209
-
210
- // Remove an item from the store, nice and simple.
211
- function removeItem(key, callback) {
212
- key = normalizeKey(key);
213
- const promise = this.ready().then(function() {
214
- sessionStorage.removeItem(dbInfo.keyPrefix + key);
215
- });
216
- executeCallback(promise, callback);
217
- return promise;
218
- }
219
-
220
- // Set a key's value and run an optional callback once the value is set.
221
- // Unlike Gaia's implementation, the callback function is passed the value,
222
- // in case you want to operate on that value only after you're sure it
223
- // saved, or something like that.
224
- async function setItem(key, value, callback) {
225
- key = normalizeKey(key);
226
- await this.ready();
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
- }
255
-
256
- function dropInstance(options, callback) {
257
- callback = getCallback.apply(this, arguments);
258
-
259
- options = (typeof options !== 'function' && options) || {};
260
- if (!options.name) {
261
- const currentConfig = this.config();
262
- options.name = options.name || currentConfig.name;
263
- options.storeName = options.storeName || currentConfig.storeName;
264
- }
265
-
266
- const self = this;
267
- let promise;
268
- if (!options.name) {
269
- promise = Promise.reject(new Error('Invalid arguments'));
270
- } else {
271
- promise = new Promise(function(resolve) {
272
- if (!options.storeName) {
273
- resolve(`${options.name}/`);
274
- } else {
275
- resolve(_getKeyPrefix(options, self._defaultConfig));
276
- }
277
- }).then(function(keyPrefix) {
278
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
279
- const key = sessionStorage.key(i);
280
- if (key.indexOf(keyPrefix) === 0) {
281
- sessionStorage.removeItem(key);
282
- }
283
- }
284
- });
285
- }
286
-
287
- executeCallback(promise, callback);
288
- return promise;
289
- }
290
-
291
- const sessionStorageWrapper = {
292
- _driver: 'sessionStorageWrapper',
293
- _initStorage: _initStorage,
294
- _support: isSessionStorageValid(),
295
- iterate: iterate,
296
- getItem: getItem,
297
- setItem: setItem,
298
- removeItem: removeItem,
299
- clear: clear,
300
- length: length,
301
- key: key,
302
- keys: keys,
303
- dropInstance: dropInstance
304
- };
305
-
306
- export default sessionStorageWrapper;
@@ -1,307 +0,0 @@
1
- // Copyright 2014 Mozilla
2
- // Copyright 2015 Thodoris Greasidis
3
- // Copyright 2018 JC Brand
4
- //
5
- // Licensed under the Apache License, Version 2.0 (the "License");
6
- // you may not use this file except in compliance with the License.
7
- // You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing, software
12
- // distributed under the License is distributed on an "AS IS" BASIS,
13
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- // See the License for the specific language governing permissions and
15
- // limitations under the License.
16
-
17
- import executeCallback from 'localforage/src/utils/executeCallback';
18
- import getCallback from 'localforage/src/utils/getCallback';
19
- import normalizeKey from 'localforage/src/utils/normalizeKey';
20
- import serializer from 'localforage/src/utils/serializer';
21
-
22
- const serialize = serializer["serialize"];
23
- const deserialize = serializer["deserialize"];
24
-
25
-
26
- function isSessionStorageValid () {
27
- // If the app is running inside a Google Chrome packaged webapp, or some
28
- // other context where sessionStorage isn't available, we don't use
29
- // sessionStorage. This feature detection is preferred over the old
30
- // `if (window.chrome && window.chrome.runtime)` code.
31
- // See: https://github.com/mozilla/localForage/issues/68
32
- try {
33
- // If sessionStorage isn't available, we get outta here!
34
- // This should be inside a try catch
35
- if (sessionStorage && ('setItem' in sessionStorage)) {
36
- return true;
37
- }
38
- } catch (e) {
39
- console.log(e);
40
- }
41
- return false;
42
- }
43
-
44
- function _getKeyPrefix(options, defaultConfig) {
45
- let keyPrefix = options.name + '/';
46
-
47
- if (options.storeName !== defaultConfig.storeName) {
48
- keyPrefix += options.storeName + '/';
49
- }
50
- return keyPrefix;
51
- }
52
-
53
- const dbInfo = {
54
- 'serializer': {
55
- 'serialize': serialize,
56
- 'deserialize': deserialize
57
- }
58
- };
59
-
60
- function _initStorage(options) {
61
- dbInfo.keyPrefix = _getKeyPrefix(options, this._defaultConfig);
62
- if (options) {
63
- for (const i in options) { // eslint-disable-line guard-for-in
64
- dbInfo[i] = options[i];
65
- }
66
- }
67
- }
68
-
69
- // Remove all keys from the datastore, effectively destroying all data in
70
- // the app's key/value store!
71
- function clear(callback) {
72
- const promise = this.ready().then(function() {
73
- const keyPrefix = dbInfo.keyPrefix;
74
-
75
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
76
- const key = sessionStorage.key(i);
77
-
78
- if (key.indexOf(keyPrefix) === 0) {
79
- sessionStorage.removeItem(key);
80
- }
81
- }
82
- });
83
-
84
- executeCallback(promise, callback);
85
- return promise;
86
- }
87
-
88
- // Retrieve an item from the store. Unlike the original async_storage
89
- // library in Gaia, we don't modify return values at all. If a key's value
90
- // is `undefined`, we pass that value to the callback function.
91
- function getItem(key, callback) {
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
- }
108
-
109
- // Iterate over all items in the store.
110
- function iterate(iterator, callback) {
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(
142
- value,
143
- key.substring(keyPrefixLength),
144
- iterationNumber++
145
- );
146
-
147
- if (value !== void 0) { // eslint-disable-line no-void
148
- return value;
149
- }
150
- }
151
- });
152
-
153
- executeCallback(promise, callback);
154
- return promise;
155
- }
156
-
157
- // Same as sessionStorage's key() method, except takes a callback.
158
- function key(n, callback) {
159
- const self = this;
160
- const promise = self.ready().then(function() {
161
- let result;
162
- try {
163
- result = sessionStorage.key(n);
164
- } catch (error) {
165
- result = null;
166
- }
167
-
168
- // Remove the prefix from the key, if a key is found.
169
- if (result) {
170
- result = result.substring(dbInfo.keyPrefix.length);
171
- }
172
-
173
- return result;
174
- });
175
-
176
- executeCallback(promise, callback);
177
- return promise;
178
- }
179
-
180
- function keys(callback) {
181
- const self = this;
182
- const promise = self.ready().then(function() {
183
- const length = sessionStorage.length;
184
- const keys = [];
185
-
186
- for (let i = 0; i < length; i++) {
187
- const itemKey = sessionStorage.key(i);
188
- if (itemKey.indexOf(dbInfo.keyPrefix) === 0) {
189
- keys.push(itemKey.substring(dbInfo.keyPrefix.length));
190
- }
191
- }
192
- return keys;
193
- });
194
-
195
- executeCallback(promise, callback);
196
- return promise;
197
- }
198
-
199
- // Supply the number of keys in the datastore to the callback function.
200
- function length(callback) {
201
- const self = this;
202
- const promise = self.keys().then(function(keys) {
203
- return keys.length;
204
- });
205
-
206
- executeCallback(promise, callback);
207
- return promise;
208
- }
209
-
210
- // Remove an item from the store, nice and simple.
211
- function removeItem(key, callback) {
212
- key = normalizeKey(key);
213
- const promise = this.ready().then(function() {
214
- sessionStorage.removeItem(dbInfo.keyPrefix + key);
215
- });
216
- executeCallback(promise, callback);
217
- return promise;
218
- }
219
-
220
- // Set a key's value and run an optional callback once the value is set.
221
- // Unlike Gaia's implementation, the callback function is passed the value,
222
- // in case you want to operate on that value only after you're sure it
223
- // saved, or something like that.
224
- async function setItem(key, value, callback) {
225
- key = normalizeKey(key);
226
- await this.ready();
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
-
255
- }
256
-
257
- function dropInstance(options, callback) {
258
- callback = getCallback.apply(this, arguments);
259
-
260
- options = (typeof options !== 'function' && options) || {};
261
- if (!options.name) {
262
- const currentConfig = this.config();
263
- options.name = options.name || currentConfig.name;
264
- options.storeName = options.storeName || currentConfig.storeName;
265
- }
266
-
267
- const self = this;
268
- let promise;
269
- if (!options.name) {
270
- promise = Promise.reject(new Error('Invalid arguments'));
271
- } else {
272
- promise = new Promise(function(resolve) {
273
- if (!options.storeName) {
274
- resolve(`${options.name}/`);
275
- } else {
276
- resolve(_getKeyPrefix(options, self._defaultConfig));
277
- }
278
- }).then(function(keyPrefix) {
279
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
280
- const key = sessionStorage.key(i);
281
- if (key.indexOf(keyPrefix) === 0) {
282
- sessionStorage.removeItem(key);
283
- }
284
- }
285
- });
286
- }
287
-
288
- executeCallback(promise, callback);
289
- return promise;
290
- }
291
-
292
- const sessionStorageWrapper = {
293
- _driver: 'sessionStorageWrapper',
294
- _initStorage: _initStorage,
295
- _support: isSessionStorageValid(),
296
- iterate: iterate,
297
- getItem: getItem,
298
- setItem: setItem,
299
- removeItem: removeItem,
300
- clear: clear,
301
- length: length,
302
- key: key,
303
- keys: keys,
304
- dropInstance: dropInstance
305
- };
306
-
307
- export default sessionStorageWrapper;