Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 29, 2019
1 parent 0f43b8f commit 9796855
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 62 deletions.
105 changes: 47 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,74 @@
[![Build Status](https://travis-ci.org/vitaly-t/sub-events.svg?branch=master)](https://travis-ci.org/vitaly-t/sub-events)
[![Coverage Status](https://coveralls.io/repos/vitaly-t/sub-events/badge.svg?branch=master)](https://coveralls.io/r/vitaly-t/sub-events?branch=master)

Events subscription in TypeScript.
Easy event subscription, implemented in TypeScript.

**THIS PROJECT IS UNDER ACTIVE DEVELOPMENT**
Supports all versions of Node.js and web browsers.

<!--
## Preamble
## Install

It was originally written to overcome [RXJS] complexities of monitoring subscriptions, see [this issue](https://stackoverflow.com/questions/56195932/how-to-monitor-number-of-rxjs-subscriptions).
However, it is now a powerful event-handling engine by itself, supporting all browsers and Node.js versions.
```
npm i sub-events
```

## Usage

Install this module via `npm i subcount`.
* On the event's provider side:

### Simple Observable
```ts
import {SubEvent} from 'sub-events';

Class [Observable] works very much like in [RXJS]:
// creating with the event's data type:
const e: SubEvent<string> = new SubEvent();

```ts
import {Observable} from 'subcount';
// trigerring the event when needed:
e.emit('hello');
```

// declare observable with any type:
const a: Observable<string> = new Observable();
* On the event's consumer side:

// subscribe for data:
const sub = a.subscribe((data: string) => {
```ts
// subscribing to the event:
const sub = e.subscribe((data: string) => {
// data = 'hello'
});

a.next('hello'); // send data
sub.unsubscribe(); // unsubscribe
// cancel the subscription when no longer needed:
sub.cancel();
```

And if you need to wait for `next` to finish, you can use method `nextSync` instead.
### Observing Subscriptions

### Counted Observable
Class [CountedObservable] extends [Observable] with `onCount`, to monitor the subscriptions counter:
Class [SubCount] extends [SubEvent] with `onCount`, to monitor the subscriptions counter:

```ts
import {CountedObservable, ISubCounts} from 'subcount';
import {SubCount, ICountChange} from 'sub-events';

// creating with the event's data type:
const e: SubCount<string> = new SubCount();
```

// declare observable with any type:
const a: CountedObservable<string> = new CountedObservable();
Any side can monitor the number of subscriptions:

// monitor the subscriptions counter:
const countSub = a.onCount.subscribe((info: ISubCounts) => {
```ts
const monSub = e.onCount.subscribe((info: ICountChange) => {
// number of subscribers has changed;
// info = {newCount, prevCount}
});

// subscribe for data:
const sub = a.subscribe((data: string) => {
// data = 'hello'
});
// send data:
a.next('hello');
// unsubscribe:
sub.unsubscribe();
countSub.unsubscribe();
```
If you need `onCount` sent synchronously, use `new CountedObservable({sync: true})`.
// cancel monitoring when no longer needed:
monSub.cancel();
```

## Browser
### Browser

Including `./subcount/dist` in your HTML will give you access to all types under `subcount` namespace:
Including `./sub-events/dist` in your HTML will give you access to all types under `subEvents` namespace:

```html
<script src="./subcount/dist"></script>
<script src="./sub-events/dist"></script>
<script>
const a = new subcount.Observable();
a.subscribe(data => {
const e = new subEvents.SubEvent();
e.subscribe(data => {
// data received
});
</script>
Expand All @@ -89,21 +81,18 @@ And when using it directly in TypeScript, you can compile and bundle it any way
**Example:**

```ts
function fromEvent(from: Node, event: string): Observable<Event> {
const obs = new Observable<Event>();
from.addEventListener(event, e => obs.next(e));
return obs;
function fromEvent(source: Node, event: string): SubEvent<Event> {
const sub = new SubEvent<Event>();
source.addEventListener(event, e => sub.emit(e));
return sub;
}

fromEvent(document, 'click').subscribe((e: Event) => {
// handle the event
// handle click events from the document
});
```

See also: [API generated from code](https://vitaly-t.github.io/subcount).
-->
See also: [API generated from code](https://vitaly-t.github.io/sub-events).

[RXJS]:https://github.com/reactivex/rxjs
[Observable]:https://vitaly-t.github.io/subcount/classes/observable.html
[CountedObservable]:https://vitaly-t.github.io/subcount/classes/countedobservable.html
[SubEvent]:https://vitaly-t.github.io/sub-events/classes/subevent.html
[SubCount]:https://vitaly-t.github.io/sub-events/classes/subcount.html
2 changes: 1 addition & 1 deletion src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ICountOptions extends IEventOptions {

/**
* @class
* Extends [[SubEvent]] with [[onCount]] event to monitor subscriptions count.
* Extends [[SubEvent]] with [[onCount]] event to observe the number of subscriptions.
*/
export class SubCount<T = any> extends SubEvent<T> {
protected _notify: (data: any) => number;
Expand Down
6 changes: 3 additions & 3 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ISubscriber<T> {

/**
* @class
* Implements subscribing to events and triggering them.
* Implements subscribing to and triggering an event.
*/
export class SubEvent<T = any> {

Expand All @@ -64,13 +64,13 @@ export class SubEvent<T = any> {
}

/**
* Subscribes to receive all data events.
* Subscribes for the event.
*
* @param cb
* Event notification callback function.
*
* @returns
* Object for un-subscribing safely.
* Object for cancelling the subscription safely.
*/
public subscribe(cb: SubFunction<T>): Subscription {
const sub: ISubscriber<T> = {cb, cancel: null};
Expand Down

0 comments on commit 9796855

Please sign in to comment.