Created
March 11, 2013 15:46
-
-
Save Narigo/5135168 to your computer and use it in GitHub Desktop.
vertx event bus addition
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
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); | |
}) |
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
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