Skip to content

Instantly share code, notes, and snippets.

@rostero1
Created September 20, 2019 19:50
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 rostero1/94b86bb1b154ae4b2211f2e00e4b0e0a to your computer and use it in GitHub Desktop.
Save rostero1/94b86bb1b154ae4b2211f2e00e4b0e0a to your computer and use it in GitHub Desktop.
function createMachineEpic(machine, _, updateStoreActionFn) {
return function epic(action$) {
const service = interpret(machine);
const state$ = fromEventPattern(
handler => {
service.onTransition(handler).start();
return service;
},
(_, service) => service.stop()
).pipe(map(([state, action]) => updateStoreActionFn(state, action)));
return merge(
state$,
action$.pipe(
filter(({ type }) => machine.handles(type)),
tap(action => {
service.send(action);
}),
ignoreElements()
)
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment