Giter VIP home page Giter VIP logo

postal.when's Introduction

postal.when

What is It?

postal.when is a plugin for postal.js which enables "deferred" style behavior, but using a message bus (vs a typical jQuery approach).

With postal.when, you specify the channe/topics you want to wait on, and then a callback that will be invoked once all the messages have arrived. The default behavior is for the "when" to be reset once it fires, allowing another round of the messages to be received until the callback is invoked again - but it can be forced to run only once.

/* channelDefs - is an array of object literals which specify channel and topic
   onSuccess   - invoked once all the messages have successfully arrived
				 (note: onSuccess will receive the data from each individual subscription, as args, in order)
   onError     - [optional] - invoked if the operation fails (currently only a timeout fails the operation)
   options     - [optional] - can provide the following:
				 {
					once: true,     // if true, the 'fork/join" process is only run once
									// and not reset afterwards to run again
					timeout: 2000,  // a timeout value in milliseconds.  It fires the onError
									// callback if the timeout occurs before all messages have arrived
				 }
*/
postal.when(channelDefs, onSuccess, onError, options);
postal.when(channelDefs, onSuccess, options);
postal.when(channelDefs, onSuccess, onError);
postal.when(channelDefs, onSuccess);

Here's a quick usage example:

postal.when([
    { channel: "ChannelA", topic: "topic.on.channel.a" },
    { channel: "ChannelB", topic: "topic.on.channel.b" },
    { channel: "ChannelC", topic: "topic.on.channel.c" },
    { channel: "ChannelD", topic: "topic.on.channel.d" },
], function(a, b, c, d){
    _.each(arguments, function(x) {
        $('body').append("<div>" + x + "</div>");
    });
});

postal.publish( { channel: "ChannelD", topic: "topic.on.channel.d", data: "And it's testable!" } );
postal.publish( { channel: "ChannelB", topic: "topic.on.channel.b", data: "Deferred behavior!" } );
postal.publish( { channel: "ChannelA", topic: "topic.on.channel.a", data: "Hey look!" } );
postal.publish( { channel: "ChannelC", topic: "topic.on.channel.c", data: "Via message bus!" } );

How to Include It

For browsers (build is in ./lib/browser), postal.when will work in both standard and AMD/require.js environments. Simply include it in your project (after underscore and postal, if you're not using AMD), and it will automatically add the "when" member to postal's global object.

For node.js, (build is in ./lib/node), you should require the module and pass in the references to underscore and postal:

// this is assuming underscore and postal have been required above somewhere...
var postalWhen = require("path/to/postal/when");
postalWhen(_, postal);

postal.when's People

Contributors

gerhobbelt avatar ifandelse avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.