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

[NEW FEATURE] Stateful functions #354

Open
skoropadas opened this issue Apr 30, 2024 · 1 comment
Open

[NEW FEATURE] Stateful functions #354

skoropadas opened this issue Apr 30, 2024 · 1 comment

Comments

@skoropadas
Copy link

Hello guys, recently I created and started using a function that I think can be useful as part of this library.

The utility is quite handy when working with forms and requests like PUT, PATCH, POST, DELETE, i.e., when you need to perform some action on the backend while displaying its state to the user.

StatefulFn

This utility creates a function based on the passed function that contains the execution state based on the new signal API.

Creating a stateful function

This way you can create a function whose state you are interested in:

@Component()
class MyComponent {
  submit: StatefulFn;
  
  constructor() {
    this.submit = createStatefulFn(async () => {
      await firstValueFrom(this.httpClient.post(url));
    })
  }
}

And here's how you can use it in the template:

<form>
  <button (click)="sumbit()" [disabled]="submit.pending()">Submit</button>

  @if (submit.error(); as error) {
    {{ error.message }}}
  }
</form>

Let me know if you're interested and then I'll create a PR.

@ajitzero
Copy link
Contributor

ajitzero commented May 1, 2024

Duplicate of #56 in terms of usage in the template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants