aboutsummaryrefslogtreecommitdiff
path: root/src/store
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-09-13 21:11:01 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-09-13 21:16:21 +0900
commit0ae39f1b67e269216ce3d45b870e448f6dbf21d7 (patch)
tree9f8fba389b0fd490cab0dcf0ccfdffb0b9bdee2d /src/store
parentb2cddcd69b4ae06770d66808624fc43f3dcbcb0e (diff)
add simple store
Diffstat (limited to 'src/store')
-rw-r--r--src/store/index.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/store/index.js b/src/store/index.js
new file mode 100644
index 0000000..bcb65d6
--- /dev/null
+++ b/src/store/index.js
@@ -0,0 +1,27 @@
+class Store {
+ constructor(reducer, catcher) {
+ this.reducer = reducer;
+ this.catcher = catcher;
+ this.state = this.reducer(undefined, {});
+ }
+
+ dispatch(action) {
+ if (action instanceof Promise) {
+ action.then((a) => {
+ this.state = this.reducer(this.state, a);
+ }).catch(this.catcher)
+ } else {
+ try {
+ this.state = this.reducer(this.state, action);
+ } catch (e) {
+ this.catcher(e);
+ }
+ }
+ }
+}
+
+const empty = () => {};
+
+export function createStore(reducer, catcher = empty) {
+ return new Store(reducer, catcher);
+}