[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A signal with BehaviorSubject prop is possible #56809

Open
muhamedkarajic opened this issue Jul 2, 2024 · 1 comment
Open

A signal with BehaviorSubject prop is possible #56809

muhamedkarajic opened this issue Jul 2, 2024 · 1 comment
Labels
area: core Issues related to the framework runtime cross-cutting: signals
Milestone

Comments

@muhamedkarajic
Copy link
muhamedkarajic commented Jul 2, 2024

Which @angular/* package(s) are relevant/related to the feature request?

No response

Description

I have a scenario which is such that I use inside the .ts file observables actually they are BehaviorSubjects. I believe the difference between those two are pretty minor, I'm writing my code in such a way that my pipe always outputs BehaviorSubjects.

I still want to keep that approach cause its async what I do and I benefit from the pipeline operators from RxJS. However I do see the potential long term which Signals will bring to the table.

Proposed solution

What I would like to do is have beside the model signal also the observable version or better said the BehaviorSubject version cause that one is the closest to the signal. This would have the benefit then that we could still code RxJS inside the ts file while it would still be possible to use the new API.

Imagine we have the following function which adds to our signal the BehaviorSubject reference (almost same as toObservable function):

interface ICustomSignal<T> extends ModelSignal<T>
{
    obs$: BehaviorSubject<T>;
}
export function toCustomSignal<T>(source: ModelSignal<T>, options?: ToObservableOptions): ICustomSignal<T> {
    !options?.injector && assertInInjectionContext(toCustomSignal);
    const injector = options?.injector ?? inject(Injector);
    let castedSource = source as ICustomSignal<T>

    if(castedSource.obs$)
        return castedSource;
    
    const subject = new BehaviorSubject<T>(source());
    castedSource.obs$ = subject;
    
    let subscription = subject.subscribe(x => {
        if (source() == x)
            return;
        source.set(x);
    });

    const watcher = effect(
        () => {
            let value: T;
            try {
                value = source();
            } catch (err) {
                untracked(() => subject.error(err));
                return;
            }
            untracked(() => subject.next(value));
        },
        { injector, manualCleanup: true },
    );

    injector.get(DestroyRef).onDestroy(() => {
        subscription.unsubscribe();
        watcher.destroy();
        subject.complete();
    });

    return castedSource;
}

Then inside the component:

someInput = model<string>('LOADING');
someInputExtended = toCustomSignal(this.someInput);

Alternatives considered

I wonder if you would support such things, cause personally I don't see myself going away from RxJS. Or are the people using RxJS will never truly be first-class citizens in Angular?

Besides, I would personally be more happy if you would not force me to write 2 lines of code, right now this is not possible:

someInput= toCustomSignal(model<string>('LOADING'));
image

Maybe we could have simply a flag which enables this functionality, or the RxJS part provides a signal which is also having the BehaviorSubject part.

@muhamedkarajic muhamedkarajic changed the title Custom signals are possible Custom signal which has a BehaviorSubject is possible Jul 2, 2024
@muhamedkarajic muhamedkarajic changed the title Custom signal which has a BehaviorSubject is possible A signal which has a BehaviorSubject is possible Jul 2, 2024
@muhamedkarajic muhamedkarajic changed the title A signal which has a BehaviorSubject is possible A signal with BehaviorSubject prop is possible Jul 2, 2024
@thePunderWoman thePunderWoman added area: core Issues related to the framework runtime cross-cutting: signals labels Jul 2, 2024
@ngbot ngbot bot modified the milestone: needsTriage Jul 2, 2024
@muhamedkarajic
Copy link
Author

Any updates on this? I just wonder if it could even be considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime cross-cutting: signals
Projects
None yet
Development

No branches or pull requests

2 participants