Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
Killavus committed Dec 28, 2015
1 parent 674193a commit ddcf945
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,76 @@

[![Build Status](https://travis-ci.org/arkency/event-bus.svg?branch=master)](https://travis-ci.org/arkency/event-bus)

Simple event bus for your JavaScript application.
Simple event bus for your JavaScript application without any dependencies and with a low size footprint.

# Usage
## Subscribing to events:

````javascript
import EventBus from 'eventing-bus'

var callback = function() { console.log("Hello!"); };
var callback = function(name) { console.log("Hello, " + name + "!"); };

EventBus.on("exampleEventName", function() { callback(); });
EventBus.publish("exampleEventName"); // Console output: `Hello!`
EventBus.on("exampleEventName", callback);
````

## Publishing events:

````javascript
import EventBus from 'eventing-bus';

EventBus.publish("exampleEventName", "Watson");
/* After registering the subscription and publishing an event you should see
"Hello, Watson!" printed in your browser's console. */
````

## More than one event bus:

To create more than one event bus, you can import the function constructor by itself:
By default you have only one, singleton event bus instance which holds subscriptions from all parts of your application. But nothing stands on your way to create your own, private instances (for example, for each logically distinct part of your complex app):

````javascript
import EventStream from 'eventing-bus/lib/event_stream';

var bus = EventStream();
var anotherBus = new EventStream(); /* Both styles possible. */
/* You can use EventStream both as a constructor and as a factory function. */
var privateBus = EventStream();
var newPrivateBus = new EventStream();
````

# Compatibility
Those _streams_ created by you won't share any subscriptions, nor events.

## Unregistering subscriptions:

If you need to unregister a subscription (a typical case would be inside the React.js component), it is as easy as calling a return value of the `#on` method as a function:

````javascript
import EventBus from "eventing-bus";

var subscription = EventBus.on('event', function() {
// ...
});

/* This will unregister this (and only this) subscription.
subscription();
````
## Unregistering all subscriptions:
Since by default `EventBus` is a singleton instance of the bus, there may be occasions where you need to unregister all subscriptions (most notably - during testing as a `afterEach` step). It can be done by calling `unregisterAllCallbacks` method of an event bus:
````javascript
import EventBus from "eventing-bus";
EventBus.unregisterAllCallbacks();
````
## Compatibility
If you want to use this library on legacy browsers (IE <= 8 etc.), you need to
provide polyfills for `Array.forEach` function. Check out e.g.
[es5-shim](https://github.com/es-shims/es5-shim) to read more.
## Contributing
Feel free to report any issue or idea on the GitHub page of this project. We can't do open source so we can't grasp the typical _fork_ process of doing things. If you report an issue, please try to provide reproducing steps or any piece of code that can reproduce the issue.
Oh, and you can ask us anything by [writing an e-mail to us](mailto:dev@arkency.com).

0 comments on commit ddcf945

Please sign in to comment.