question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BehaviorSubject subject.value is not set immediately after onNext

See original GitHub issue

Hello, I have been using Reaktive since a week and saw that BehaviorSubject sets value (_value) in onBeforeNext which is called some point later when serializer calls onSerializedValue Is this expected behavior?

The problem I am facing is, I am using BehaviorSubject to store some state and immediately after onNext if I read the value it does not reflect the latest value and wrong value is being set again. (PS: I am using this in Store in Mvi pattern but my State is global like Redux app) I am making sure that only one thread is allowed to read and write to store’s state (Stored in behavior subject) still I end up getting wrong state due to this behavior. Currenly I am using below workaround.

class Store(initialState: GlobalState): StoreInterface, DisposableScope by DisposableScope() {
    private val subject = BehaviorSubject(initialState)
    private var currentState: GlobalState = initialState

    override fun getState(): GlobalState {
        // FIXME: 18/09/20 
        //  subject.value does not give latest value if called immediately after onNext so maintaining separate var
        // return subject.value
        return currentState
    }

    override fun setState(newState: GlobalState) {
        currentState = newState
        subject.onNext(newState)
    }

    override fun subscribe(observer: ObservableObserver<GlobalState>) {
        subject.subscribe(observer)
    }
}

If BehaviorSubject is indeed correct as per Rx specifications, can you suggest any better workaround to the issue? If DefaultSubject can be made public then also we can make our own custom Subject where value can be set in onNext itself rather than onBeforeNext

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arkivanovcommented, Sep 19, 2020

Hello. BehaviourSubject sets its value before calling subscribers. The only case when it won’t set the value immediately is when you call onNext recursively, while previous value is being emitted. The latter looks like a bug. I will check this and fix is needed. Thanks.

0reactions
arkivanovcommented, Sep 25, 2020

Closing this one. Feel free to reopen if there are any questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Behavior subject not updating subscriber on .next(value)
I am new to typescript but i think i must just be missing something small. it seems correct from what i have seen....
Read more >
BehaviorSubject (RxJava Javadoc 3.1.5) - ReactiveX
The BehaviorSubject does not support clearing its cached value (to appear empty again), however, the effect can be achieved by using a special...
Read more >
Angular View Updating Using Observables, Behavior Subject ...
The reason being is that our observable is “pointed” to an http request, which by design closes as soon as it delivers its...
Read more >
BehaviorSubject - Learn RxJS
If you want the last emitted value(s) on subscription, but do not need to supply a seed value, ... const subject = new...
Read more >
Understanding RxJS Subjects and their use - Tevpro
Subject has no initial value and does not return the current value on Subscription. It triggers only on .next(value) call and return/output the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found