#Encapsulation of BehaviourSubject in Service with Getter/Setter or Methods

24 messages · Page 1 of 1 (latest)

blissful salmon
#

I know it's a good practice to encapsulate BehaviourSubjects in your services and fetch values with the Observable and set values with a setter.

But I wonder what kind of setter is prefered. There is the "old" way with a simple method called setExample(value) or the approach with set example(value). Are there any opinions and why?

Also when I need the current value from the BehaviourSubject in the coding, should I always use the Observable with a take(1) or is it okay to also have a getter with exampleSubject.getValue()?

import { Injectable } from '@angular/core';
import { BehaviorSubject } from "rxjs";

@Injectable({
    providedIn: 'root'
})
export class ExampleService {

    private exampleSubject = new BehaviorSubject<string | null>(null)
    example$ = this.exampleSubject.asObservable();

    setExample(value: string | null) {
        this.exampleSubject.next(value)
    }
}
import { Injectable } from '@angular/core';
import { BehaviorSubject } from "rxjs";

@Injectable({
    providedIn: 'root'
})
export class ExampleService {

    private exampleSubject = new BehaviorSubject<string | null>(null)
    example$ = this.exampleSubject.asObservable();

    set example(value: string | null) {
        this.exampleSubject.next(value)
    }
}
jaunty geyser
#

using an "old" setter vs a set function is nothing but syntactic sugar, so I think it really just boils down to personal preference. In this case I'd personally probably prefer "setExample". Regarding your 2nd question, in general "getValue" is a bad practice as it breaks the reactive paradigm, it forces you to consider things like sync. vs async. and object mutations and stuff, but if you are aware of the boundaries and the implications, feel free to use it. I personally would always use take(1) as it is universally easier to handle and enforces a certain consistency. Hope that helps somewhat, let me know if something is unclear

brisk musk
#

set example is fine.

  • It makes it intuitive if you were to also provide get example to get the current value of the Subject (which is also fine when you just need the current value and a Subscription would be overkill).
  • The keywords "get" and "set" imply an accessor or mutator, which can cause confusion. For methods that aren't accessors or mutators, you should use a different term for clarity (e.g. "fetch" or "update")
blissful salmon
#

@jaunty geyser @brisk musk Thanks a lot for your replies!

blissful salmon
blissful salmon
frank reef
#

I also use _variable$ as a BS and variable$ as an Obsrvbl

frank reef
frank reef
#

How looks take(1) code example?

mint hornet
#

I think there is case where you want to use getter from this reactive variables

#

like when you want to submit a form

#

i dont want to use multiple withLatestFrom just to achieve this

#

but yeah other than that

#

getter just breaks reactive code

#

I believe that Ben Lesh (project lead of RxJS) recommend to not use .value from BehaviourSubject

frank reef
#

I also use get value or something like that to submit form

mint hornet
#

Yep! it is certainly fine, because submit form is purely imperative and it's okay to pull value from your observable

#

@blissful salmon sorry for tagging but this is very important. it's from ben Lesh himself

frank reef
#

If I want to make an true false flag with function and use it in .html, like: displaySomething() in which I have few conditions where one of them relies on BehaviorSubject, can I use getValue() or should I do a pipe() and do all other stuff in it?

mint hornet
#

I prefer use async pipe with that

#

So yes, observable

jaunty geyser
#

Out of curiosity, would signals be a solution for your problem, I mean usually when you work with behaviorSubjects you are using it as some kind of synchronous statemanagement solution, so maybe Signals could simplify your code and get rid of the take(1) for you?