-
-
Save hailelagi/4b1baa0e2e0909e2df391f552f8d77d7 to your computer and use it in GitHub Desktop.
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Helper fns */ | |
function mapValues(obj, fn) { | |
return Object.keys(obj).reduce((result, key) => { | |
result[key] = fn(obj[key], key); | |
return result; | |
}, {}); | |
} | |
function pick(obj, fn) { | |
return Object.keys(obj).reduce((result, key) => { | |
if (fn(obj[key])) { | |
result[key] = obj[key]; | |
} | |
return result; | |
}, {}); | |
} | |
/*creates an action to be dispatched to the store*/ | |
function bindActionCreator(actionCreator, dispatch) { | |
return (...args) => dispatch(actionCreator(...args)); | |
} | |
/*CORE API */ | |
/*What the total fuck? */ | |
export function bindActionCreators(actionCreators, dispatch) { | |
return typeof actionCreators === 'function' ? | |
bindActionCreator(actionCreators, dispatch) : | |
mapValues(actionCreators, actionCreator => | |
bindActionCreator(actionCreator, dispatch) | |
); | |
} | |
export function compose(...funcs) { | |
return arg => funcs.reduceRight((composed, f) => f(composed), arg); | |
} | |
/*how does this handle async??? */ | |
export function applyMiddleware(...middlewares) { | |
return (next) => (reducer, initialState) => { | |
var store = next(reducer, initialState); | |
var dispatch = store.dispatch; | |
var chain = []; | |
chain = middlewares.map(middleware => middleware({ | |
getState: store.getState, | |
dispatch: (action) => dispatch(action) | |
})); | |
dispatch = compose(...chain)(store.dispatch); | |
return { ...store, dispatch }; | |
}; | |
} | |
/*A reducer of actions*/ | |
export function combineReducers(reducers) { | |
var finalReducers = pick(reducers, (val) => typeof val === 'function'); | |
return (state = {}, action) => mapValues(finalReducers, | |
(reducer, key) => reducer(state[key], action) | |
); | |
} | |
/*The ACTUAL redux store */ | |
/* createStore is the prototype object. getState etc are "methods" of this object*/ | |
export function createStore(reducer, initialState) { | |
var currentReducer = reducer; | |
var currentState = initialState; | |
var listeners = []; | |
var isDispatching = false; | |
function getState() { | |
return currentState; | |
} | |
/*what are listeners? */ | |
function subscribe(listener) { | |
listeners.push(listener); | |
return function unsubscribe() { | |
var index = listeners.indexOf(listener); | |
listeners.splice(index, 1); | |
}; | |
} | |
/*why is dispatch initiated here? */ | |
/*where is isDispatching coming from? */ | |
function dispatch(action) { | |
if (isDispatching) { | |
throw new Error('Reducers may not dispatch actions.'); | |
} | |
try { | |
isDispatching = true; | |
currentState = currentReducer(currentState, action); | |
} finally { | |
isDispatching = false; | |
} | |
listeners.slice().forEach(listener => listener()); | |
return action; | |
} | |
function replaceReducer(nextReducer) { | |
currentReducer = nextReducer; | |
dispatch({ type: '@@redux/INIT' }); | |
} | |
dispatch({ type: '@@redux/INIT' }); | |
return { dispatch, subscribe, getState, replaceReducer }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment