react-wire-persisted 1.2.0 → 1.3.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.
- package/dist/react-wire-persisted.mjs +232 -0
- package/dist/react-wire-persisted.umd.js +1 -0
- package/package.json +36 -44
- package/dist/react-wire-persisted.js +0 -717
- package/dist/react-wire-persisted.min.js +0 -1
- package/es/react-wire-persisted.js +0 -702
- package/es/react-wire-persisted.mjs +0 -1
- package/lib/react-wire-persisted.js +0 -802
- /package/src/{index.ts → index.js} +0 -0
- /package/src/{react-wire-persisted.ts → react-wire-persisted.js} +0 -0
- /package/src/utils/{fakeLocalStorage.ts → fakeLocalStorage.js} +0 -0
- /package/src/utils/{index.ts → index.js} +0 -0
- /package/src/utils/{keys.ts → keys.js} +0 -0
|
@@ -1,702 +0,0 @@
|
|
|
1
|
-
import { createWire } from '@forminator/react-wire';
|
|
2
|
-
|
|
3
|
-
function ownKeys(object, enumerableOnly) {
|
|
4
|
-
var keys = Object.keys(object);
|
|
5
|
-
|
|
6
|
-
if (Object.getOwnPropertySymbols) {
|
|
7
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
8
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
9
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
10
|
-
})), keys.push.apply(keys, symbols);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return keys;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function _objectSpread2(target) {
|
|
17
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
18
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
|
19
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
20
|
-
_defineProperty(target, key, source[key]);
|
|
21
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
22
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return target;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function _classCallCheck(instance, Constructor) {
|
|
30
|
-
if (!(instance instanceof Constructor)) {
|
|
31
|
-
throw new TypeError("Cannot call a class as a function");
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function _defineProperties(target, props) {
|
|
36
|
-
for (var i = 0; i < props.length; i++) {
|
|
37
|
-
var descriptor = props[i];
|
|
38
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
39
|
-
descriptor.configurable = true;
|
|
40
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
41
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
46
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
47
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
48
|
-
Object.defineProperty(Constructor, "prototype", {
|
|
49
|
-
writable: false
|
|
50
|
-
});
|
|
51
|
-
return Constructor;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function _defineProperty(obj, key, value) {
|
|
55
|
-
if (key in obj) {
|
|
56
|
-
Object.defineProperty(obj, key, {
|
|
57
|
-
value: value,
|
|
58
|
-
enumerable: true,
|
|
59
|
-
configurable: true,
|
|
60
|
-
writable: true
|
|
61
|
-
});
|
|
62
|
-
} else {
|
|
63
|
-
obj[key] = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return obj;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function _inherits(subClass, superClass) {
|
|
70
|
-
if (typeof superClass !== "function" && superClass !== null) {
|
|
71
|
-
throw new TypeError("Super expression must either be null or a function");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
75
|
-
constructor: {
|
|
76
|
-
value: subClass,
|
|
77
|
-
writable: true,
|
|
78
|
-
configurable: true
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
Object.defineProperty(subClass, "prototype", {
|
|
82
|
-
writable: false
|
|
83
|
-
});
|
|
84
|
-
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function _getPrototypeOf(o) {
|
|
88
|
-
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
89
|
-
return o.__proto__ || Object.getPrototypeOf(o);
|
|
90
|
-
};
|
|
91
|
-
return _getPrototypeOf(o);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function _setPrototypeOf(o, p) {
|
|
95
|
-
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
96
|
-
o.__proto__ = p;
|
|
97
|
-
return o;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return _setPrototypeOf(o, p);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function _isNativeReflectConstruct() {
|
|
104
|
-
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
105
|
-
if (Reflect.construct.sham) return false;
|
|
106
|
-
if (typeof Proxy === "function") return true;
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
110
|
-
return true;
|
|
111
|
-
} catch (e) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function _assertThisInitialized(self) {
|
|
117
|
-
if (self === void 0) {
|
|
118
|
-
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return self;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function _possibleConstructorReturn(self, call) {
|
|
125
|
-
if (call && (typeof call === "object" || typeof call === "function")) {
|
|
126
|
-
return call;
|
|
127
|
-
} else if (call !== void 0) {
|
|
128
|
-
throw new TypeError("Derived constructors may only return object or undefined");
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return _assertThisInitialized(self);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function _createSuper(Derived) {
|
|
135
|
-
var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
|
136
|
-
|
|
137
|
-
return function _createSuperInternal() {
|
|
138
|
-
var Super = _getPrototypeOf(Derived),
|
|
139
|
-
result;
|
|
140
|
-
|
|
141
|
-
if (hasNativeReflectConstruct) {
|
|
142
|
-
var NewTarget = _getPrototypeOf(this).constructor;
|
|
143
|
-
|
|
144
|
-
result = Reflect.construct(Super, arguments, NewTarget);
|
|
145
|
-
} else {
|
|
146
|
-
result = Super.apply(this, arguments);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return _possibleConstructorReturn(this, result);
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function _slicedToArray(arr, i) {
|
|
154
|
-
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function _toConsumableArray(arr) {
|
|
158
|
-
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function _arrayWithoutHoles(arr) {
|
|
162
|
-
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function _arrayWithHoles(arr) {
|
|
166
|
-
if (Array.isArray(arr)) return arr;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function _iterableToArray(iter) {
|
|
170
|
-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function _iterableToArrayLimit(arr, i) {
|
|
174
|
-
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
175
|
-
|
|
176
|
-
if (_i == null) return;
|
|
177
|
-
var _arr = [];
|
|
178
|
-
var _n = true;
|
|
179
|
-
var _d = false;
|
|
180
|
-
|
|
181
|
-
var _s, _e;
|
|
182
|
-
|
|
183
|
-
try {
|
|
184
|
-
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
|
|
185
|
-
_arr.push(_s.value);
|
|
186
|
-
|
|
187
|
-
if (i && _arr.length === i) break;
|
|
188
|
-
}
|
|
189
|
-
} catch (err) {
|
|
190
|
-
_d = true;
|
|
191
|
-
_e = err;
|
|
192
|
-
} finally {
|
|
193
|
-
try {
|
|
194
|
-
if (!_n && _i["return"] != null) _i["return"]();
|
|
195
|
-
} finally {
|
|
196
|
-
if (_d) throw _e;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return _arr;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
204
|
-
if (!o) return;
|
|
205
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
206
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
207
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
208
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
209
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function _arrayLikeToArray(arr, len) {
|
|
213
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
214
|
-
|
|
215
|
-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
216
|
-
|
|
217
|
-
return arr2;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function _nonIterableSpread() {
|
|
221
|
-
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function _nonIterableRest() {
|
|
225
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Convenience map of keys
|
|
230
|
-
*/
|
|
231
|
-
var storageKeys = {};
|
|
232
|
-
/**
|
|
233
|
-
* Adds a key to the keys map
|
|
234
|
-
*
|
|
235
|
-
* @param {String} value Key name
|
|
236
|
-
*/
|
|
237
|
-
|
|
238
|
-
var addKey = function addKey(value) {
|
|
239
|
-
storageKeys[value] = value;
|
|
240
|
-
};
|
|
241
|
-
/**
|
|
242
|
-
* Adds a key to the keys map
|
|
243
|
-
* (Alias for `addKey`)
|
|
244
|
-
*
|
|
245
|
-
* @param {String} value Key name
|
|
246
|
-
*/
|
|
247
|
-
|
|
248
|
-
var key = function key(value) {
|
|
249
|
-
return addKey(value);
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* Convenience method to get internally managed storage keys
|
|
253
|
-
*
|
|
254
|
-
* @returns {Object} Storage keys map
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
var getKeys = function getKeys() {
|
|
258
|
-
return storageKeys;
|
|
259
|
-
};
|
|
260
|
-
/**
|
|
261
|
-
* Helper utility to prefix all keys in a map to use a namespace
|
|
262
|
-
*
|
|
263
|
-
* @param {String} namespace Storage namespace prefix
|
|
264
|
-
* @param {Object} keys (Optional) Storage key/values. Defaults to the internally managed keys map
|
|
265
|
-
*/
|
|
266
|
-
|
|
267
|
-
var getPrefixedKeys = function getPrefixedKeys(namespace) {
|
|
268
|
-
var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
269
|
-
var items = keys || storageKeys;
|
|
270
|
-
if (!namespace) return items;
|
|
271
|
-
return Object.keys(items).reduce(function (acc, it) {
|
|
272
|
-
return _objectSpread2(_objectSpread2({}, acc), {}, _defineProperty({}, it, "".concat(namespace, ".").concat(items[it])));
|
|
273
|
-
}, {});
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
var fakeLocalStorage = {
|
|
277
|
-
getItem: function getItem(key) {
|
|
278
|
-
return fakeLocalStorage[key];
|
|
279
|
-
},
|
|
280
|
-
setItem: function setItem(key, value) {
|
|
281
|
-
fakeLocalStorage[key] = value;
|
|
282
|
-
},
|
|
283
|
-
removeItem: function removeItem(key) {
|
|
284
|
-
delete fakeLocalStorage[key];
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Checks if a value is a primitive type
|
|
290
|
-
*
|
|
291
|
-
* @param {*} val Value to check
|
|
292
|
-
* @returns {Boolean} True if value is a primitive type
|
|
293
|
-
*/
|
|
294
|
-
|
|
295
|
-
var isPrimitive = function isPrimitive(val) {
|
|
296
|
-
var type = typeof val;
|
|
297
|
-
if (val === null) return true;
|
|
298
|
-
if (Array.isArray(val)) return false;
|
|
299
|
-
if (type === 'object') return false;
|
|
300
|
-
return type !== 'function';
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
var index = /*#__PURE__*/Object.freeze({
|
|
304
|
-
__proto__: null,
|
|
305
|
-
isPrimitive: isPrimitive,
|
|
306
|
-
addKey: addKey,
|
|
307
|
-
key: key,
|
|
308
|
-
getKeys: getKeys,
|
|
309
|
-
getPrefixedKeys: getPrefixedKeys,
|
|
310
|
-
fakeLocalStorage: fakeLocalStorage
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Base class to allow storage access
|
|
315
|
-
* @see `LocalStorageProvider.js` for an example implementation
|
|
316
|
-
*/
|
|
317
|
-
var StorageProvider = /*#__PURE__*/function () {
|
|
318
|
-
/**
|
|
319
|
-
* Initializes the class
|
|
320
|
-
* @param {String} namespace Namespace to prefix all keys with. Mostly used for the logging & reset functions
|
|
321
|
-
* @param {Object} registry (Optional) Initialize the storage provider with an existing registry
|
|
322
|
-
*/
|
|
323
|
-
function StorageProvider(namespace, registry) {
|
|
324
|
-
_classCallCheck(this, StorageProvider);
|
|
325
|
-
|
|
326
|
-
// Simulate being an abstract class
|
|
327
|
-
if ((this instanceof StorageProvider ? this.constructor : void 0) === StorageProvider) throw TypeError("StorageProvider is abstract. Extend this class to implement it");
|
|
328
|
-
this.namespace = namespace || null;
|
|
329
|
-
this.registry = registry ||
|
|
330
|
-
/* istanbul ignore next */
|
|
331
|
-
{};
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Sets the namespace for this storage provider, and migrates
|
|
335
|
-
* all stored values to the new namespace
|
|
336
|
-
* @param {String} namespace New namespace for this storage provider
|
|
337
|
-
*/
|
|
338
|
-
|
|
339
|
-
/* istanbul ignore next */
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
_createClass(StorageProvider, [{
|
|
343
|
-
key: "setNamespace",
|
|
344
|
-
value: function setNamespace(namespace) {}
|
|
345
|
-
/**
|
|
346
|
-
* Registers an item with it's initial value. This is used for logging, resetting, etc.
|
|
347
|
-
* @param {String} key Storage item's key
|
|
348
|
-
* @param {*} initialValue Storage item's initial value
|
|
349
|
-
*/
|
|
350
|
-
|
|
351
|
-
}, {
|
|
352
|
-
key: "register",
|
|
353
|
-
value: function register(key, initialValue) {
|
|
354
|
-
this.registry[key] = initialValue;
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Reads an item from storage
|
|
358
|
-
* @param {String} key Key for the item to retrieve
|
|
359
|
-
*/
|
|
360
|
-
|
|
361
|
-
/* istanbul ignore next */
|
|
362
|
-
|
|
363
|
-
}, {
|
|
364
|
-
key: "getItem",
|
|
365
|
-
value: function getItem(key) {}
|
|
366
|
-
/**
|
|
367
|
-
* Stores a value
|
|
368
|
-
* @param {String} key Item's storage key
|
|
369
|
-
* @param {String} value Item's value to store
|
|
370
|
-
*/
|
|
371
|
-
|
|
372
|
-
/* istanbul ignore next */
|
|
373
|
-
|
|
374
|
-
}, {
|
|
375
|
-
key: "setItem",
|
|
376
|
-
value: function setItem(key, value) {}
|
|
377
|
-
/**
|
|
378
|
-
* Removes an item from storage
|
|
379
|
-
* @param {String} key Item's storage key
|
|
380
|
-
* @param {Boolean} fromRegistry (Optional) If the item should also be removed from the registry
|
|
381
|
-
*/
|
|
382
|
-
|
|
383
|
-
/* istanbul ignore next */
|
|
384
|
-
|
|
385
|
-
}, {
|
|
386
|
-
key: "removeItem",
|
|
387
|
-
value: function removeItem(key) {
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Gets all stored keys & values
|
|
391
|
-
* If a `namespace` was set, only keys prefixed with the namespace will be returned
|
|
392
|
-
*/
|
|
393
|
-
|
|
394
|
-
/* istanbul ignore next */
|
|
395
|
-
|
|
396
|
-
}, {
|
|
397
|
-
key: "getAll",
|
|
398
|
-
value: function getAll() {}
|
|
399
|
-
/**
|
|
400
|
-
*
|
|
401
|
-
* @param {Boolean} useInitialValues If values should be replaced with their initial values. If false, keys are removed
|
|
402
|
-
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
403
|
-
* @param {Boolean} clearRegistry (Optional) If the registry should also be cleared
|
|
404
|
-
*/
|
|
405
|
-
|
|
406
|
-
/* istanbul ignore next */
|
|
407
|
-
|
|
408
|
-
}, {
|
|
409
|
-
key: "_resetAll",
|
|
410
|
-
value: function _resetAll() {
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* Resets all values to their initial values
|
|
414
|
-
* If a `namespace` is set, only keys prefixed with the namespace will be reset
|
|
415
|
-
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
416
|
-
*/
|
|
417
|
-
|
|
418
|
-
/* istanbul ignore next */
|
|
419
|
-
|
|
420
|
-
}, {
|
|
421
|
-
key: "resetAll",
|
|
422
|
-
value: function resetAll() {
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Removes all items from local storage.
|
|
426
|
-
* If a `namespace` is set, only keys prefixed with the namespace will be removed
|
|
427
|
-
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
428
|
-
*/
|
|
429
|
-
|
|
430
|
-
/* istanbul ignore next */
|
|
431
|
-
|
|
432
|
-
}, {
|
|
433
|
-
key: "removeAll",
|
|
434
|
-
value: function removeAll() {
|
|
435
|
-
}
|
|
436
|
-
}]);
|
|
437
|
-
|
|
438
|
-
return StorageProvider;
|
|
439
|
-
}();
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* A storage provider for `localStorage`
|
|
443
|
-
* @see `StorageProvider.js` for documentation
|
|
444
|
-
*/
|
|
445
|
-
|
|
446
|
-
var LocalStorageProvider = /*#__PURE__*/function (_StorageProvider) {
|
|
447
|
-
_inherits(LocalStorageProvider, _StorageProvider);
|
|
448
|
-
|
|
449
|
-
var _super = _createSuper(LocalStorageProvider);
|
|
450
|
-
|
|
451
|
-
function LocalStorageProvider() {
|
|
452
|
-
var _this;
|
|
453
|
-
|
|
454
|
-
var namespace = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
455
|
-
var registry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
456
|
-
|
|
457
|
-
_classCallCheck(this, LocalStorageProvider);
|
|
458
|
-
|
|
459
|
-
_this = _super.call(this, namespace, registry);
|
|
460
|
-
_this.storage = _this.getStorage();
|
|
461
|
-
return _this;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
_createClass(LocalStorageProvider, [{
|
|
465
|
-
key: "getStorage",
|
|
466
|
-
value: function getStorage() {
|
|
467
|
-
try {
|
|
468
|
-
return window.localStorage;
|
|
469
|
-
} catch (e) {
|
|
470
|
-
/* istanbul ignore next */
|
|
471
|
-
console.warn('LocalStorageProvider: localStorage not supported');
|
|
472
|
-
/* istanbul ignore next */
|
|
473
|
-
|
|
474
|
-
return fakeLocalStorage;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}, {
|
|
478
|
-
key: "setNamespace",
|
|
479
|
-
value: function setNamespace(namespace) {
|
|
480
|
-
if (!this.namespace) {
|
|
481
|
-
this.namespace = namespace;
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
if (this.namespace === namespace) return;
|
|
486
|
-
var items = JSON.parse(JSON.stringify(this.getAll()));
|
|
487
|
-
this.removeAll();
|
|
488
|
-
|
|
489
|
-
for (var _i = 0, _Object$entries = Object.entries(items); _i < _Object$entries.length; _i++) {
|
|
490
|
-
var _ref3 = _Object$entries[_i];
|
|
491
|
-
|
|
492
|
-
var _ref2 = _slicedToArray(_ref3, 2);
|
|
493
|
-
|
|
494
|
-
var key = _ref2[0];
|
|
495
|
-
var value = _ref2[1];
|
|
496
|
-
var newKey = key.replace(this.namespace, namespace);
|
|
497
|
-
this.setItem(newKey, value);
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
this.namespace = namespace;
|
|
501
|
-
}
|
|
502
|
-
}, {
|
|
503
|
-
key: "getItem",
|
|
504
|
-
value: function getItem(key) {
|
|
505
|
-
var val = this.storage.getItem(key);
|
|
506
|
-
if (val === undefined || val === null) return null;
|
|
507
|
-
|
|
508
|
-
try {
|
|
509
|
-
return JSON.parse(val);
|
|
510
|
-
} catch (e) {
|
|
511
|
-
return val;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}, {
|
|
515
|
-
key: "setItem",
|
|
516
|
-
value: function setItem(key, value) {
|
|
517
|
-
var val = value; // Don't allow "null" & similar values to be stringified
|
|
518
|
-
|
|
519
|
-
if (val !== undefined && val !== null) val = isPrimitive(value) ? value : JSON.stringify(value);
|
|
520
|
-
return this.storage.setItem(key, val);
|
|
521
|
-
}
|
|
522
|
-
}, {
|
|
523
|
-
key: "removeItem",
|
|
524
|
-
value: function removeItem(key) {
|
|
525
|
-
var fromRegistry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
526
|
-
if (fromRegistry) delete this.registry[key];
|
|
527
|
-
return this.storage.removeItem(key);
|
|
528
|
-
}
|
|
529
|
-
}, {
|
|
530
|
-
key: "getAll",
|
|
531
|
-
value: function getAll() {
|
|
532
|
-
var _this2 = this;
|
|
533
|
-
|
|
534
|
-
var prefixNs = "".concat(this.namespace, ".");
|
|
535
|
-
return Object.keys(this.storage).reduce(function (acc, it) {
|
|
536
|
-
if (_this2.namespace ? it.startsWith(prefixNs) : true) acc[it] = _this2.storage.getItem(it);
|
|
537
|
-
return acc;
|
|
538
|
-
}, {});
|
|
539
|
-
}
|
|
540
|
-
}, {
|
|
541
|
-
key: "_resetAll",
|
|
542
|
-
value: function _resetAll() {
|
|
543
|
-
var _this3 = this;
|
|
544
|
-
|
|
545
|
-
var useInitialValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
546
|
-
var excludedKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
547
|
-
var clearRegistry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
548
|
-
var prefixNs = "".concat(this.namespace, ".");
|
|
549
|
-
Object.keys(localStorage).forEach(function (it) {
|
|
550
|
-
var isAppKey = _this3.namespace ? it.startsWith(prefixNs) : true;
|
|
551
|
-
var isExcluded = (excludedKeys === null || excludedKeys === void 0 ? void 0 : excludedKeys.includes(it)) || false;
|
|
552
|
-
if (!isAppKey || isExcluded) return;
|
|
553
|
-
|
|
554
|
-
if (useInitialValues) {
|
|
555
|
-
var isRegistered = Object.prototype.hasOwnProperty.call(_this3.registry, it);
|
|
556
|
-
if (isRegistered) _this3.storage.setItem(it, _this3.registry[it]);else _this3.storage.removeItem(it);
|
|
557
|
-
} else {
|
|
558
|
-
_this3.storage.removeItem(it);
|
|
559
|
-
|
|
560
|
-
if (clearRegistry) delete _this3.registry[it];
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
}, {
|
|
565
|
-
key: "resetAll",
|
|
566
|
-
value: function resetAll() {
|
|
567
|
-
var excludedKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
568
|
-
var clearRegistry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
569
|
-
|
|
570
|
-
this._resetAll(true, excludedKeys || [], clearRegistry);
|
|
571
|
-
}
|
|
572
|
-
}, {
|
|
573
|
-
key: "removeAll",
|
|
574
|
-
value: function removeAll() {
|
|
575
|
-
var excludedKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
576
|
-
var clearRegistry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
577
|
-
|
|
578
|
-
this._resetAll(false, excludedKeys || [], clearRegistry);
|
|
579
|
-
}
|
|
580
|
-
}]);
|
|
581
|
-
|
|
582
|
-
return LocalStorageProvider;
|
|
583
|
-
}(StorageProvider);
|
|
584
|
-
|
|
585
|
-
var defaultOptions = {
|
|
586
|
-
logging: {
|
|
587
|
-
enabled: false
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
var Provider = LocalStorageProvider;
|
|
591
|
-
var storage = new Provider();
|
|
592
|
-
|
|
593
|
-
var options = _objectSpread2({}, defaultOptions);
|
|
594
|
-
|
|
595
|
-
var pendingLogs = [];
|
|
596
|
-
/**
|
|
597
|
-
* Gets the namespace of the storage provider
|
|
598
|
-
*
|
|
599
|
-
* @returns {String}
|
|
600
|
-
*/
|
|
601
|
-
|
|
602
|
-
var getNamespace = function getNamespace() {
|
|
603
|
-
return storage.namespace;
|
|
604
|
-
};
|
|
605
|
-
/**
|
|
606
|
-
* Gets the current storage provider class instance
|
|
607
|
-
*
|
|
608
|
-
* @returns {StorageProvider}
|
|
609
|
-
*/
|
|
610
|
-
|
|
611
|
-
var getStorage = function getStorage() {
|
|
612
|
-
return storage;
|
|
613
|
-
};
|
|
614
|
-
var getOptions = function getOptions() {
|
|
615
|
-
return options;
|
|
616
|
-
};
|
|
617
|
-
/**
|
|
618
|
-
* Sets the namespace for the storage provider
|
|
619
|
-
*
|
|
620
|
-
* @param {String} namespace The namespace for the storage provider
|
|
621
|
-
*/
|
|
622
|
-
|
|
623
|
-
var setNamespace = function setNamespace(namespace) {
|
|
624
|
-
storage.setNamespace(namespace);
|
|
625
|
-
storage = new Provider(namespace || getNamespace());
|
|
626
|
-
};
|
|
627
|
-
var setOptions = function setOptions(value) {
|
|
628
|
-
options = _objectSpread2(_objectSpread2({}, options), value);
|
|
629
|
-
/* istanbul ignore next */
|
|
630
|
-
|
|
631
|
-
if (options.logging.enabled) {
|
|
632
|
-
console.info('Flushing', pendingLogs.length, 'pending logs');
|
|
633
|
-
|
|
634
|
-
while (pendingLogs.length) {
|
|
635
|
-
var _console;
|
|
636
|
-
|
|
637
|
-
/* istanbul ignore next */
|
|
638
|
-
(_console = console).log.apply(_console, _toConsumableArray(pendingLogs.shift()));
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
var log = function log() {
|
|
644
|
-
var _console2;
|
|
645
|
-
|
|
646
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
647
|
-
args[_key] = arguments[_key];
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/* istanbul ignore next */
|
|
651
|
-
if (options.logging.enabled)
|
|
652
|
-
/* istanbul ignore next */
|
|
653
|
-
(_console2 = console).log.apply(_console2, args);else pendingLogs.push(args);
|
|
654
|
-
};
|
|
655
|
-
/**
|
|
656
|
-
* Creates a persisted Wire using the `StorageProvider` that is currently set
|
|
657
|
-
* Defaults to `localStorage` via `LocalStorageProvider`
|
|
658
|
-
*
|
|
659
|
-
* @param {String} key Unique key for storing this value
|
|
660
|
-
* @param {*} value Initial value of this Wire
|
|
661
|
-
* @returns A new Wire decorated with localStorage functionality
|
|
662
|
-
*/
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
var createPersistedWire = function createPersistedWire(key) {
|
|
666
|
-
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
667
|
-
// This check helps ensure no accidental key typos occur
|
|
668
|
-
if (!key && typeof key !== 'number') throw new Error("createPersistedWire: Key cannot be a falsey value (".concat(key, "}")); // Track this writable entry so we can easily clear all
|
|
669
|
-
|
|
670
|
-
storage.register(key, value); // The actual Wire backing object
|
|
671
|
-
|
|
672
|
-
var wire = createWire(value);
|
|
673
|
-
|
|
674
|
-
var getValue = function getValue() {
|
|
675
|
-
return wire.getValue();
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
var setValue = function setValue(newValue) {
|
|
679
|
-
storage.setItem(key, newValue);
|
|
680
|
-
return wire.setValue(newValue);
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
var subscribe = function subscribe(fn) {
|
|
684
|
-
wire.subscribe(fn);
|
|
685
|
-
};
|
|
686
|
-
|
|
687
|
-
var storedValue = storage.getItem(key);
|
|
688
|
-
var initialValue = storedValue === null ? value : storedValue;
|
|
689
|
-
log('react-wire-persisted: create', key, {
|
|
690
|
-
value: value,
|
|
691
|
-
storedValue: storedValue,
|
|
692
|
-
initialValue: initialValue
|
|
693
|
-
});
|
|
694
|
-
if (initialValue !== value) setValue(initialValue);
|
|
695
|
-
return _objectSpread2(_objectSpread2({}, wire), {}, {
|
|
696
|
-
getValue: getValue,
|
|
697
|
-
setValue: setValue,
|
|
698
|
-
subscribe: subscribe
|
|
699
|
-
});
|
|
700
|
-
};
|
|
701
|
-
|
|
702
|
-
export { createPersistedWire, defaultOptions, getNamespace, getOptions, getStorage, setNamespace, setOptions, index as utils };
|