Create one-time subscription

后端 未结 6 2315
遇见更好的自我
遇见更好的自我 2020-12-13 01:05

I need to create a subscription to an Observable that is immediately disposed of when it is first called.

Is there something like:

obser         


        
6条回答
  •  轮回少年
    2020-12-13 02:05

    I had similar question.

    Below was called even later on from different state changers. As I did not want to.

    function foo() {
        // this was called many times which was not needed
        observable.subscribe(func);
        changeObservableState("new value");
    }
    

    I decided to try unsubscribe() after subscribe as below.

    function foo() {
        // this was called ONE TIME
        observable.subscribe(func).unsubscribe();
        changeObservableState("new value");
    }
    

    subscribe(func).unsubscribe(); is like subscribeOnce(func).

    I hope that helped you too.

提交回复
热议问题