Browse Source

First implementation.

Herbert Vojčík 6 years ago
parent
commit
68b4acb7e8
2 changed files with 67 additions and 1 deletions
  1. 58 0
      index.js
  2. 9 1
      package.json

+ 58 - 0
index.js

@@ -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};
+}

+ 9 - 1
package.json

@@ -17,5 +17,13 @@
     "combine"
   ],
   "author": "Herbert Vojčík <herby@mailbox.sk>",
-  "license": "MIT"
+  "license": "MIT",
+  "dependencies": {
+    "event-emitter": ">=0.3.5",
+    "redux": "^3.7.2",
+    "redux-action-buffer": "^1.1.0",
+    "redux-effex": "^1.1.2",
+    "redux-persist": "^4.10.1",
+    "redux-sac": ">=0.5.3"
+  }
 }