gertqin / vuex-class-modules

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

Request for new feature - @Onload method decorator #38

Closed LiamWiddop closed 4 years ago

LiamWiddop commented 4 years ago

As per #37, I have opened a pull request. Regards.

gertqin commented 4 years ago

Hi, first of all thanks for your contribution!

I am however a bit hesitant about adding this feature, as it is not somehing that standard Vuex supports. The goal of vuex-class-modules is not to build a new framework on top of Vuex, but simply to provide simple class syntax to access it.

Also, I have a hard time seeing the benefit of this decorator over just calling the methods after instantiating the module, like

export const userModule = new UserModule({ store, name: "user" });
userModule.onLoad();

Have I missed something?

LiamWiddop commented 4 years ago

Not a problem, thanks for having a look.

I came up with this idea as I was looking to create a similar functionality as mounted in vue. I sought this as modules could thereby carry independent functionality for ease of translation between projects while also allowing better support for standalone methods that did not necessarily warrant being called - such as one that establishes some websocket listeners. I tried this in the module constructor, but the environment of this was not the same as those shared by @Action or @Mutation methods. Hence, I made the decorator.

You're right in that you can instil the same functionality through calling the method directly. However, I was working towards making the vuex modules carry some independent or standalone functionality.

Do my thoughts make sense?

Again, thanks!

gertqin commented 4 years ago

Hmm, I see your point, but I'm still not sure whether it is a good idea to introduce "lifecycle" hooks into the modules. If anything I actually thinks it makes the modules less standalone, as the apps using it now have to know their internals, in order to register them at the right time. For instance if the user needs to be authenticated before registering the websocket listeners, you would have to wait for that before you can register the module.

If the modules are just simple global storage, as in vuex, you can just register and use them whenever you need to, without having to worry about its internals.

LiamWiddop commented 4 years ago

Mhmm, I can see your point. No worries, thanks for taking a look into it!