Code Monkey home page Code Monkey logo

mediator's Introduction

mediator

Another implementation of mediator pattern in JavaScript that the publish method returns a promise object that informs the publisher when all the subscribers completed their asynchronous or synchronous tasks.

There are some cases that a publisher wants to know when all the subscribers of a specific channel complete their asynchronous tasks while the publisher does not any knowledge about its subscribers. The publish method of this library returns a promise of all the returned promises of subscribers that do an asynchronous task and want to inform the publisher about completion of the task. The returned promise of the publish method resolves or rejects on completion of all the subscribers promises (resolve or reject). In other hand if a subscriber returns a promise that rejects later, the publish method of the publisher do not ignore the remaining promises.

This approach is not like the jQuery.when() that ignores the remaining promises immediately if any of the promises rejects.

###Dependencies

This implementation depends on jQuery for it's deferred objects.

How to use?

###mediator.subscribe

Add a subscriber callback for the channel that will be called with the specified context (if any). If context is not specified, mediator itself will be use.

mediator.subscribe(channels, callback[, context]);

channels
Type: String or Array of Strings
An Array of strings or a white space delimited string as the mediator channel names.

callback
Type: Function
Callback function that invokes on a publish with the specified channel. All the parameters that passed to publish method will be arguments of this callback function plus the published channel string.

context (Optional)
Type: any JavaScript valid type
The context of the callback function.

###mediator.unsubscribe

Remove all subscribers of the channels with the specified callback. If callback is not specified, all the subscribers of the channels will be remove.

mediator.unsubscribe(channels[, callback])

chanels
Type: String or Array of Strings
An Array of strings or a white space delimited string as the mediator channel names.

callback (Optional)
Type: Function
A reference of the callback that passed to the subscribe method.

###mediator.publish

Dispatch data over the specified channel and returns a promise of all the promises returned from subscribers callbacks. If a promise fails, the result promise will not be reject and continue waiting for other promises. The result promise will be resolve if the return of all the subscribers resolves, and rejects otherwise.

mediator.publish(channel[, arg1[, arg2[, ...[, argN]]]]);

channel
Type: String
Any string as the mediator channel name.

arg1, ..., argN (Optional)
Type: any JavaScript valid type
The arguments that will be passing to the registered subscribers.

returns
A promise of all the returned promises of subscribers.

Example

// In module A
mediator.subscribe('app:refresh', function () {
	return $.ajax({...});
});

// In module B
mediator.subscribe('app:refresh', function () {
	return $.ajax({...});
});

// In module C
mediator.subscribe('app:refresh', function () {
	// Do not return any promise.
});

// In module D
mediator.publish('app:refresh', ...).then(function () {
	// Now two ajax calls in modules A and B completed successfully.
}, function () {
	// Ajax calls in modules A and B completed but One of them or both failed.
});

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.