为什么值仍然发出
Why values still emitted
我有以下可连接的可观察对象:
//emit value every 1 second
const source = Rx.Observable.interval(1000);
const example = source
//side effects will be executed once
.do(() => console.log('Do Something!'))
//do nothing until connect() is called
.publish();
/*
source will not emit values until connect() is called
output: (after 5s)
"Do Something!"
"Subscriber One: 0"
"Subscriber Two: 0"
"Do Something!"
"Subscriber One: 1"
"Subscriber Two: 1"
*/
const subscribe = example.subscribe(val => console.log(`Subscriber One: ${val}`));
//call connect after 5 seconds, causing source to begin emitting items
setTimeout(() => {
example.connect();
},5000)
setTimeout(() => {
subscribe.unsubscribe();
},7000)
为什么即使我取消订阅,源 observable 仍然发出一个项目?
.connect()
基本上也是一个订阅,所以为了 停止 流,你必须 "disconnect" 它,像这样例如:
let connection;
setTimeout(() => {
connection = example.connect();
},5000)
setTimeout(() => {
connection.unsubscribe();
subscribe.unsubscribe();
},7000)
我有以下可连接的可观察对象:
//emit value every 1 second
const source = Rx.Observable.interval(1000);
const example = source
//side effects will be executed once
.do(() => console.log('Do Something!'))
//do nothing until connect() is called
.publish();
/*
source will not emit values until connect() is called
output: (after 5s)
"Do Something!"
"Subscriber One: 0"
"Subscriber Two: 0"
"Do Something!"
"Subscriber One: 1"
"Subscriber Two: 1"
*/
const subscribe = example.subscribe(val => console.log(`Subscriber One: ${val}`));
//call connect after 5 seconds, causing source to begin emitting items
setTimeout(() => {
example.connect();
},5000)
setTimeout(() => {
subscribe.unsubscribe();
},7000)
为什么即使我取消订阅,源 observable 仍然发出一个项目?
.connect()
基本上也是一个订阅,所以为了 停止 流,你必须 "disconnect" 它,像这样例如:
let connection;
setTimeout(() => {
connection = example.connect();
},5000)
setTimeout(() => {
connection.unsubscribe();
subscribe.unsubscribe();
},7000)