///
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) {
try {
let storage = localStorage.getItem(memoryPrefix + id);
return storage == undefined ? defValue : JSON.parse(storage);
} catch (e) {
// no localStorage
console.error("[MEMORY] Can't use localStorage!");
return defValue;
}
}
export function setValue (id : string, value : any) {
try {
localStorage.setItem(memoryPrefix + id, JSON.stringify(value));
} catch (e) {
console.error("[MEMORY] Can't use localStorage!");
}
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();
}
}
}