index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; }
  3. var id = 0;
  4. function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; }
  5. const packageJson = {
  6. "version": "2.1.1"
  7. };
  8. /**
  9. * Default store that keeps state in a simple object.
  10. */
  11. var _publish = /*#__PURE__*/_classPrivateFieldLooseKey("publish");
  12. class DefaultStore {
  13. constructor() {
  14. Object.defineProperty(this, _publish, {
  15. value: _publish2
  16. });
  17. this.state = {};
  18. this.callbacks = []; // TODO: use a Set instead, make it a private prop
  19. }
  20. getState() {
  21. return this.state;
  22. }
  23. setState(patch) {
  24. const prevState = { ...this.state
  25. };
  26. const nextState = { ...this.state,
  27. ...patch
  28. };
  29. this.state = nextState;
  30. _classPrivateFieldLooseBase(this, _publish)[_publish](prevState, nextState, patch);
  31. }
  32. subscribe(listener) {
  33. this.callbacks.push(listener);
  34. return () => {
  35. // Remove the listener.
  36. this.callbacks.splice(this.callbacks.indexOf(listener), 1);
  37. };
  38. }
  39. } // TODO: export the class instead in the next major.
  40. function _publish2() {
  41. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  42. args[_key] = arguments[_key];
  43. }
  44. this.callbacks.forEach(listener => {
  45. listener(...args);
  46. });
  47. }
  48. DefaultStore.VERSION = packageJson.version;
  49. function defaultStore() {
  50. return new DefaultStore();
  51. }
  52. module.exports = defaultStore;