Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andy2046/observants
mini RxJS implementation
https://github.com/andy2046/observants
observable observer reactivex rxjs5
Last synced: about 1 month ago
JSON representation
mini RxJS implementation
- Host: GitHub
- URL: https://github.com/andy2046/observants
- Owner: andy2046
- License: mit
- Created: 2018-03-01T01:48:15.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-13T15:14:54.000Z (almost 7 years ago)
- Last Synced: 2024-08-10T11:44:26.420Z (5 months ago)
- Topics: observable, observer, reactivex, rxjs5
- Language: JavaScript
- Homepage:
- Size: 166 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# observants
observants is a mini RxJS implementation with the following operators: **map**, **filter**, **reduce**, **scan**, **every**, **of**, **from**, **skip**, **take**, **range**, **timer**, **interval**, **fromEvent**, **fromPromise**, **pipe**## Examples
```js
const { Observable, Observer, Subscriber } = require('observants');let observable1 = Observable.create(observer => {
let i = 0
observer.next(i++)
const interval = setInterval(() => {
observer.next(i++)
}, 200)setTimeout(() => {
observer.next('next') // next not dispatched after unsubscribe
observer.complete() // complete not dispatched after unsubscribe
observer.error('error') // error not dispatched after unsubscribe
}, 600)return () => {
console.log('clearInterval')
clearInterval(interval)
}
})let subscription1 = observable1.subscribe(
value => console.log(value),
error => console.log(error),
() => console.log('completed')
)setTimeout(() => {
subscription1.unsubscribe()
}, 500)//=> 0
//=> 1
//=> 2
//=> clearIntervallet observable2 = new Observable(observer => {
let timer = setTimeout(() => {
observer.next('hello')
observer.complete() // complete will trigger unsubscribe
observer.next('world') // next not dispatched after complete
}, 1000)return () => {
clearTimeout(timer)
console.log('clearTimeout')
}
})let subscription2 = observable2.subscribe(
value => console.log(value),
undefined,
() => console.log('completed')
)//=> hello
//=> completed
//=> clearTimeoutlet observable3 = new Observable(observer => {
let timer = setTimeout(() => {
observer.next('hello')
observer.error('error') // error will trigger unsubscribe
observer.next('world') // next not dispatched after error
}, 1000)return () => {
clearTimeout(timer)
console.log('clearTimeout')
}
})let subscription3 = observable3.subscribe(
value => console.log(value),
error => console.log(error),
undefined
)//=> hello
//=> error
//=> clearTimeoutsetTimeout(() => {
Observable.of(4, 5, 6).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
}, 1000)//=> 4
//=> 5
//=> 6
//=> completedsetTimeout(() => {
Observable.of(5, 6).map(x => x + 2).filter(x => x % 2 === 0).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
}, 1500)//=> 8
//=> completedsetTimeout(() => {
Observable.of(1, 2, 3).reduce((acc, x) => acc + x).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
}, 2000)//=> 6
//=> completedsetTimeout(() => {
Observable.of(1, 2, 3).scan((acc, x) => acc + x).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
}, 2500)//=> 1
//=> 3
//=> 6
//=> completedsetTimeout(() => {
Observable.of(5, 6).map(x => x + 2).do(x => console.log('do:' + x))
.filter(x => x % 2 === 0).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
}, 3000)//=> do:7
//=> do:8
//=> 8
//=> completedsetTimeout(() => {
let subscription1 = Observable.interval(200).subscribe(
x => console.log(x),
error => console.log(error),
() => console.log('completed')
)
setTimeout(() => {
subscription1.unsubscribe()
}, 500)
}, 3500)//=> 0
//=> 1setTimeout(() => {
Observable.of().defaultIfEmpty('Empty!').subscribe(val => console.log(val))
//=> Empty!Observable.range(1, 3).subscribe(val => console.log(val))
//=> 1
//=> 2
//=> 3Observable.of(1, 2, 3).every(val => val % 2 === 0).subscribe(val => console.log(val))
//=> falseObservable.from([1, 2, 3]).subscribe(val => console.log(val))
//=> 1
//=> 2
//=> 3Observable.of(1,2,3,4).skip(2).subscribe(console.log)
//=> 3
//=> 4Observable.of(1, 2, 3).take(1).subscribe(val => console.log(val))
//=> 1Observable.fromPromise(new Promise(resolve => {resolve('Resolved!')}))
.subscribe(val => console.log(val))
//=> Resolved!Observable.timer(1000).subscribe(val => console.log(val))
//=> 0let subscription1 = Observable.timer(1000, 2000).subscribe(val => console.log(val))
setTimeout(() => {
subscription1.unsubscribe()
}, 3500)
//=> 0
//=> 1}, 4500)
```
## Installation
```
npm install --save observants
```## Usage
You can import one or multiple operators from `observants`:```js
const { Observable, Observer, Subscriber } = require('observants');
// or
import { Observable, Observer, Subscriber } from 'observants';
```## Todo
> Subject
> Scheduler