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

🚀[FEATURE]: Action status stream listener decorators #646

Open
futbotism opened this issue Nov 6, 2018 · 7 comments
Open

🚀[FEATURE]: Action status stream listener decorators #646

futbotism opened this issue Nov 6, 2018 · 7 comments

Comments

@futbotism
Copy link

futbotism commented Nov 6, 2018

Great library, im really finding ngxs's clean and straightforward approach a joy to write with

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior

As far as i know this is the only way to subscribe to an action status

this.actions$.pipe(ofActionSuccessful(CartDelete)).subscribe(...)

Expected behavior

It would be neat if we could do this

  @Action(ReadReport)
  readReport(ctx: ReportStateContext) {
    ctx.patchState({ isLoading: true });
    return this.reportsService
      .getReports();
  }

  @ofActionSuccessful(ReadReport) // automatically called as part of action lifecycle
  readReportSuccess(ctx: ReportStateContext, data: Data) {
    ctx.patchState({ data });
  }

  @ofActionFailure(ReadReport) // automatically gets called if ReadReport action throws error
  readReportFailure(ctx: ReportStateContext, error: Error) {
    ctx.patchState({ error });
  }

This would reduce the boilerplate for traditional actions declarations and allows us to hook into the ngxs action stream more easily.

@markwhitfeld
Copy link
Member

I like the concept but the tricky thing is that dispatching the action is a multicast.
ie. The ReadReport action could be handled by multiple states, and could even be handled multiple times within this state. How would the value of data be determined? It would represent multiple return values.

I could foresee a syntax like this:
@Action(ofActionSuccessful(ReadReport))
But the value of the result is still a bit difficult to assertain.

@futbotism
Copy link
Author

futbotism commented Nov 6, 2018

Ah i didn't see that the ofActionSuccessful pipeable operator doesn't actually deliver a value in its stream...

Yea i would rather use the ngxs built in action statuses than have to dispatch success and failure actions for every request.

On a side note, do you recommend using the patter of dispatching success and failure actions as a result of a request? eg.

ReadReport = '[Report] read report';
ReadReportSuccess  = '[Report] read report success';
ReadReportFailure  = '[Report] read report failure';

@splincode
Copy link
Member

@futbotism https://github.com/ngxs-labs/emitter#lifecycle

what do you think about the simplified use of the actions

@futbotism
Copy link
Author

I did have a look at this pattern, and the simplification looks appealing, but i'm reluctant to move away from redux currently

@splincode
Copy link
Member

@arturovt I think we update example into documentation

@splincode
Copy link
Member

@markwhitfeld https://stackblitz.com/edit/ngxs-of-manage-operation

@splincode splincode changed the title Action status stream listener decorators 🚀[FEATURE]: Action status stream listener decorators Nov 4, 2019
@splincode
Copy link
Member

Duplicate #488

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

No branches or pull requests

3 participants