Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add higher level functionality to API #39

Open
tejacques opened this issue Aug 11, 2015 · 1 comment
Open

Add higher level functionality to API #39

tejacques opened this issue Aug 11, 2015 · 1 comment
Milestone

Comments

@tejacques
Copy link
Owner

Currently thinking something along the lines of this:

crosstab({ event, data, destination, callback, timeout })

It could be used like this:

Setup

crosstab.on('PING', function(message, reply) {
  if (message.destination === crosstab.id
    || (!message.destination && crosstab.isMaster())) {
    reply('PONG'); // replies directly to this message
  }
});

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:

crosstab.onDirect(event, fn(message, reply));
crosstab.onMaster(event, fn(message, reply));
crosstab.on(event, fn(message, reply), type=DIRECT | MASTER);

Call

crosstab({
  event: 'PING',
  callback: function(response, err) {
    if(!err) {
      console.log("Received: ", response.data, " from: ", response.origin);
    } else {
      console.log("Error: ", err);
    }
  },
  timeout: 1000
});
crosstab.broadcast(event, data, destination, callback = null, timeout = infinity);
crosstab.broadcast(event, destination, callback = null, timeout = infinity);
crosstab.broadcast(event, callback = null, timeout = infinity);

crosstab.broadcastMaster(event, data, callback = null, timeout = infinity);
crosstab.broadcastMaster(event, callback = null, timeout = infinity);

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.

@tejacques tejacques added this to the Version 0.3.0 milestone Aug 17, 2015
tejacques added a commit that referenced this issue Aug 22, 2015
@tejacques
Copy link
Owner Author

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()

tejacques added a commit that referenced this issue Nov 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant