RX Subjects - are they to be avoided?

后端 未结 4 1311
攒了一身酷
攒了一身酷 2020-12-15 05:06

I\'ve had a mini-discussion on the topic in another thread, and would like to have people\'s input on the \"bad\" sides of subjects.

People who frequent the RX forum

4条回答
  •  萌比男神i
    2020-12-15 05:25

    One reason that I would be wary of using a Subject as a part of the public API is that it mixes concerns; an observer is a concern distinct from the observable.

    What if some miscreant observer calls OnNext or OnCompleted or OnError on the Subject where it was only supposed to be an observer?

    Even if it isn't a part of the API, and you tuck it away in your server as a private backing field, just the very fact that it has a dual role is disturbing. In the case of using it as a backing field, you are still only expecting it to perform one role / concern -- that of an observable. However, it has the potential to do two things and that's just mentally disturbing.

提交回复
热议问题