|
1 | | -import {LocalStorageEmitter} from './LocalStorageEmitter'; |
| 1 | +import {LocalStorageEmitter} from "./LocalStorageEmitter"; |
2 | 2 |
|
3 | | -interface IWebStorage{ |
4 | | - getItem:(key:string) => string; |
5 | | - setItem:(key:string, value: string) => void; |
| 3 | +interface IWebStorage { |
| 4 | + getItem: (key: string) => string; |
| 5 | + setItem: (key: string, value: string) => void; |
6 | 6 | } |
7 | 7 |
|
8 | | -export function LocalStorage(storageKey?: string){ |
| 8 | +export function LocalStorage(storageKey?: string) { |
9 | 9 | return WebStorage(storageKey, localStorage); |
10 | 10 | } |
11 | 11 |
|
12 | | -export function SessionStorage(storageKey?: string){ |
| 12 | +export function SessionStorage(storageKey?: string) { |
13 | 13 | return WebStorage(storageKey, sessionStorage); |
14 | 14 | } |
15 | 15 |
|
16 | | -function WebStorage(storageKey:string, webStorage: IWebStorage) { |
17 | | - return (target:Object, decoratedPropertyName?:string):void => { |
| 16 | +function WebStorage(storageKey: string, webStorage: IWebStorage) { |
| 17 | + return (target: Object, decoratedPropertyName?: string): void => { |
18 | 18 | if (!webStorage) { |
19 | 19 | return; |
20 | 20 | } |
21 | 21 |
|
22 | 22 | if (!storageKey) { |
23 | | - storageKey = '' + '/' + decoratedPropertyName; |
| 23 | + storageKey = "" + "/" + decoratedPropertyName; |
24 | 24 | } |
25 | 25 |
|
26 | | - Object.defineProperty(target, '_' + decoratedPropertyName + '_mapped', { |
| 26 | + Object.defineProperty(target, "_" + decoratedPropertyName + "_mapped", { |
27 | 27 | enumerable: false, |
28 | 28 | configurable: true, |
29 | 29 | writable: true, |
30 | 30 | value: false |
31 | 31 | }); |
32 | 32 |
|
33 | | - var instances:any = []; |
34 | | - var values = {}; |
| 33 | + let instances: any = []; |
| 34 | + let values = {}; |
35 | 35 |
|
36 | | - var storageValue = webStorage.getItem(storageKey) || null; |
37 | | - var storageValueJSON = storageValue; |
38 | | - if ('string' === typeof storageValue) { |
| 36 | + let storageValue = webStorage.getItem(storageKey) || null; |
| 37 | + let storageValueJSON = storageValue; |
| 38 | + if ("string" === typeof storageValue) { |
39 | 39 | try { |
40 | 40 | storageValue = JSON.parse(storageValue); |
41 | | - } catch(e) { |
| 41 | + } catch (e) { |
42 | 42 | storageValue = null; |
43 | | - storageValueJSON = 'null'; |
| 43 | + storageValueJSON = "null"; |
44 | 44 | } |
45 | 45 | } |
46 | | - var oldJSONValues = {}; |
| 46 | + let oldJSONValues = {}; |
47 | 47 |
|
48 | 48 | Object.defineProperty(target, decoratedPropertyName, { |
49 | 49 | get: function () { |
50 | | - if (false === this['_' + decoratedPropertyName + '_mapped']) { |
51 | | - this['_' + decoratedPropertyName + '_mapped'] = instances.length; |
| 50 | + if (false === this["_" + decoratedPropertyName + "_mapped"]) { |
| 51 | + this["_" + decoratedPropertyName + "_mapped"] = instances.length; |
52 | 52 |
|
53 | | - //first registration triggers a setting to localStorage value |
| 53 | + // first registration triggers a setting to localStorage value |
54 | 54 | values[instances.length] = storageValue; |
55 | 55 | oldJSONValues[instances.length] = storageValueJSON; |
56 | 56 |
|
57 | 57 | instances.push(this); |
58 | 58 | } |
59 | | - return values[this['_' + decoratedPropertyName + '_mapped']]; |
| 59 | + return values[this["_" + decoratedPropertyName + "_mapped"]]; |
60 | 60 | }, |
61 | 61 | set: function (newValue) { |
62 | | - if (false === this['_' + decoratedPropertyName + '_mapped']) { |
63 | | - this['_' + decoratedPropertyName + '_mapped'] = instances.length; |
| 62 | + if (false === this["_" + decoratedPropertyName + "_mapped"]) { |
| 63 | + this["_" + decoratedPropertyName + "_mapped"] = instances.length; |
64 | 64 |
|
65 | | - //first registration triggers a setting to localStorage value |
| 65 | + // first registration triggers a setting to localStorage value |
66 | 66 | values[instances.length] = storageValue; |
67 | 67 | oldJSONValues[instances.length] = storageValueJSON; |
68 | 68 |
|
69 | 69 | instances.push(this); |
70 | | - //first 'set' call is ignored if we have already a value from the localStorage |
| 70 | + // first "set" call is ignored if we have already a value from the localStorage |
71 | 71 | if (storageValue) { |
72 | 72 | return; |
73 | 73 | } |
74 | 74 | } |
75 | | - values[this['_' + decoratedPropertyName + '_mapped']] = newValue; |
| 75 | + values[this["_" + decoratedPropertyName + "_mapped"]] = newValue; |
76 | 76 | }, |
77 | 77 | enumerable: true, |
78 | 78 | configurable: true |
79 | 79 | }); |
80 | 80 |
|
81 | 81 | LocalStorageEmitter.subscribe(() => { |
82 | 82 | for (let instance of instances) { |
83 | | - var currentValue = JSON.stringify(instance[decoratedPropertyName]); |
84 | | - var oldJSONValue = oldJSONValues[instance['_' + decoratedPropertyName + '_mapped']]; |
| 83 | + let currentValue = JSON.stringify(instance[decoratedPropertyName]); |
| 84 | + let oldJSONValue = oldJSONValues[instance["_" + decoratedPropertyName + "_mapped"]]; |
85 | 85 | if (currentValue !== oldJSONValue) { |
86 | | - oldJSONValues[instance['_' + decoratedPropertyName + '_mapped']] = currentValue; |
| 86 | + oldJSONValues[instance["_" + decoratedPropertyName + "_mapped"]] = currentValue; |
87 | 87 | webStorage.setItem(storageKey, currentValue); |
88 | 88 | } |
89 | 89 | } |
90 | 90 | }); |
91 | | - } |
| 91 | + }; |
92 | 92 | } |
0 commit comments