()
| 2 | import { getConfigKey } from './constants/storage.const'; |
| 3 | |
| 4 | function save() { |
| 5 | // eslint-disable-next-line @typescript-eslint/ban-types |
| 6 | return function(target: Object, propertyKey: string) { |
| 7 | const localStorageKey = getConfigKey(target.constructor.name, propertyKey); |
| 8 | const storedValue = localStorage.getItem(localStorageKey); |
| 9 | let value = JSON.parse(storedValue ?? 'null'); |
| 10 | const wasStored = storedValue !== null; |
| 11 | let isInitialized = false; |
| 12 | |
| 13 | const setter = (newVal: unknown) => { |
| 14 | if(!wasStored || isInitialized) { |
| 15 | value = newVal; |
| 16 | if(isInitialized) { |
| 17 | localStorage.setItem(localStorageKey, JSON.stringify(value)); |
| 18 | } |
| 19 | } |
| 20 | isInitialized = true; |
| 21 | }; |
| 22 | |
| 23 | Object.defineProperty(target, propertyKey, { |
| 24 | get: () => value, |
| 25 | set: setter |
| 26 | }); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | |
| 31 |
no test coverage detected