1 | var _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; };
|
2 |
|
3 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
4 |
|
5 | var cuid = require('cuid');
|
6 |
|
7 |
|
8 | var STATE_UPDATE = 'uppy/STATE_UPDATE';
|
9 |
|
10 |
|
11 | var defaultSelector = function defaultSelector(id) {
|
12 | return function (state) {
|
13 | return state.uppy[id];
|
14 | };
|
15 | };
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var ReduxStore = function () {
|
28 | function ReduxStore(opts) {
|
29 | _classCallCheck(this, ReduxStore);
|
30 |
|
31 | this._store = opts.store;
|
32 | this._id = opts.id || cuid();
|
33 | this._selector = opts.selector || defaultSelector(this._id);
|
34 |
|
35 |
|
36 | this.setState({});
|
37 | }
|
38 |
|
39 | ReduxStore.prototype.setState = function setState(patch) {
|
40 | this._store.dispatch({
|
41 | type: STATE_UPDATE,
|
42 | id: this._id,
|
43 | payload: patch
|
44 | });
|
45 | };
|
46 |
|
47 | ReduxStore.prototype.getState = function getState() {
|
48 | return this._selector(this._store.getState());
|
49 | };
|
50 |
|
51 | ReduxStore.prototype.subscribe = function subscribe(cb) {
|
52 | var _this = this;
|
53 |
|
54 | var prevState = this.getState();
|
55 | return this._store.subscribe(function () {
|
56 | var nextState = _this.getState();
|
57 | if (prevState !== nextState) {
|
58 | var patch = getPatch(prevState, nextState);
|
59 | cb(prevState, nextState, patch);
|
60 | prevState = nextState;
|
61 | }
|
62 | });
|
63 | };
|
64 |
|
65 | return ReduxStore;
|
66 | }();
|
67 |
|
68 | function getPatch(prev, next) {
|
69 | var nextKeys = Object.keys(next);
|
70 | var patch = {};
|
71 | nextKeys.forEach(function (k) {
|
72 | if (prev[k] !== next[k]) patch[k] = next[k];
|
73 | });
|
74 | return patch;
|
75 | }
|
76 |
|
77 | function reducer() {
|
78 | var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
79 | var action = arguments[1];
|
80 |
|
81 | if (action.type === STATE_UPDATE) {
|
82 | var _extends2;
|
83 |
|
84 | var newState = _extends({}, state[action.id], action.payload);
|
85 | return _extends({}, state, (_extends2 = {}, _extends2[action.id] = newState, _extends2));
|
86 | }
|
87 | return state;
|
88 | }
|
89 |
|
90 | function middleware() {
|
91 |
|
92 | return function () {
|
93 | return function (next) {
|
94 | return function (action) {
|
95 | next(action);
|
96 | };
|
97 | };
|
98 | };
|
99 | }
|
100 |
|
101 | module.exports = function createReduxStore(opts) {
|
102 | return new ReduxStore(opts);
|
103 | };
|
104 |
|
105 | module.exports.STATE_UPDATE = STATE_UPDATE;
|
106 | module.exports.reducer = reducer;
|
107 | module.exports.middleware = middleware; |
\ | No newline at end of file |