Skip to content

How to properly use/type this when using debounced actions #425

Closed Answered by posva
spaceemotion asked this question in Help and Questions
Discussion options

You must be logged in to vote

The ts-debounce library seems to have the necessary code to infer this when given a function but given the type definitions that enable automatic this inference in Pinia by directly writing the functions in actions, I don't think there is a way to automatically type this when using

actions: {
  foo: debounce(function bar() {
    this.baz = [];
  }),
}

However, with the WIP plugin API (#416), one could add a plugin that reads an option debounce:

defineStore({
  actions: { foo() {} }
  // autocompletion would be possible as well
  denounce: {
    foo: 300 // ms
  }
})

and a plugin that replaces actions to be denounced:

pinia.use(({ store, options }) => {
  if (options.debounce) {
    return O…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@posva
Comment options

Answer selected by posva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants