1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /// <reference path="../../Version.ts" />
- module Controls.Memory {
- let versionSet = false;
- let settingsVersion : Array<number> = Version.currentVersion;
- let versionId = "Version";
- let memoryPrefix = "obelisk_";
- let memories : {[id : string] : StoredMemory<any>} = {};
- 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<any>) {
- 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();
- }
- }
- }
|