class createStore {
private state = {}
reducer = {}
listeners = []
constructor(reducer, state) {
this.reducer = reducer
this.state = state
}
dispatch(action) {
this.state = this.reducer(this.state, action)
this.listeners.forEach(listener => listener())
}
subscribe(listener) {
this.listeners.push(listener)
return () => {
this.listeners = this.listeners.filter(l => l !== listener)
}
}
getState() {
return this.state
}
}
let defaultState = {
t1:1
}
let reducer1 = function ((state = defaultState, action)){
const _currentState = {
ADD_CHAT: () => {
state.mm = 20;
state.d2 = Object.assign({}, action.data);
return state;
}
};
return _currentState[action.type] && _currentState[action.type]();
}
let store1 = new createStore(reducer1)
store1.subscribe(function(){
console.log('subscribe')
})
store1.dispatch({
type: 'ADD_CHAT',
data: {
a: 2
}
})
console