Skip to content

Subject: ro.BehaviorSubject get last Value missing #178

@PatrickJung94

Description

@PatrickJung94

I am trying out this package in my project and I like it so far because I am used to RxJS when it comes to frontend. However I have noticed when I want to use a BehaviorSubject like I do in RxJS for state management I am not able to quickly get its latest value via a function. I have looked into the available code and the last value is stored but there is no way to retrieve it other that doing a subscribe.

I have solved this by making a helper function in my code like below but I think there should be a way to get the value like I can in RxJS.

Helper function that I used:

func GetLastValue() StatusType {
	var status StatusType
	sub := statusSubject.Subscribe(ro.NewObserver(
		func(value StatusType) {
			status = value
		},
		func(err error) {
			logrus.Errorf("Status retrieval error: %v", err)
		},
		func() {
			logrus.Info("completed")
		},
	))
	defer sub.Unsubscribe()
	return status
}

I also looked how it is done in RxJS and they have made a simple get function to get the last value:

https://github.com/ReactiveX/rxjs/blob/master/packages/rxjs/src/internal/BehaviorSubject.ts

getValue(): T {
    const { hasError, thrownError, _value } = this;
    if (hasError) {
      throw thrownError;
    }
    return _value;
  }

Possible version in this package but I am not sure how errors would be handled:

func (s *behaviorSubjectImpl[T]) GetValue() T {
	return s.last.B
}

I am not sure if it possible to implement it like this in this go version but personally I find it quite useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreChanges affecting the core logic or main components of the project, in root/internal/testing pkgenhancementRequest or implementation of a new feature or improvement.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions