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

I have a few suggestions #105

Closed
anion155 opened this issue Oct 14, 2019 · 3 comments
Closed

I have a few suggestions #105

anion155 opened this issue Oct 14, 2019 · 3 comments

Comments

@anion155
Copy link

Hi, @denisw ! First of all, great lib!

I have a couple suggestions. Just wanted to know if you are interesting in any of them )).

Add payload creator option to createAction#withPayload, for custom arguments in generated action creator. Also maybe add helpers like withMeta, error for FSA compabillity.

Create some form of middleware generator, with ho-functions mapped to corresponding action type. In my projects I'm using this construction:

declare function middlewareMappedByActions(): {
  onAction<T, A>(action: { type: T; (...args: any[]): A; }, middleware: (store: Store, next, action: A) => any): this;
  map(): (store: Store) => (next) => (action) => any;
};

declare function middleBefore<A>(middle: (store: Store, action: A) => any): (store: Store, next, action: A) => any;

middlewareMappedByActions()
.onAction(actions.setHostStatus, middleBefore((store, action) => {
  // do staff
})
.map()
@denisw
Copy link
Owner

denisw commented Oct 15, 2019

Hi @anion155! Thanks a lot for you for taking the time for making these suggestions. 🙂

Add payload creator option to createAction#withPayload, for custom arguments in generated action creator. Also maybe add helpers like withMeta, error for FSA compabillity.

Good idea! I was already considering that, but just didn't get around to it yet. Out of curiosity, are there any particular use cases you have for action metadata and/or the error flag?

Create some form of middleware generator, with ho-functions mapped to corresponding action type. In my projects I'm using this construction:

declare function middlewareMappedByActions(): {
  onAction<T, A>(action: { type: T; (...args: any[]): A; }, middleware: (store: Store, next, action: A) => any): this;
  map(): (store: Store) => (next) => (action) => any;
};

declare function middleBefore<A>(middle: (store: Store, action: A) => any): (store: Store, next, action: A) => any;

middlewareMappedByActions()
.onAction(actions.setHostStatus, middleBefore((store, action) => {
  // do staff
})
.map()

I'm interested in learning more about how you use this. Do you have some examples where this helper was useful for you?

@anion155
Copy link
Author

Out of curiosity, are there any particular use cases you have for action metadata and/or the error flag?

Not yet, sometimes it can used, but for me it was two or three this kind of actions, and in all my cases it's all was handled by middleware, 'cause I don't really like to store object like error or some mythic-non-typed meta information in store. But FSA is kind of standart already, and some people may found some good use for this.

@anion155
Copy link
Author

anion155 commented Oct 15, 2019

I'm interested in learning more about how you use this. Do you have some examples where this helper was useful for you?

For the middleBefore, it is just shortcut for:

store => next => action => {
  // do staff
  return next(action);
}

Similar middleAfter:

store => next => action => {
  const result = next(action);
  // do staff
  return result;
}

For the middlewareMappedByActions, I think it's common case for middlewares that do staff on some types of action:

store => next => action =. {
  switch (action.type) {
    case 'SOME_AWESOME_ACTION_TYPE':
      // do staff
      return next(action);
  }
}

Helper just creates map of handlers, and map method returns middleware it self. I'm using it simple middleware declarations like export default middlewareMappedByActions.... All helpers returns currying functions.

@anion155 anion155 reopened this Oct 15, 2019
@anion155 anion155 closed this as completed Jan 3, 2024
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