Created
December 3, 2018 15:28
-
-
Save lmatteis/959128492e02b0c54ab97ea2d4fd12f0 to your computer and use it in GitHub Desktop.
createBehavioralMiddleware
This file contains hidden or 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
import BProgram from "./bp.js"; | |
function createBehavioralMiddleware(threads) { | |
return ({ dispatch, getState }) => { | |
const bp = new BProgram(); | |
let pr = 1; | |
bp.run(); | |
threads.forEach(thread => bp.addBThread(``, pr++, thread)); | |
bp.addBThread("dispatch", pr++, function*() { | |
while (true) { | |
yield { | |
wait: [ | |
(event, payload) => { | |
console.log("dispatching", { | |
type: event, | |
payload, | |
bpThread: true | |
}); | |
dispatch({ type: event, payload, bpThread: true }); | |
return true; | |
} | |
] | |
}; | |
} | |
}); | |
return next => action => { | |
// if it's a BP event continue reducers, otherwise return null | |
// we only can run to the reducers and other middlewars | |
// stuff coming "out" of the b-threads. | |
if (action.bpThread) { | |
return next(action); | |
} else { | |
bp.event(action.type, action.payload); | |
return null; | |
} | |
}; | |
}; | |
} | |
export default createBehavioralMiddleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment