This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/src/store/index.js
2017-09-16 23:17:42 +09:00

51 lines
1 KiB
JavaScript

class Store {
constructor(reducer, catcher) {
this.reducer = reducer;
this.catcher = catcher;
this.subscribers = [];
try {
this.state = this.reducer(undefined, {});
} catch (e) {
catcher(e);
}
}
dispatch(action, sender) {
if (action instanceof Promise) {
action.then((a) => {
this.transitNext(a, sender);
}).catch((e) => {
this.catcher(e, sender);
});
} else {
try {
this.transitNext(action, sender);
} catch (e) {
this.catcher(e, sender);
}
}
return action
}
getState() {
return this.state;
}
subscribe(callback) {
this.subscribers.push(callback);
}
transitNext(action, sender) {
let newState = this.reducer(this.state, action);
if (JSON.stringify(this.state) !== JSON.stringify(newState)) {
this.state = newState;
this.subscribers.forEach(f => f(sender));
}
}
}
const empty = () => {};
export function createStore(reducer, catcher = empty) {
return new Store(reducer, catcher);
}