Last active
December 31, 2019 13:49
-
-
Save pzuraq/5355b33928bd5635e0732bfbf0e0a232 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { get, notifyPropertyChange } from '@ember/object'; | |
export function inLocalStorage( | |
target, | |
propertyKey, | |
descriptor | |
) { | |
const targetName = target.constructor.name; | |
const { initializer } = descriptor; | |
return { | |
configurable: true, | |
enumerable: true, | |
get() { | |
const key = `${targetName}-${propertyKey}`; | |
const lsValue = localStorage.getItem(key); | |
const json = (lsValue && JSON.parse(lsValue)) || { value: initializer() }; | |
get(this, key); | |
return json.value; | |
}, | |
set(value) { | |
const key = `${targetName}-${propertyKey}`; | |
const lsValue = JSON.stringify({ value }); | |
localStorage.setItem(key, lsValue); | |
// this is required to dirty the change tracking system | |
notifyPropertyChange(this, keys); | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment