Skip to content

Instantly share code, notes, and snippets.

@jonnyreeves
Last active December 18, 2015 07:09
Show Gist options
  • Save jonnyreeves/5744277 to your computer and use it in GitHub Desktop.
Save jonnyreeves/5744277 to your computer and use it in GitHub Desktop.
Message Passing
// Register a message handler for "some message"
messageBus.addHandler("some_message", function (message, next) {
// do something with the data in an async fashion
$.get(message.url, function () {
if (response.code !== 200) {
// Transition into an error state, terminates processing.
next(new Error("bad http request!"));
}
else {
// Modify the state of the object and pass execution to the next handler
message.someState = response;
}
});
});
// Trigger a message...
messageBus.dispatch("some_message", function (err, message) {
// Callback is invoked after all handlers have triggered.
// error will be null if no error was raised.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment