英文:
Subscribe cancels the subject when the source publisher completes
问题
我有一个函数,它接收一个Publisher
并创建一个PassthroughSubject
,用于以下两个目的:
-
- 订阅源 Publisher
-
- 手动
send
值
- 手动
例如:
class Adapter<T>{
let innerSubject=PassThroughSubject<T,Never>()
let scope = Scope() // 自定义类型,Array<AnyCancellable>
func init(_ source:Publisher<T,Never>){
source.register(innerSubject).in(scope)
innerSubject.sink(receiveValue:{debugPrint($0)}).in(scope)
}
func adapt(_ T val){
innerSubject.send(val)
}
}
fn usage(){
let adapter=Adapter(Empty()) // 更改为Empty(completeImmediately:false) 以修复
adapter.adapt(42) // 应该打印 42,
}
内部主题似乎已被取消,因为 Empty 调用了 complete。
我期望内部主题的_subscription_被取消,而不是主题本身。至少在 .Net 中,这是行为方式 - 我是否遗漏了什么?我需要将其包装在广播主题中吗?我以为 Publishers 可以接收多个 Receivers 并进行多播到每个接收者?
英文:
I have a function which receives a Publisher
and creates a PassthroughSubject
that is used to both:
-
- subscribe to the source publisher
-
send
values manually
E.g.:
class Adapter<T>{
let innerSubject=PassThroughSubject<T,Never>()
let scope = Scope() // custom type, Array<AnyCancellable>
func init(_ source:Publisher<T,Never>){
source.register(innerSubject).in(scope)
innerSubject.sink(receiveValue:{debugPrint($0)}).in(scope)
}
func adapt(_ T val){
innerSubject.send(val)
}
}
fn usage(){
let adapter=Adapter(Empty()) // change to Empty(completeImmediately:false) to fix
adapter.adapt(42) // should print 42,
}
The inner subject seems to be cancelled because Empty calls complete.
I would expect the inner subject's subscription to be cancelled, not the subject itself. In .Net at least that's the behaviour - am I missing something? Do I need to wrap this in a broadcasting subject? I thought that Publishers could receive multiple Receivers and multicast to each?
答案1
得分: 2
I would expect the inner subject's subscription to be cancelled, not the subject itself. In .Net at least that's the behaviour - am I missing something?
我会期望内部主题的订阅被取消,而不是主题本身。至少在 .Net 中,这是行为 - 我漏掉了什么吗?
Just like with Observables, when a Publisher emits a completed event, it will not emit any more events. A Subject is a kind of Publisher. The behavior is exactly the same as in .Net so I can only assume you are missing something.
就像可观察对象一样,当发布者发出完成事件时,它将不会再发出任何事件。主题是一种发布者。行为与 .Net 中的完全相同,所以我只能假设你漏掉了什么。
I thought that Publishers could receive multiple Receivers and multicast to each?
我以为发布者可以接收多个接收器并广播给每一个?
Yes they can. But they send the exact same values to each receiver, including the completion event. However, again just like with Observables and Rx Subjects, once it emits a completion event, it cannot emit anything else.
是的,它们可以。但它们将完全相同的值发送给每个接收器,包括完成事件。然而,再次就像可观察对象和 Rx 主题一样,一旦它发出完成事件,就不能再发出其他任何事件。
英文:
> I would expect the inner subject's subscription to be cancelled, not the subject itself. In .Net at least that's the behaviour - am I missing something?
Just like with Observables, when a Publisher emits a completed event, it will not emit any more events. A Subject is a kind of Publisher. The behavior is exactly the same as in .Net so I can only assume you are missing something.
> I thought that Publishers could receive multiple Receivers and multicast to each?
Yes they can. But they send the exact same values to each receiver, including the completion event. However, again just like with Observables and Rx Subjects, once it emits a completion event, it cannot emit anything else.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论