Skip to content

Instantly share code, notes, and snippets.

@Narigo
Created March 11, 2013 15:46
Show Gist options
  • Save Narigo/5135168 to your computer and use it in GitHub Desktop.
Save Narigo/5135168 to your computer and use it in GitHub Desktop.
vertx event bus addition
eventBus.replaceData("address", "read-action", "replace-action", {
"id" : 15
},
// action on replace computation
function (readData) {
var replacedData = readData;
replacedData.nickname = replacedData.nickname.toLowerCase();
return replacedData;
},
// action on success
function () {
console.log('success!');
},
// action on error (data not foundm, module doesn't support these actions, ...)
function (error) {
console.log('fail: ' + error);
})
eventBus = {
// new eventbus function 'replace'
replaceData : function replaceData(address, readAction, replaceAction, toRead, replaceComputation, success, failure) {
function callbackForData(data) {
eventBus.send(address, { action: readAction, data: readData }, function(readReply) {
if (readReply.status === 'ok') {
var replacedData = replaceComputation(readReply.data);
eventBus.replace(address, { action: replaceAction, oldData: data, newData: replacedData }, function(replaceReply) {
if (replaceReply.status === 'ok') {
// successfully replaced!
onSuccess();
} else if (replaceReply.status === 'replace-error') {
// try again, some concurrent action changed it
callbackForData(data);
} else {
// some other error occurred, tell error callback
onFailure(replaceReply.error);
}
});
} else {
// some other error occurred, tell error callback
onFailure(readReply.error);
}
});
}
callbackForData(toRead);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment