const createStore = function (reducer, initState) {
let state = initState;
let listeners = [];
function subscribe(listener) {
listeners.push(listener);
}
function dispatch(action) {
const currentState = reducer(state, action);
state = currentState;
listeners.forEach((handler) => handler());
}
function getState() {
return state;
}
return { subscribe, dispatch, getState };
};
const combineReducers = (reducers) => {
const keys = Object.keys(reducers);
return function (state = {}, action) {
const newState = {};
keys.forEach((key) => {
const reducer = reducers[key];
const next = reducer(state[key], action);
newState[key] = next;
});
return newState;
};
};