@ssh-uppy/store-default
Version:
The default simple object-based store for Uppy.
55 lines (41 loc) • 1.54 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
* Default store that keeps state in a simple object.
*/
var DefaultStore = /*#__PURE__*/function () {
function DefaultStore() {
this.state = {};
this.callbacks = [];
}
var _proto = DefaultStore.prototype;
_proto.getState = function getState() {
return this.state;
};
_proto.setState = function setState(patch) {
var prevState = _extends({}, this.state);
var nextState = _extends({}, this.state, patch);
this.state = nextState;
this._publish(prevState, nextState, patch);
};
_proto.subscribe = function subscribe(listener) {
var _this = this;
this.callbacks.push(listener);
return function () {
// Remove the listener.
_this.callbacks.splice(_this.callbacks.indexOf(listener), 1);
};
};
_proto._publish = function _publish() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
this.callbacks.forEach(function (listener) {
listener.apply(void 0, args);
});
};
return DefaultStore;
}();
DefaultStore.VERSION = require('../package.json').version;
module.exports = function defaultStore() {
return new DefaultStore();
};