1
1

Memory.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /// <reference path="../../Version.ts" />
  2. module Controls.Memory {
  3. let versionSet = false;
  4. let settingsVersion : Array<number> = Version.currentVersion;
  5. let versionId = "Version";
  6. let memoryPrefix = "obelisk_";
  7. let memories : {[id : string] : StoredMemory<any>} = {};
  8. export function getValue (id : string, defValue : any) {
  9. let storage = localStorage.getItem(memoryPrefix + id);
  10. return storage == undefined ? defValue : JSON.parse(storage);
  11. }
  12. export function setValue (id : string, value : any) {
  13. localStorage.setItem(memoryPrefix + id, JSON.stringify(value));
  14. if (!versionSet) {
  15. versionSet = true;
  16. settingsVersion = getValue(versionId, Version.currentVersion);
  17. setValue(versionId, Version.currentVersion);
  18. }
  19. // TODO: Figure out why settingsVersion exists.
  20. if (settingsVersion == undefined) {}
  21. }
  22. export function registerMemory (mem : StoredMemory<any>) {
  23. if (memories[mem.getId()] != undefined) {
  24. console.error("[Controls.Memory] Memory already registered on id " + mem.getId(), "Old:", memories[mem.getId()], "New:", mem);
  25. } else {
  26. memories[mem.getId()] = mem;
  27. mem.setValueFromLocalStorageDoNotInvoke(getValue(mem.getId(), mem.getValue()));
  28. let updateF = () => {
  29. Controls.Memory.setValue(mem.getId(), mem.getValue());
  30. }
  31. mem.addListener(updateF);
  32. updateF();
  33. }
  34. }
  35. }