|
@@ -0,0 +1,58 @@
|
|
|
+"use strict";
|
|
|
+
|
|
|
+import EventEmitter from 'event-emitter';
|
|
|
+import {createStore, compose, applyMiddleware} from 'redux';
|
|
|
+import {composeReducers} from 'redux-sac';
|
|
|
+import {effectsMiddleware} from 'redux-effex';
|
|
|
+import {persistStore, autoRehydrate} from 'redux-persist';
|
|
|
+import {REHYDRATE} from 'redux-persist/constants';
|
|
|
+import createActionBuffer from 'redux-action-buffer';
|
|
|
+
|
|
|
+export default () => {
|
|
|
+ const ee = new EventEmitter();
|
|
|
+
|
|
|
+ function on (event, handler) {
|
|
|
+ ee.on(event, handler);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ let persistOptions = {};
|
|
|
+
|
|
|
+ function withPersistence (_persistOptions) {
|
|
|
+ persistOptions = _persistOptions;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ let reducers = [], effects = [];
|
|
|
+
|
|
|
+ function withReducers (..._reducers) {
|
|
|
+ reducers.push(..._reducers);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ function withEffects (..._effects) {
|
|
|
+ effects.push(..._effects);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ function create (reset = false) {
|
|
|
+ const store = createStore(
|
|
|
+ composeReducers(...reducers),
|
|
|
+ compose(
|
|
|
+ autoRehydrate(),
|
|
|
+ applyMiddleware(createActionBuffer(REHYDRATE), effectsMiddleware(effects))
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ const persistor = persistStore(store, persistOptions, err => {
|
|
|
+ if (err) ee.emit('error', err);
|
|
|
+ else ee.emit('rehydrate', store);
|
|
|
+ });
|
|
|
+
|
|
|
+ if (reset) persistor.purge();
|
|
|
+
|
|
|
+ return store;
|
|
|
+ }
|
|
|
+
|
|
|
+ return {withReducers, withEffects, withPersistence, on, create};
|
|
|
+}
|