You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
"use strict";
|
|
|
|
function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; }
|
|
|
|
var id = 0;
|
|
|
|
function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; }
|
|
|
|
const packageJson = {
|
|
"version": "2.1.1"
|
|
};
|
|
/**
|
|
* Default store that keeps state in a simple object.
|
|
*/
|
|
|
|
var _publish = /*#__PURE__*/_classPrivateFieldLooseKey("publish");
|
|
|
|
class DefaultStore {
|
|
constructor() {
|
|
Object.defineProperty(this, _publish, {
|
|
value: _publish2
|
|
});
|
|
this.state = {};
|
|
this.callbacks = []; // TODO: use a Set instead, make it a private prop
|
|
}
|
|
|
|
getState() {
|
|
return this.state;
|
|
}
|
|
|
|
setState(patch) {
|
|
const prevState = { ...this.state
|
|
};
|
|
const nextState = { ...this.state,
|
|
...patch
|
|
};
|
|
this.state = nextState;
|
|
|
|
_classPrivateFieldLooseBase(this, _publish)[_publish](prevState, nextState, patch);
|
|
}
|
|
|
|
subscribe(listener) {
|
|
this.callbacks.push(listener);
|
|
return () => {
|
|
// Remove the listener.
|
|
this.callbacks.splice(this.callbacks.indexOf(listener), 1);
|
|
};
|
|
}
|
|
|
|
} // TODO: export the class instead in the next major.
|
|
|
|
|
|
function _publish2() {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
this.callbacks.forEach(listener => {
|
|
listener(...args);
|
|
});
|
|
}
|
|
|
|
DefaultStore.VERSION = packageJson.version;
|
|
|
|
function defaultStore() {
|
|
return new DefaultStore();
|
|
}
|
|
|
|
module.exports = defaultStore;
|