Skip to content

Instantly share code, notes, and snippets.

@lmatteis
Created December 3, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmatteis/959128492e02b0c54ab97ea2d4fd12f0 to your computer and use it in GitHub Desktop.
Save lmatteis/959128492e02b0c54ab97ea2d4fd12f0 to your computer and use it in GitHub Desktop.
createBehavioralMiddleware
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