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

Access non-decorated function #335

Open
eikebartels opened this issue Dec 19, 2020 · 8 comments
Open

Access non-decorated function #335

eikebartels opened this issue Dec 19, 2020 · 8 comments

Comments

@eikebartels
Copy link

First of all, thanks for this sweet library!

I start using it and I don't get one thing. I have the following:

 makeAwsomeString(): string {
}
  @Action({ rawError: true })
  async doSomething(data: { email: string }) {
    this.makeAwsomeString() /// this causes an error 
  }

When I call makeAwsomeString from an Action I'm getting:

error in v-on handler (promise/async): "typeerror: makeAwsomeString is not a function"

Whats the issue here and how can I solve it?

@fen89
Copy link

fen89 commented Dec 21, 2020

@eikebartels as far as I know: non decorated functions are stripped of when a module gets converted (e,g. only getters, actions, mutation methods will remain) - someone please correct me if I am wrong.

how can I solve it?

Move these non-decorated functions outside of the class module and call them directly (just like helper functions). Something like this:

function toUpperCase(value: string): string {
    return value.toUpperCase();
}

@Module({
  store,
  dynamic: true,
  namespaced: true,
  name: "foo",
})
export default class FooModule extends VuexModule {

  @Action({ rawError: true })
  async doSomething(data: { email: string }) {
    const awesomeEmail = toUpperCase(data.email);
    // do whatever you wish with the converted string
  }
}

@danwalker-caci
Copy link

It appears @fen89 is correct. I for some reason tried to do the same thing. I moved my function to the top and it worked a treat!

@eikebartels
Copy link
Author

One way to make it possible is by decorate a private function like:

  @Action({ rawError: true })
  private async createUserDocument(data: {
    fireUser: firebase.User
    additionalUserInfo: firebase.auth.AdditionalUserInfo
  }) {
  }

@nsorin
Copy link

nsorin commented Feb 18, 2021

This is should be considered a bug, for two main reasons:

  • It makes it impossible to outsource instance logic in reusable methods unless one misuses the "Action" paradigm. Sometimes the called method needs access to this, for instance to call getters, without being an action. So the solution proposed by @fen89 has its limits.
  • It produces an unexpected behaviour that is very hard to debug. By TypeScript/JavaScript standards, @eikebartels 's makeAwsomeString method should be callable from doSomething. It is not obvious at all when looking at the code that such a thing is not possible.

Stripping code from the original class seems like a terrible idea that can only cause headaches to the users. At the very least, private methods should be kept, since their very purpose is to be called by other methods.

@msmsimondean
Copy link

@eikebartels I've tried your suggestion of annotating a private function with @Action({ rawError: true }) and I can't seem to get it to work. I still get is not a function. Do you have slightly longer working example? Thanks!

@msmsimondean
Copy link

@eikebartels I'm trying to call my private function from a Mutation and not from an Action. Could that be related to the error?

@msmsimondean
Copy link

I think I understand now. data is actually the action's context

@msmsimondean
Copy link

I'm not sure the work around works when you want to call non-decorated private functions from a mutation rather than from an action. I've got some helper functions that need to be able to both get and set state and I can't call them from my mutation

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

5 participants