UNPKG

3.16 kBJavaScriptView Raw
1var _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
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5var cuid = require('cuid');
6
7// Redux action name.
8var STATE_UPDATE = 'uppy/STATE_UPDATE';
9
10// Pluck Uppy state from the Redux store in the default location.
11var defaultSelector = function defaultSelector(id) {
12 return function (state) {
13 return state.uppy[id];
14 };
15};
16
17/**
18 * Redux store.
19 *
20 * @param {object} opts.store - The Redux store to use.
21 * @param {string} opts.id - This store instance's ID. Defaults to a random string.
22 * If you need to access Uppy state through Redux, eg. to render custom UI, set this to something constant.
23 * @param {function} opts.selector - Function, `(state) => uppyState`, to pluck state from the Redux store.
24 * Defaults to retrieving `state.uppy[opts.id]`. Override if you placed Uppy state elsewhere in the Redux store.
25 */
26
27var 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 // Initialise the `uppy[id]` state key.
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
68function 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
77function 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
90function middleware() {
91 // Do nothing, at the moment.
92 return function () {
93 return function (next) {
94 return function (action) {
95 next(action);
96 };
97 };
98 };
99}
100
101module.exports = function createReduxStore(opts) {
102 return new ReduxStore(opts);
103};
104
105module.exports.STATE_UPDATE = STATE_UPDATE;
106module.exports.reducer = reducer;
107module.exports.middleware = middleware;
\No newline at end of file