Skip to content

Instantly share code, notes, and snippets.

@goatslacker
Last active June 20, 2017 03:51
Show Gist options
  • Save goatslacker/e21539784342c63f22c030ee1e600cac to your computer and use it in GitHub Desktop.
Save goatslacker/e21539784342c63f22c030ee1e600cac to your computer and use it in GitHub Desktop.

Revisions

  1. goatslacker revised this gist Jun 20, 2017. 1 changed file with 10 additions and 25 deletions.
    35 changes: 10 additions & 25 deletions injectableMiddleware.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,19 @@
    function createInjectableStore(reducers, preloadedState, enhancer) {
    const scopedMiddleware = []
    import { createStore, compose, applyMiddleware } from 'redux'

    export default function createInjectableStore(reducers, preloadedState, enhancer) {
    const injectedMiddleware = []

    const injectableMiddleware = store => next => action => {
    const chain = scopedMiddleware.map(m => m(store))
    const injectedMiddleware = chain.reduceRight((next, f) => {
    const chain = injectedMiddleware.map(m => m(store))
    const dispatchThroughMiddleware = chain.reduceRight((next, f) => {
    return f(next)
    }, action => next(action))

    return injectedMiddleware(action)
    return dispatchThroughMiddleware(action)
    }

    function injectMiddleware(actionTypes, middleware) {
    // TODO make sure that you don't try and inject multiple middleware
    // type check the fuck out of actionTypes
    scopedMiddleware.push(store => next => action => {
    if (!actionTypes.hasOwnProperty(action.type)) {
    console.error(
    'Action type lookup failed',
    action.type,
    'for',
    middleware.name
    )
    return next(action)
    } else {
    console.log(
    'action lookup successful for', middleware.name, action.type
    )
    }
    function injectMiddleware(middleware) {
    injectedMiddleware.push(store => next => action => {
    return middleware(store)(next)(action)
    })
    }
    @@ -40,6 +27,4 @@ function createInjectableStore(reducers, preloadedState, enhancer) {
    return Object.assign(store, {
    injectMiddleware,
    })
    }

    exports.createInjectableStore = createInjectableStore;
    }
  2. goatslacker created this gist Jun 17, 2017.
    45 changes: 45 additions & 0 deletions injectableMiddleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    function createInjectableStore(reducers, preloadedState, enhancer) {
    const scopedMiddleware = []

    const injectableMiddleware = store => next => action => {
    const chain = scopedMiddleware.map(m => m(store))
    const injectedMiddleware = chain.reduceRight((next, f) => {
    return f(next)
    }, action => next(action))

    return injectedMiddleware(action)
    }

    function injectMiddleware(actionTypes, middleware) {
    // TODO make sure that you don't try and inject multiple middleware
    // type check the fuck out of actionTypes
    scopedMiddleware.push(store => next => action => {
    if (!actionTypes.hasOwnProperty(action.type)) {
    console.error(
    'Action type lookup failed',
    action.type,
    'for',
    middleware.name
    )
    return next(action)
    } else {
    console.log(
    'action lookup successful for', middleware.name, action.type
    )
    }
    return middleware(store)(next)(action)
    })
    }

    const store = createStore(
    reducers,
    preloadedState,
    compose(enhancer, applyMiddleware(injectableMiddleware))
    )

    return Object.assign(store, {
    injectMiddleware,
    })
    }

    exports.createInjectableStore = createInjectableStore;