PubsubJS
Description
pubsubjs is Micro pubsub library which provides the observer pattern to JavaScript.
It works in the browser and server (Node).
Usage
In browser DownLoad pubsub.js, or install via bower
$ bower install pubsubjs
and then include single JavaScript file:
On server install PubsubJS via npm first:
npm install pubsubjs
and then include it in your project with:
var pubsub = ;
What is Pubsub
Publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers. Published messages are characterized into classes, without knowledge of what, if any, subscribers there may be. Subscribers express interest in one or more classes, and only receives messages that are of interest, without knowledge of what, if any, publishers there are.
Example Usage
pubsub#publish / pubsub#subscribe
For example, "The display of notifications and mail to desktop when mail arrives" can be written using PubsubJS as follows.
// Define the global subscriber use of pubsubjspubsub; pubsub; mailer;
Also, you may want to think subscriber publish as below.
pubsub; pubsub; mailer;
pubsub#subscribeOnce
When are notified first and a subscriber is performed.
It is realizable by using subscribeOnce
.
var pubsub = PubSub actual = a: 0 ; pubsub;pubsub;pubsub;assert; // pass
pubsub#unsubscribe
You may think that you would like to remove subscriber at the time of some conditions.
It is realizable by using unsubscribe
.
var pubsub = PubSub actual = a: 0 ; var { actuala += 1; if actuala > 1 pubsub; }; pubsub;pubsub;pubsub;pubsub;assert; // pass
pubsub#Context
pubsub#Context is the most useful API in this library. It is a bit more complex cases, there will often suffer from following situation.
// in jQuerypubsub; ;
This can be solved by using the pubsub#Context.
// in jQuerypubsub; ;
pubsub#dump
You would think when you are developing, you want to know what kind of handler has been registered to any event.
It is a good idea to use the pubsub#dump of the API for debugging such a case.
var pubsub = PubSub actual ; {} {} {} pubsub;pubsub;pubsub; actual = pubsub;assert;
If you want to check handlers that are registered to a particular event, it is preferable to provide an event name as an argument.
var pubsub = PubSub actual ; {} {} {} pubsub;pubsub;pubsub; actual = pubsub;assert;
Notice: This API is for debugging purposes only. Please do not use in production code.
All API
- Pubsub#create()
- pubsub#publish(eventName, context/null, arg1, arg2...)
- pubsub#subscribe(eventName, handler)
- pubsub#subscribeOnce(eventName, handler)
- pubsub#unsubscribe(eventName, [handler])
- pubsub#dump([eventName])
- pubsub#globalContext
- pubsub#Context
- Context#create()
- context#publish(eventName, context/null, arg1, arg2...)
- context#subscribe(eventName, handler)
- context#subscribeOnce(eventName, handler)
- context#unsubscribe(eventName, [handler])
- context#dump([eventName])
Contributors
License:
(The MIT License) Copyright (c) 2013 nazomikan https://github.com/nazomikan/PubsubJS Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.