最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to create BehaviourSubject<void>() without initial value? - Stack Overflow

programmeradmin0浏览0评论

I'm using a BehaviourSubject purely as trigger, so the payload doesn't matter. The subject should "fire" (call next) on subscription (a simple Subject wouldn't work!). In principle it would be enough to have a BehaviourSubject<void> for this purpose:

const readonly trigger = new BehaviourSubject<void>()
                                                   ^^
                                                   must not be empty

However, it seems to be impossible to create such a subject. Or is there a way to create a BehaviourSubject of type void without an initial value?

I'm using a BehaviourSubject purely as trigger, so the payload doesn't matter. The subject should "fire" (call next) on subscription (a simple Subject wouldn't work!). In principle it would be enough to have a BehaviourSubject<void> for this purpose:

const readonly trigger = new BehaviourSubject<void>()
                                                   ^^
                                                   must not be empty

However, it seems to be impossible to create such a subject. Or is there a way to create a BehaviourSubject of type void without an initial value?

Share Improve this question edited Oct 21, 2020 at 8:00 deamon asked Oct 20, 2020 at 14:11 deamondeamon 92.6k116 gold badges330 silver badges460 bronze badges 5
  • Sure, the payload doesn't matter but there has to be a payload. How else can you tell the difference between a stream that doesn't emit and a stream that emits nothing? Those two things looks the same. Consider any other signal. "When I say 'BERRY', you start the race" - sure it doesn't matter what word you use, but it wouldn't therefore make sense to say "When I don't say anything, then you start the race." That would be a very poor signal indeed. - I would just pick a boolean or null (small memory footprint) and go with it. – Mrk Sef Commented Oct 21, 2020 at 3:28
  • @MrkSef No, there doesn't have to be a payload. The observer will be notified that something happened even if there is no value, since the next method of the observer will be called. See StackBlitz exmple – deamon Commented Oct 21, 2020 at 7:53
  • undefined is a payload though. In javascript, an uninitialized value actually has the value undefined. undefined is a primitive type. As strange as it sounds, you are initializing your BehaviorSubject with undefined. Your lamba that you pass a parameter to the subscription is called with undefined. – Mrk Sef Commented Oct 21, 2020 at 14:28
  • That is true. But important to me is to avoid calling next with a useless value that could even be misunderstood (because someone sees a meaning in the value itself). And then there is no payload on next. – deamon Commented Oct 21, 2020 at 15:01
  • It seems to me that null is semantically pretty clear in this case. Typescript will plain as well if you try ` trigger.next(). It should say something like next` expects 1 parameter and you've entered 0. So you'd be doing something like trigger.next(null) there as well. In Javascript, trigger.next() is the same as trigger.next(undefined) so if you want to keep the typescript piler happy while making it feel closest to a vanilla JS function call without parameters, then undefined may be the ticket. – Mrk Sef Commented Oct 21, 2020 at 15:16
Add a ment  | 

1 Answer 1

Reset to default 13

clearly Subject doesn't meet your needs as you want a trigger on subscription. you can set the initial value to undefined though...

new BehaviorSubject<void>(undefined)

or null should work fine too.

发布评论

评论列表(0)

  1. 暂无评论