| 123 | return value == undefined ? _.cloneDeep(defaultValue)! : fromString(value)!; |
| 124 | } |
| 125 | function set(key: string, value: T, options: SetterOptions<T> = {}): void { |
| 126 | const { |
| 127 | defaultValue, |
| 128 | useLocalStorage = false, |
| 129 | useLocationReplace = false, |
| 130 | } = options; |
| 131 | const stringValue = toString(value); |
| 132 | if (useLocalStorage) { |
| 133 | window.localStorage.setItem(key, stringValue); |
| 134 | // Because of listeners.ts:[1], we need to manually notify all UI elements |
| 135 | // listening to storage within the tab of a change. |
| 136 | fireStorageChanged(); |
| 137 | } else if (!_.isEqual(value, get(key, {useLocalStorage}))) { |
| 138 | if (_.isEqual(value, defaultValue)) { |
| 139 | unsetFromURI(key, useLocationReplace); |
| 140 | } else { |
| 141 | const items = componentToDict(readComponent()); |
| 142 | items[key] = stringValue; |
| 143 | writeComponent(dictToComponent(items), useLocationReplace); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | /** |
| 148 | * Returns a function that can be used on a `value` declaration to a Polymer |
| 149 | * property. It updates the `polymerProperty` when storage changes -- i.e., |