fv-store
Version:
Fast and simple key = value storage
134 lines (102 loc) • 4.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _Cache = require("./Cache");
var _Cache2 = _interopRequireDefault(_Cache);
var _Query = require("./Query");
var _Query2 = _interopRequireDefault(_Query);
var _Subscription = require("./Subscription");
var _Subscription2 = _interopRequireDefault(_Subscription);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var STATE = {};
var Store = function () {
function Store() {
_classCallCheck(this, Store);
}
_createClass(Store, null, [{
key: "subscribe",
/**
* Listen to the given subscription and call the given func.
*
* @param {String} subscription
* @param {Function} func
* @returns {{key: String, index: Number}}
*/
value: function subscribe(subscription, func) {
_Subscription2.default.subscribe(subscription, func);
}
/**
* Unsubscribe from the given subscription.
*
* @param {String} key
* @param {Number} index
*/
}, {
key: "unSubscribe",
value: function unSubscribe(_ref) {
var key = _ref.key,
index = _ref.index;
_Subscription2.default.unSubscribe({ key: key, index: index });
}
/**
* Set the state with the given state object.
*
* @param {Object} state
*/
}, {
key: "set",
value: function set() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var stateCopy = JSON.parse(JSON.stringify(STATE));
Object.assign(stateCopy, state);
Object.assign(STATE, stateCopy);
_Cache2.default.update(state);
_Subscription2.default.emit('update', STATE);
}
/**
* Get the state value from the given query. If no results are returned
* from the query then the defaultValue will be returned.
*
* @param {String} query
* @param {*=} defaultValue
*/
}, {
key: "get",
value: function get(query) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (query.indexOf('*') >= 0) {
return _Query2.default.match(query, defaultValue);
}
if (_Cache2.default.has(query)) {
return _Cache2.default.get(query);
}
var keys = String(query).split('.');
var state = STATE;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!state.hasOwnProperty(key)) {
return defaultValue;
}
state = state[key];
}
_Cache2.default.set(query, state);
return state;
}
/**
* Unset the given query from the cache.
*
* @param {String} query
*/
}, {
key: "unset",
value: function unset(query) {
_Cache2.default.unset(query);
_Subscription2.default.emit('update', STATE);
}
}]);
return Store;
}();
exports.default = Store;