storage.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. 'use strict';
  5. ;(function() {
  6. /************************************************
  7. *** Utilities to store data in local storage ***
  8. ************************************************/
  9. window.textsecure = window.textsecure || {};
  10. window.textsecure.storage = window.textsecure.storage || {};
  11. // Overrideable storage implementation
  12. window.textsecure.storage.impl = window.textsecure.storage.impl || {
  13. /*****************************
  14. *** Base Storage Routines ***
  15. *****************************/
  16. put: function(key, value) {
  17. if (value === undefined)
  18. throw new Error("Tried to store undefined");
  19. localStorage.setItem("" + key, textsecure.utils.jsonThing(value));
  20. },
  21. get: function(key, defaultValue) {
  22. var value = localStorage.getItem("" + key);
  23. if (value === null)
  24. return defaultValue;
  25. return JSON.parse(value);
  26. },
  27. remove: function(key) {
  28. localStorage.removeItem("" + key);
  29. },
  30. };
  31. window.textsecure.storage.put = function(key, value) {
  32. return textsecure.storage.impl.put(key, value);
  33. };
  34. window.textsecure.storage.get = function(key, defaultValue) {
  35. return textsecure.storage.impl.get(key, defaultValue);
  36. };
  37. window.textsecure.storage.remove = function(key) {
  38. return textsecure.storage.impl.remove(key);
  39. };
  40. })();