https://github.com/alexeyraspopov/newsletter
Observer done right
https://github.com/alexeyraspopov/newsletter
javascript publish-subscribe
Last synced: 5 months ago
JSON representation
Observer done right
- Host: GitHub
- URL: https://github.com/alexeyraspopov/newsletter
- Owner: alexeyraspopov
- License: mit
- Created: 2014-06-21T08:58:21.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2024-11-11T14:24:21.000Z (6 months ago)
- Last Synced: 2024-12-01T03:34:08.511Z (5 months ago)
- Topics: javascript, publish-subscribe
- Language: JavaScript
- Homepage: https://alexeyraspopov.github.io/newsletter
- Size: 40 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Newsletter.d.ts
- License: LICENSE
Awesome Lists containing this project
README
# newsletter
npm install newsletter
Simple pub/sub implementation.
## ESM Package
Starting from `v4.0` this package fully moved to ES Modules and ES2015 code. This means no more
build step before publishing to NPM.Ideally you shouldn't spot any difference, but in case you face any issues, see
[this useful article](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-move-my-commonjs-project-to-esm).You can also downgrade to `v3.x` to use all the same functionality, precompiled to ES5.
## API
To create publisher instance use `Newsletter` constructor (see Usage). Instance implements next
interface:- `publish` - invokes all listeners and pass some received data to them
- `subscribe` - adds new listener (function) and returns subscription handling instance## Usage
```javascript
// get newsletter
import { Newsletter } from "newsletter";// create instance
var signal = new Newsletter();// subscribe notifications
var subscription = signal.subscribe((data) => console.log(data));// publish some data to subscribers
signal.publish(13);// remove listener
subscription.dispose();
```There is a way to subscribe to a single update
```javascript
var signal = new Newsletter();// subscribe to a single update
var subscription = signal.subscribe((data) => {
subscription.dispose();
console.log(data);
});// will call a listener and remove it
signal.publish(13);// no listeners called
signal.publish(14);
```[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) API can be used to
dispose a subscription:```javascript
var pubsub = new Newsletter();
var ctrl = new AbortController();// subscribe to a single update, pass AbortSignal as second param
pubsub.subscribe((data) => {
console.log(data);
}, ctrl.signal);// will call a listener, as expected
pubsub.publish(13);// abort controller can be aborted at any occasion
ctrl.abort();// no listeners called
pubsub.publish(14);
```The project is licensed under the [MIT](./LICENSE) license.