Created
July 17, 2019 02:34
-
-
Save JustenRickert/2798e37d32f204dc713f054a6f4fd859 to your computer and use it in GitHub Desktop.
This file contains 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
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