angular-persistence
Version:
A library to handle persistence for Angular 2 applications.
103 lines • 2.98 kB
JavaScript
var /** @type {?} */ INFO_KEY = '__INFO';
/**
* An internal object used to track items saved by a storage object within the persistence
* framework.
*
* @export
* \@class ContainerInfo
*
* @author Scott O'Bryan
* \@since 1.0
*/
export var ContainerInfo = (function () {
/**
* @param {?} _namespace
* @param {?} _container
*/
function ContainerInfo(_namespace, _container) {
this._namespace = _namespace;
this._container = _container;
var infoObj = _container.get(this._namespace);
// If we have an existing object, check its type
if (infoObj) {
if (typeof infoObj !== 'object' || !infoObj[INFO_KEY]) {
throw new Error('Potential attribute conflict detected');
}
}
}
/**
* Adds a key to this info object.
*
* @param {?} key
* @return {?}
*/
ContainerInfo.prototype.addAttribute = function (key) {
var /** @type {?} */ item = this._getInfo();
item[key] = true;
this._setInfo(item);
};
/**
* Removes a key from this info object.
*
* @param {?} key
* @return {?}
*/
ContainerInfo.prototype.removeAttribute = function (key) {
var /** @type {?} */ info = this._getInfo();
delete info[key];
this._setInfo(info);
};
/**
* Returns a list of keys that have been added to this
* info object.
*
* @return {?}
*/
ContainerInfo.prototype.getAttributes = function () {
return Object.keys(this._getInfo())
.filter(function (key) { return key !== INFO_KEY; });
};
/**
* Checks to see if the value stored in the _namespace
* is an info object or if it is empty. If it is NOT
* an info object, then false is returned.
*
* @return {?}
*/
ContainerInfo.prototype.available = function () {
var /** @type {?} */ infoObj = this._container.get(this._namespace);
return !infoObj || (typeof infoObj === 'object' && infoObj[INFO_KEY]);
};
/**
* @return {?}
*/
ContainerInfo.prototype._getInfo = function () {
var /** @type {?} */ obj = this._container.get(this._namespace);
if (!obj) {
obj = {};
obj[INFO_KEY] = true;
}
return obj;
};
/**
* @param {?} info
* @return {?}
*/
ContainerInfo.prototype._setInfo = function (info) {
// this is 1 because the info identifier will be there.
if (Object.keys(info).length <= 1) {
this._container.remove(this._namespace);
}
else {
this._container.set(this._namespace, info);
}
};
return ContainerInfo;
}());
function ContainerInfo_tsickle_Closure_declarations() {
/** @type {?} */
ContainerInfo.prototype._namespace;
/** @type {?} */
ContainerInfo.prototype._container;
}
//# sourceMappingURL=storage.container_info.js.map