[英]“Unsubscribe” function callback/hook in Observable “executor” function
I am confused about what the purpose of the "dispose" or "unsubscribe" function is for, which is (optionally) returned from an observable "executor" function, like so: 我对“dispose”或“unsubscribe”函数的用途是什么感到困惑,它(可选)从一个可观察的“executor”函数返回,如下所示:
const Rx = require('rxjs');
const obs = Rx.Observable.create(obs => {
// we are in the Observable "executor" function
obs.next(4);
// we return this function, which gets called if we unsubscribe
return function () {
console.log('disposed');
}
});
const s1 = obs.subscribe(
function (v) {
console.log(v);
},
function (e) {
console.log(e);
},
function () {
console.log('complete');
}
);
const s2 = obs.subscribe(
function (v) {
console.log(v);
},
function (e) {
console.log(e);
},
function () {
console.log('complete');
}
);
s1.unsubscribe();
s2.unsubscribe();
What confuses me is that such a function would actually be more likely to hold on to references in your code and therefore prevent garbage collection. 令我困惑的是,这样的函数实际上更有可能保留代码中的引用,从而防止垃圾收集。
Can anyone tell me what the purpose is of returning a function in that scenario, what the function is called, and what it's signature is? 任何人都可以告诉我在这种情况下返回函数的目的是什么,调用函数是什么,以及它的签名是什么? I am having trouble figuring out information about it.
我无法弄清楚它的相关信息。
I also see much more complex examples of returning a subscription from the executor function, for example this: 我还看到了从执行程序函数返回订阅的更复杂的示例,例如:
let index = 0;
let obsEnqueue = this.obsEnqueue = new Rx.Subject();
this.queueStream = Rx.Observable.create(obs => {
const push = Rx.Subscriber.create(v => {
if ((index % obsEnqueue.observers.length) === obsEnqueue.observers.indexOf(push)) {
obs.next(v);
}
});
return obsEnqueue.subscribe(push);
});
This seems to return a subscription instead of just a plain function. 这似乎返回订阅而不是简单的函数。 Can anyone explain what's going on with this?
任何人都可以解释这是怎么回事?
To make it a clear question, what is the difference between doing this: 为了使它成为一个明确的问题,这样做有什么区别:
const sub = new Rx.Subject();
const obs = Rx.Observable.create($obs => {
$obs.next(4);
return sub.subscribe($obs);
});
and not returning the result of the subscribe call: 并且不返回订阅调用的结果:
const sub = new Rx.Subject();
const obs = Rx.Observable.create($obs => {
$obs.next(4);
sub.subscribe($obs);
});
The unsubscribe
function that Rx.Observable.create
needs to return is invoked when downstream does not listen to the stream anymore, effectively giving you time to clean up resources. 当下游不再监听流时,将调用
Rx.Observable.create
需要返回的unsubscribe
功能, Rx.Observable.create
有效地为您提供时间来清理资源。
In regards to your question; 关于你的问题;
.subscribe()
returns the subscription on which you can call .unsubscribe()
. .subscribe()
返回可以调用.unsubscribe()
的订阅。 So if you want to do something with an other subscription you can pipe through that subscription to your downstream: 因此,如果您想对其他订阅执行某些操作,则可以通过该订阅管理您的下游:
const obs = Rx.Observable.create($obs => { const timer = Rx.Observable.interval(300) .do(i => console.log('emission: ' + i)) return timer.subscribe($obs); }); obs.take(4).subscribe(i => console.log('outer-emission:'+i))
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.2/Rx.js"></script>
Without the unsubscribe function you would stop listening to the observable but the interval created internally would keep on running: 如果没有取消订阅功能,您将停止侦听observable,但内部创建的间隔将继续运行:
const obs = Rx.Observable.create($obs => { const timer = Rx.Observable.interval(300) .do(i => console.log('emission: ' + i)) .take(10) .subscribe( val => $obs.next(val), err => $obs.error(err), () => $obs.complete() ); return function(){} // empty unsubscribe function, internal subscription will keep on running }); obs.take(4).subscribe(i => console.log('outer-emission:'+i))
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.2/Rx.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.