UNPKG

1.51 kBJavaScriptView Raw
1function _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); }
2
3/**
4 * Default store that keeps state in a simple object.
5 */
6var DefaultStore =
7/*#__PURE__*/
8function () {
9 function DefaultStore() {
10 this.state = {};
11 this.callbacks = [];
12 }
13
14 var _proto = DefaultStore.prototype;
15
16 _proto.getState = function getState() {
17 return this.state;
18 };
19
20 _proto.setState = function setState(patch) {
21 var prevState = _extends({}, this.state);
22
23 var nextState = _extends({}, this.state, patch);
24
25 this.state = nextState;
26
27 this._publish(prevState, nextState, patch);
28 };
29
30 _proto.subscribe = function subscribe(listener) {
31 var _this = this;
32
33 this.callbacks.push(listener);
34 return function () {
35 // Remove the listener.
36 _this.callbacks.splice(_this.callbacks.indexOf(listener), 1);
37 };
38 };
39
40 _proto._publish = function _publish() {
41 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
42 args[_key] = arguments[_key];
43 }
44
45 this.callbacks.forEach(function (listener) {
46 listener.apply(void 0, args);
47 });
48 };
49
50 return DefaultStore;
51}();
52
53DefaultStore.VERSION = "1.2.0";
54
55module.exports = function defaultStore() {
56 return new DefaultStore();
57};
\No newline at end of file