use-reducer-async icon indicating copy to clipboard operation
use-reducer-async copied to clipboard

proposal: async callback

Open KonstantinBabichev opened this issue 4 years ago • 5 comments

Idea is to provide ability to listen for async actions callback. basically is to listen on .then of returned Promise. redux-thunk have that ability, so proposal is to introduce it into that package.

KonstantinBabichev avatar Jul 31 '20 08:07 KonstantinBabichev

Hi! Thanks for the proposal. I would like to discuss something about this.

  1. Are you familiar with TypeScript? We need to fix that too.
  2. Because useReducer's dispatch returns nothing, I'm a bit skeptical of this change being not compatible.
  3. What is your use case? I would like to learn it, because I would have used a state value instead to know the async callback result.

Meanwhile, let me work on updating master (to eliminate dist in repo.)

dai-shi avatar Jul 31 '20 09:07 dai-shi

  1. a bit, what exaclty should be fixed?
  2. i'm looking on "thunk" variations of redux hooks, and those folks are fine with it - https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js#L43 . dunno if it's bad or good.
  3. oh, indeed, i also would like to use state, but in my case it will be super-overkill. in my case there are several AJAX calls, once i receive data i have to a) update state (thats fine) b) trigger some stuff manually on , for example update some dots there, move lines, etc. general idea is that operation with is hard via state, it's much much easier to make some adjustments by manually calling it's JS API methods

KonstantinBabichev avatar Jul 31 '20 09:07 KonstantinBabichev

Thanks for the notes. You could trigger some stuff in async functions, couldn't you. Anyway, I understand your mental model coming from redux-thunk.

Basically, because this change is not breaking anything, I would like to merge it if we were able to solve the type issue. One of the goal of this lib is to provide strict typing.

The issue is React.Dispatch returns void. So, we can't simply do Dispatch<ExportAction>. We somehow need to infer Dispatch type from asyncActionHandlers.

dai-shi avatar Jul 31 '20 22:07 dai-shi

thanks! great to hear that! talking about TS, ya, it's tricky or to verbose to do it in proper manner, as far as i can see. at the same time, will something like that do the trick:

const aaHandler = (
  (type && aaHandlers[type]) || null
) as (typeof action extends AsyncAction ? (s: {
  dispatch: Dispatch<ReducerAction<R>>;
  getState: () => ReducerState<R>;
  signal: AbortSignal;
}) => (a: typeof action) => **Promise<Promise<any> | void>** : null);

what do you feel? as far as i understand, it's already returns Promise, so difference is that it can be Promise chain with some output, or void in general case

KonstantinBabichev avatar Aug 03 '20 10:08 KonstantinBabichev

Well, my goal for this library is to provide proper types in TS. (I mean, this is a tiny lib one could create in JS, pretty easily.)

So, no, we'd like to provide no-any types. Like if your action handler returns Promise<boolean>, you will get it with dispatch. I know it's going to be tricky and verbose. I'd be willing to help coding for this, and hope you would be helping with testing. What I think is good is to create a failing test either in __tests__ or examples.

dai-shi avatar Aug 03 '20 23:08 dai-shi

closing as stale.

dai-shi avatar Aug 29 '22 23:08 dai-shi