1.0.3 • Published 8 years ago
rxjs-small v1.0.3
Rxjs 精简版 (inspire by Rxjs)
- 主要用于个人学习
安装
nodejs
rxjs-small
is available on npm. To install it, type:$ npm install rxjs-small
基本使用
Observerable系列 (单路推送)
'use strict';
const Observerable = require('rxjs-small').Observerable;
const observerable = new Observerable((observer) => {
let initial = 1;
let id = setInterval(() => {
observer.next(initial++);
},1000);
return () => {
clearInterval(id);
}
});
observerable.subscribe({
next: value => console.log(`[value]=[${value}]`)
});
setTimeout(function() {
observerable.unSubscribe();
}, 5000);
output:
[value]=[1]
[value]=[2]
[value]=[3]
[value]=[4]
Subject系列 (多路推送)
'use strict';
const Subject = require('rxjs-small').Subject;
const subject = new Subject();
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.next(1);
subject.next(2);
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
subject.next(3)
output:
observerA: 1
observerA: 2
observerA: 3
observerB: 3