See you my redux
This commit is contained in:
parent
f2ac75fff8
commit
96649fef63
3 changed files with 0 additions and 178 deletions
|
@ -1,53 +0,0 @@
|
|||
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 = () => {};
|
||||
|
||||
const createStore = (reducer, catcher = empty) => {
|
||||
return new Store(reducer, catcher);
|
||||
};
|
||||
|
||||
export { createStore };
|
|
@ -1,15 +0,0 @@
|
|||
import { h, Component } from 'preact';
|
||||
|
||||
class Provider extends Component {
|
||||
getChildContext() {
|
||||
return { store: this.props.store };
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div>
|
||||
{ this.props.children }
|
||||
</div>;
|
||||
}
|
||||
}
|
||||
|
||||
export default Provider;
|
Reference in a new issue