How to avoid the use of Subjects in RX

后端 未结 2 806
悲&欢浪女
悲&欢浪女 2021-02-19 14:10

So I keep reading everywhere that use of Subject is \"bad\" - and I kind of agree with the reasoning.

However, I am trying to think of the best way

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 14:37

    A simple way to enter the observable is via an Action

    private Action _action;
    

    Create the observable

    public IObservable GetObservable()
    {
        return Observable.FromEvent>(
                    ev => _action += ev, 
                    ev => _action -= ev);
    }
    

    Then add to the observable using

    public void OnNext(ObservableArgs args)
    {
        _action?.Invoke(args);
    }
    

提交回复
热议问题