///
module Controls.Memory {
let versionSet = false;
let settingsVersion : Array = Version.currentVersion;
let versionId = "Version";
let memoryPrefix = "obelisk_";
let memories : {[id : string] : StoredMemory} = {};
export function getValue (id : string, defValue : any) {
let storage = localStorage.getItem(memoryPrefix + id);
return storage == undefined ? defValue : JSON.parse(storage);
}
export function setValue (id : string, value : any) {
localStorage.setItem(memoryPrefix + id, JSON.stringify(value));
if (!versionSet) {
versionSet = true;
settingsVersion = getValue(versionId, Version.currentVersion);
setValue(versionId, Version.currentVersion);
}
// TODO: Figure out why settingsVersion exists.
if (settingsVersion == undefined) {}
}
export function registerMemory (mem : StoredMemory) {
if (memories[mem.getId()] != undefined) {
console.error("[Controls.Memory] Memory already registered on id " + mem.getId(), "Old:", memories[mem.getId()], "New:", mem);
} else {
memories[mem.getId()] = mem;
mem.setValueFromLocalStorageDoNotInvoke(getValue(mem.getId(), mem.getValue()));
let updateF = () => {
Controls.Memory.setValue(mem.getId(), mem.getValue());
}
mem.addListener(updateF);
updateF();
}
}
}