1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Provides a convenient API for data with attached metadata
* persistence. You probably don't want to use this class directly as it
* does not save any metadata by itself. It only provides the necessary
* infrastructure for subclasses that need to save metadata along with
* values stored.
*
*/
goog.provide('goog.storage.RichStorage');
goog.provide('goog.storage.RichStorage.Wrapper');
goog.require('goog.storage.ErrorCode');
goog.require('goog.storage.Storage');
/**
* Provides a storage for data with attached metadata.
*
* @param {!goog.storage.mechanism.Mechanism} mechanism The underlying
* storage mechanism.
* @constructor
* @struct
* @extends {goog.storage.Storage}
*/
goog.storage.RichStorage = function(mechanism) {
goog.storage.RichStorage.base(this, 'constructor', mechanism);
};
goog.inherits(goog.storage.RichStorage, goog.storage.Storage);
/**
* Metadata key under which the actual data is stored.
*
* @type {string}
* @protected
*/
goog.storage.RichStorage.DATA_KEY = 'data';
/**
* Wraps a value so metadata can be associated with it. You probably want
* to use goog.storage.RichStorage.Wrapper.wrapIfNecessary to avoid multiple
* embeddings.
*
* @param {*} value The value to wrap.
* @constructor
* @final
*/
goog.storage.RichStorage.Wrapper = function(value) {
this[goog.storage.RichStorage.DATA_KEY] = value;
};
/**
* Convenience method for wrapping a value so metadata can be associated with
* it. No-op if the value is already wrapped or is undefined.
*
* @param {*} value The value to wrap.
* @return {(!goog.storage.RichStorage.Wrapper|undefined)} The wrapper.
*/
goog.storage.RichStorage.Wrapper.wrapIfNecessary = function(value) {
if (!goog.isDef(value) || value instanceof goog.storage.RichStorage.Wrapper) {
return /** @type {(!goog.storage.RichStorage.Wrapper|undefined)} */ (value);
}
return new goog.storage.RichStorage.Wrapper(value);
};
/**
* Unwraps a value, any metadata is discarded (not returned). You might want to
* use goog.storage.RichStorage.Wrapper.unwrapIfPossible to handle cases where
* the wrapper is missing.
*
* @param {!Object} wrapper The wrapper.
* @return {*} The wrapped value.
*/
goog.storage.RichStorage.Wrapper.unwrap = function(wrapper) {
var value = wrapper[goog.storage.RichStorage.DATA_KEY];
if (!goog.isDef(value)) {
throw goog.storage.ErrorCode.INVALID_VALUE;
}
return value;
};
/**
* Convenience method for unwrapping a value. Returns undefined if the
* wrapper is missing.
*
* @param {(!Object|undefined)} wrapper The wrapper.
* @return {*} The wrapped value or undefined.
*/
goog.storage.RichStorage.Wrapper.unwrapIfPossible = function(wrapper) {
if (!wrapper) {
return undefined;
}
return goog.storage.RichStorage.Wrapper.unwrap(wrapper);
};
/** @override */
goog.storage.RichStorage.prototype.set = function(key, value) {
goog.storage.RichStorage.base(
this, 'set', key,
goog.storage.RichStorage.Wrapper.wrapIfNecessary(value));
};
/**
* Get an item wrapper (the item and its metadata) from the storage.
*
* WARNING: This returns an Object, which once used to be
* goog.storage.RichStorage.Wrapper. This is due to the fact
* that deserialized objects lose type information and it
* is hard to do proper typecasting in JavaScript. Be sure
* you know what you are doing when using the returned value.
*
* @param {string} key The key to get.
* @return {(!Object|undefined)} The wrapper, or undefined if not found.
*/
goog.storage.RichStorage.prototype.getWrapper = function(key) {
var wrapper = goog.storage.RichStorage.superClass_.get.call(this, key);
if (!goog.isDef(wrapper) || wrapper instanceof Object) {
return /** @type {(!Object|undefined)} */ (wrapper);
}
throw goog.storage.ErrorCode.INVALID_VALUE;
};
/** @override */
goog.storage.RichStorage.prototype.get = function(key) {
return goog.storage.RichStorage.Wrapper.unwrapIfPossible(
this.getWrapper(key));
};
|