Open tejacques opened 8 years ago
It might make more sense to treat this an an observable (RxJs style) in this form:
crosstab
// Listen to PINGs
.subscribe('PING')
// Only handle messages from master or direct
.filter(message =>
crosstab.filters.fromMaster(message)
|| crosstab.filters.directMessage(message))
// For each PING, reply with a PONG
.forEach((message) => {
crosstab.reply(message, 'PONG');
});
// Send message
crosstab
// Send PING, and listen for replies
.broadcast('PING')
// Set a 1000ms timeout
.timeout(1000)
// Only take the first reply
.take(1)
// For each reply, print out the response
.forEach((response => {
console.log('PING response: ', response);
});
forEach
, filter
, and map
would take a function in the form of:
function(message, index, observable) {
// body
}
It's actually probably easier to do this as an RxJs wrapper with something like crosstab.RxWrapper()
So really, there are two wrappers going on here:
crosstab.promiseWrapper()
and crosstab.RxWrapper()
Currently thinking something along the lines of this:
It could be used like this:
Setup
It's still a little cumbersome to indicate that this should only handle direct messages, or if it is the master. This could be resolved by having some helper defaults:
Call
This will greatly reduce boilerplate and increase ease of use for creating tools based on crosstab.
It also makes it easy to create a
Promise
wrapper using the callbacks.