Skip to content

Instantly share code, notes, and snippets.

@hueralin
Forked from gaearon/slim-redux.js
Created August 12, 2019 08:37
Show Gist options
  • Save hueralin/e73e1c9c3247140a9b108782ac64e0b4 to your computer and use it in GitHub Desktop.
Save hueralin/e73e1c9c3247140a9b108782ac64e0b4 to your computer and use it in GitHub Desktop.

Revisions

  1. @gaearon gaearon revised this gist Sep 22, 2015. 1 changed file with 7 additions and 22 deletions.
    29 changes: 7 additions & 22 deletions slim-redux.js
    Original file line number Diff line number Diff line change
    @@ -36,31 +36,21 @@ export function applyMiddleware(...middlewares) {
    var dispatch = store.dispatch;
    var chain = [];

    var middlewareAPI = {
    chain = middlewares.map(middleware => middleware({
    getState: store.getState,
    dispatch: (action) => dispatch(action)
    };
    chain = middlewares.map(middleware => middleware(middlewareAPI));
    }));
    dispatch = compose(...chain)(store.dispatch);

    return {
    ...store,
    dispatch
    };
    return { ...store, dispatch };
    };
    }

    export function combineReducers(reducers) {
    var finalReducers = pick(reducers, (val) => typeof val === 'function');
    var defaultState = mapValues(finalReducers, () => undefined);

    return function combination(state = defaultState, action) {
    var finalState = mapValues(finalReducers, (reducer, key) => {
    return reducer(state[key], action);
    });

    return finalState;
    };
    return (state = {}, action) => mapValues(finalReducers,
    (reducer, key) => reducer(state[key], action)
    );
    }

    export function createStore(reducer, initialState) {
    @@ -105,10 +95,5 @@ export function createStore(reducer, initialState) {

    dispatch({ type: '@@redux/INIT' });

    return {
    dispatch,
    subscribe,
    getState,
    replaceReducer
    };
    return { dispatch, subscribe, getState, replaceReducer };
    }
  2. @gaearon gaearon created this gist Sep 22, 2015.
    114 changes: 114 additions & 0 deletions slim-redux.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    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;
    }, {});
    }

    function bindActionCreator(actionCreator, dispatch) {
    return (...args) => dispatch(actionCreator(...args));
    }

    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);
    }

    export function applyMiddleware(...middlewares) {
    return (next) => (reducer, initialState) => {
    var store = next(reducer, initialState);
    var dispatch = store.dispatch;
    var chain = [];

    var middlewareAPI = {
    getState: store.getState,
    dispatch: (action) => dispatch(action)
    };
    chain = middlewares.map(middleware => middleware(middlewareAPI));
    dispatch = compose(...chain)(store.dispatch);

    return {
    ...store,
    dispatch
    };
    };
    }

    export function combineReducers(reducers) {
    var finalReducers = pick(reducers, (val) => typeof val === 'function');
    var defaultState = mapValues(finalReducers, () => undefined);

    return function combination(state = defaultState, action) {
    var finalState = mapValues(finalReducers, (reducer, key) => {
    return reducer(state[key], action);
    });

    return finalState;
    };
    }

    export function createStore(reducer, initialState) {
    var currentReducer = reducer;
    var currentState = initialState;
    var listeners = [];
    var isDispatching = false;

    function getState() {
    return currentState;
    }

    function subscribe(listener) {
    listeners.push(listener);

    return function unsubscribe() {
    var index = listeners.indexOf(listener);
    listeners.splice(index, 1);
    };
    }

    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
    };
    }