https://github.com/azer/pubsub
Minimalistic Pubsub Implementation
https://github.com/azer/pubsub
Last synced: 12 months ago
JSON representation
Minimalistic Pubsub Implementation
- Host: GitHub
- URL: https://github.com/azer/pubsub
- Owner: azer
- Created: 2012-12-25T04:18:37.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-05-18T05:15:49.000Z (about 12 years ago)
- Last Synced: 2025-06-02T08:16:51.035Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 242 KB
- Stars: 13
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# pubsub [](https://travis-ci.org/azer/pubsub)
Library for creating individual events with a minimalistic API.
## Install
```bash
$ npm install pubsub
```
## Usage
```js
onReady = pubsub()
onReady(function(a, b, c){ // shortcut to: onReady.subscribe
console.log(a, b, c)
// => 3, 4, 1
})
onReady.publish(3, 4, 1)
```
You can optionally, you can pass `pubsub()` an object to mix the interfaces:
```js
foo = pubsub({ value: 12345 })
foo.subscribe(function () {
foo.value
// => 3.14
// => 158
})
foo.value = 314
foo.publish()
foo.value = 158
foo.publish()
```
## API
### subscribe(`fn`)
```js
foo.subscribe(function(update){
update
// => 3.14
// => 156
// => { last: true }
})
foo.publish(3.14)
foo.publish(156)
foo.publish({ last: true })
```
### subscribe.once(`fn`)
```js
foo.subscribe.once(function(update){
update
// => 3.14
})
foo.publish(3.14)
foo.publish(156)
```
### unsubscribe(`fn`)
### unsubscribe.once(`fn`)