richstorage.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 with attached metadata
  16. * persistence. You probably don't want to use this class directly as it
  17. * does not save any metadata by itself. It only provides the necessary
  18. * infrastructure for subclasses that need to save metadata along with
  19. * values stored.
  20. *
  21. */
  22. goog.provide('goog.storage.RichStorage');
  23. goog.provide('goog.storage.RichStorage.Wrapper');
  24. goog.require('goog.storage.ErrorCode');
  25. goog.require('goog.storage.Storage');
  26. /**
  27. * Provides a storage for data with attached metadata.
  28. *
  29. * @param {!goog.storage.mechanism.Mechanism} mechanism The underlying
  30. * storage mechanism.
  31. * @constructor
  32. * @struct
  33. * @extends {goog.storage.Storage}
  34. */
  35. goog.storage.RichStorage = function(mechanism) {
  36. goog.storage.RichStorage.base(this, 'constructor', mechanism);
  37. };
  38. goog.inherits(goog.storage.RichStorage, goog.storage.Storage);
  39. /**
  40. * Metadata key under which the actual data is stored.
  41. *
  42. * @type {string}
  43. * @protected
  44. */
  45. goog.storage.RichStorage.DATA_KEY = 'data';
  46. /**
  47. * Wraps a value so metadata can be associated with it. You probably want
  48. * to use goog.storage.RichStorage.Wrapper.wrapIfNecessary to avoid multiple
  49. * embeddings.
  50. *
  51. * @param {*} value The value to wrap.
  52. * @constructor
  53. * @final
  54. */
  55. goog.storage.RichStorage.Wrapper = function(value) {
  56. this[goog.storage.RichStorage.DATA_KEY] = value;
  57. };
  58. /**
  59. * Convenience method for wrapping a value so metadata can be associated with
  60. * it. No-op if the value is already wrapped or is undefined.
  61. *
  62. * @param {*} value The value to wrap.
  63. * @return {(!goog.storage.RichStorage.Wrapper|undefined)} The wrapper.
  64. */
  65. goog.storage.RichStorage.Wrapper.wrapIfNecessary = function(value) {
  66. if (!goog.isDef(value) || value instanceof goog.storage.RichStorage.Wrapper) {
  67. return /** @type {(!goog.storage.RichStorage.Wrapper|undefined)} */ (value);
  68. }
  69. return new goog.storage.RichStorage.Wrapper(value);
  70. };
  71. /**
  72. * Unwraps a value, any metadata is discarded (not returned). You might want to
  73. * use goog.storage.RichStorage.Wrapper.unwrapIfPossible to handle cases where
  74. * the wrapper is missing.
  75. *
  76. * @param {!Object} wrapper The wrapper.
  77. * @return {*} The wrapped value.
  78. */
  79. goog.storage.RichStorage.Wrapper.unwrap = function(wrapper) {
  80. var value = wrapper[goog.storage.RichStorage.DATA_KEY];
  81. if (!goog.isDef(value)) {
  82. throw goog.storage.ErrorCode.INVALID_VALUE;
  83. }
  84. return value;
  85. };
  86. /**
  87. * Convenience method for unwrapping a value. Returns undefined if the
  88. * wrapper is missing.
  89. *
  90. * @param {(!Object|undefined)} wrapper The wrapper.
  91. * @return {*} The wrapped value or undefined.
  92. */
  93. goog.storage.RichStorage.Wrapper.unwrapIfPossible = function(wrapper) {
  94. if (!wrapper) {
  95. return undefined;
  96. }
  97. return goog.storage.RichStorage.Wrapper.unwrap(wrapper);
  98. };
  99. /** @override */
  100. goog.storage.RichStorage.prototype.set = function(key, value) {
  101. goog.storage.RichStorage.base(
  102. this, 'set', key,
  103. goog.storage.RichStorage.Wrapper.wrapIfNecessary(value));
  104. };
  105. /**
  106. * Get an item wrapper (the item and its metadata) from the storage.
  107. *
  108. * WARNING: This returns an Object, which once used to be
  109. * goog.storage.RichStorage.Wrapper. This is due to the fact
  110. * that deserialized objects lose type information and it
  111. * is hard to do proper typecasting in JavaScript. Be sure
  112. * you know what you are doing when using the returned value.
  113. *
  114. * @param {string} key The key to get.
  115. * @return {(!Object|undefined)} The wrapper, or undefined if not found.
  116. */
  117. goog.storage.RichStorage.prototype.getWrapper = function(key) {
  118. var wrapper = goog.storage.RichStorage.superClass_.get.call(this, key);
  119. if (!goog.isDef(wrapper) || wrapper instanceof Object) {
  120. return /** @type {(!Object|undefined)} */ (wrapper);
  121. }
  122. throw goog.storage.ErrorCode.INVALID_VALUE;
  123. };
  124. /** @override */
  125. goog.storage.RichStorage.prototype.get = function(key) {
  126. return goog.storage.RichStorage.Wrapper.unwrapIfPossible(
  127. this.getWrapper(key));
  128. };