LiteObservable
Cold Observables for JS in just 617 bytes.
Usage
Installing
As a node module
npm i lite-observable
yarn add lite-observable
Creating an Observable
const observable = Observable;
Emitters have the following properties:
Field | Type | Description |
---|---|---|
next | function |
Emits a value. |
error | function |
Emits an error signal. |
complete | function |
Emits a completion signal. |
subscription | object |
The subscription context. |
Subscribing
observable;
Subscriptions have the following properties:
Field | Type | Description |
---|---|---|
active | function |
Returns the current state of the subscription. Returns false if the subscription is disposed or finished. |
dispose | function |
Disposes the subscription. |
Creating your own operators
With the pipe
method, it is easy to create and use your own custom operators.
When constructing operators, it is recommended to use the constructor instead of create
method to prevent subscription overhead, and to directly access the unabstracted Observer object. Use sub
instead of subscribe
to access the operator of the source Observable.
Example below is a map and filter operators:
const map = { const subscribe complete error next = o; source;}; const filter = { const subscribe complete error next = o; source;};
Example usage:
const observable = Observable; observable;
which outputs:
Next: 0
Next: 4
Next: 8
Next: 12
Next: 16