add simple store
This commit is contained in:
parent
b2cddcd69b
commit
0ae39f1b67
1 changed files with 27 additions and 0 deletions
27
src/store/index.js
Normal file
27
src/store/index.js
Normal file
|
@ -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);
|
||||
}
|
Reference in a new issue