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

[Suggestion] Quality of Life improvement on @Input with coerceObservable #1636

Closed
renatoaraujoc opened this issue Nov 1, 2023 · 1 comment
Labels
✍️ Enhancement New feature or request

Comments

@renatoaraujoc
Copy link
Contributor

Hello,

I've just installed version 16, it's a hell of nice work, amazing job!

Problem Solved By The Feature

Right now, we could to pass inputs to components like it's described in the docs:

@Input() set someStringInput(value: string | Observable<string>) {
    this.state.connect('someStringInput', coerceObservable(value));
}

While this works very nicely already, it's cumbersome to re-type the type twice, so I made a little helper here.

Solution

// Declare the wrapper for the input value
type RxInput<T> = Observable<T> | T;

// Decalre this function to route the connect
function rxConnectInput<TState extends object, TStateKey extends keyof TState>(
    state: RxState<TState>,
    key: TStateKey,
    value: RxInput<TState[TStateKey]>
) {
    state.connect(key, coerceObservable(value));
}

// This should be done inside the wrapping class, obviously, but for sake of simplicity, I'm writting like this.
const state = rxState<{ someStringInput: string}(({ set }) => {
    set({
        someStringInput: 'Initial value'
    });
});

@Input() set someStringInput(value: RxInput<string>) {
    rxConnectInput(this.state, 'someStringInput', value);
}

My idea here is just to make things a little bit less boilerplate and more direct to the point, it's working fine here and fully tested.

Renato

@edbzn edbzn added the ✍️ Enhancement New feature or request label Nov 6, 2023
@edbzn
Copy link
Member

edbzn commented Nov 6, 2023

Hey @renatoaraujoc,

What about using @Input({ transform: coerceObservable }) value instead? This way there's less boilerplate, RxInput is not necessary anymore and you just have to call connect.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✍️ Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants