gertqin / vuex-class-modules

Typescript class decorators for vuex modules
MIT License
193 stars 20 forks source link

Action method type #3

Closed bodograumann closed 5 years ago

bodograumann commented 5 years ago

You define the type of an action method as TypedPropertyDescriptor<(arg?: any) => Promise<void>>'. I think there should be more options.

  1. I want to be able to asynchronously return some value. So it should be Promise<any>.
  2. Is there a reason, that the method has to return a Promise, i.e. be asynchronous?
gertqin commented 5 years ago

Hmm, I'm not convinced:

  1. Vuex actions should not return values - see https://github.com/vuejs/vuex/issues/46 (although it works now you cannot be sure that it will in future implementations of vuex as it is not part of their API)
  2. Like above, you cannot be sure that the internal implementation of vuex actions won't return a promise eventhough your function definition is synchronous (if you have multiple handlers for the same action a promise is actually always returned). Therefore in order to wait for their execution I think you should always 'await' them.
c01nd01r commented 5 years ago

@gertqin

  1. As I can see, the issue are you referring is outdated. If I'm not mistaken, this was relevant for Vuex v0.3.0 (?). At the moment, Actions (dispatch method) can return resolved Promise with payload. https://github.com/vuejs/vuex/blob/dev/types/index.d.ts#L41 I don't think it will change in the near future. But for next major Vuex version, there may be more extensive changes (for example, "Getting rid of the need for separating actions and mutations")
bodograumann commented 5 years ago

I was only trying to convert my existing code to use vuex-class-modules. As @c01nd01r has pointed out, vuex allows returning values and defining actions as synchronous functions. So that’s what I did ^^

Looking more closely at my code, I now see, that I don‘t actually use the returned value. Furthermore the question of (a)synchronous methods came up because I am calling rxjs methods, which return Observables and I felt reluctant to call toPromise everywhere.

So now I’m converting all my Observables to Promises and using then instead of subscribe, but in the future I might want to do more with the Observables and I don’t know whether this is even possible with the vuex api.

gertqin commented 5 years ago

Alright, as the official Vue typings allow return type any for actions, I will change the typings to that too.