Skip to content

Instantly share code, notes, and snippets.

@JustenRickert
Created July 17, 2019 02:34
Show Gist options
  • Save JustenRickert/2798e37d32f204dc713f054a6f4fd859 to your computer and use it in GitHub Desktop.
Save JustenRickert/2798e37d32f204dc713f054a6f4fd859 to your computer and use it in GitHub Desktop.
class WorkRunner {
constructor(fork, { actionCreator, messageCreator }) {
this.fork = fork;
this.actionCreator = actionCreator;
this.messageCreator = messageCreator;
this.fork.on("message", this.actionCreator);
}
send(msg) {
fork.send(this.messageCreator(msg));
}
}
const myOtherWorker = new WorkRunner(fork('./my-other-script.js'), {
messageCreator: sentMessage => {
const { someInforamtionICantPassToSendMethod } = store.getState()
return { sentMessage, someInformationICantPassToSendMethod }
},
actionCreator: returnedMessage => {
store.dispatch({ type: MY_OTHER_SCRIPT, returnedMessage })
}
})
const myWorker = new WorkRunner(fork("./my-script.js"), {
messageCreator: sentMessage => ({ sentMessage }),
actionCreator: returnedMessage => {
store.dispatch({ type: MY_SCRIPT_ACTION, returnedMessage });
const { someInformationReducedAWhileAgo } = store.getState()
myOtherWorker.send('RUN THIS IN RESPONSE ' + someInformationReducedAWhileAgo)
}
});
myWorker.send("RUN");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment