UNPKG

redux

Version:

Atomic Flux with hot reloading

78 lines (56 loc) 2.26 kB
'use strict'; exports.__esModule = true; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _createDispatcher = require('./createDispatcher'); var _createDispatcher2 = _interopRequireDefault(_createDispatcher); var _utilsComposeStores = require('./utils/composeStores'); var _utilsComposeStores2 = _interopRequireDefault(_utilsComposeStores); var _middlewareThunk = require('./middleware/thunk'); var _middlewareThunk2 = _interopRequireDefault(_middlewareThunk); var Redux = (function () { function Redux(dispatcher, initialState) { _classCallCheck(this, Redux); var finalDispatcher = dispatcher; if (typeof dispatcher === 'object') { // A shortcut notation to use the default dispatcher finalDispatcher = (0, _createDispatcher2['default'])((0, _utilsComposeStores2['default'])(dispatcher), function (getState) { return [(0, _middlewareThunk2['default'])(getState)]; }); } this.state = initialState; this.listeners = []; this.replaceDispatcher(finalDispatcher); } Redux.prototype.getDispatcher = function getDispatcher() { return this.dispatcher; }; Redux.prototype.replaceDispatcher = function replaceDispatcher(nextDispatcher) { this.dispatcher = nextDispatcher; this.dispatchFn = nextDispatcher(this.state, this.setState.bind(this)); }; Redux.prototype.dispatch = function dispatch(action) { return this.dispatchFn(action); }; Redux.prototype.getState = function getState() { return this.state; }; Redux.prototype.setState = function setState(nextState) { this.state = nextState; this.listeners.forEach(function (listener) { return listener(); }); return nextState; }; Redux.prototype.subscribe = function subscribe(listener) { var listeners = this.listeners; listeners.push(listener); return function unsubscribe() { var index = listeners.indexOf(listener); listeners.splice(index, 1); }; }; return Redux; })(); exports['default'] = Redux; module.exports = exports['default'];