storage.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides a convenient API for data persistence using a selected
  16. * data storage mechanism.
  17. *
  18. */
  19. goog.provide('goog.storage.Storage');
  20. goog.require('goog.json');
  21. goog.require('goog.storage.ErrorCode');
  22. /**
  23. * The base implementation for all storage APIs.
  24. *
  25. * @param {!goog.storage.mechanism.Mechanism} mechanism The underlying
  26. * storage mechanism.
  27. * @constructor
  28. * @struct
  29. */
  30. goog.storage.Storage = function(mechanism) {
  31. /**
  32. * The mechanism used to persist key-value pairs.
  33. *
  34. * @protected {goog.storage.mechanism.Mechanism}
  35. */
  36. this.mechanism = mechanism;
  37. };
  38. /**
  39. * Sets an item in the data storage.
  40. *
  41. * @param {string} key The key to set.
  42. * @param {*} value The value to serialize to a string and save.
  43. */
  44. goog.storage.Storage.prototype.set = function(key, value) {
  45. if (!goog.isDef(value)) {
  46. this.mechanism.remove(key);
  47. return;
  48. }
  49. this.mechanism.set(key, goog.json.serialize(value));
  50. };
  51. /**
  52. * Gets an item from the data storage.
  53. *
  54. * @param {string} key The key to get.
  55. * @return {*} Deserialized value or undefined if not found.
  56. */
  57. goog.storage.Storage.prototype.get = function(key) {
  58. var json;
  59. try {
  60. json = this.mechanism.get(key);
  61. } catch (e) {
  62. // If, for any reason, the value returned by a mechanism's get method is not
  63. // a string, an exception is thrown. In this case, we must fail gracefully
  64. // instead of propagating the exception to clients. See b/8095488 for
  65. // details.
  66. return undefined;
  67. }
  68. if (goog.isNull(json)) {
  69. return undefined;
  70. }
  71. try {
  72. return goog.json.parse(json);
  73. } catch (e) {
  74. throw goog.storage.ErrorCode.INVALID_VALUE;
  75. }
  76. };
  77. /**
  78. * Removes an item from the data storage.
  79. *
  80. * @param {string} key The key to remove.
  81. */
  82. goog.storage.Storage.prototype.remove = function(key) {
  83. this.mechanism.remove(key);
  84. };