- 主要用于个人学习
-
rxjs-small
is available on npm. To install it, type:$ npm install rxjs-small
'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]
'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