How to properly use/type this when using debounced actions #425
-
I am trying to debounce an action (using the ts-debounce package). The problem I am an encountering is that actions: {
// like so
foo() {
this.baz = 123;
},
// or so
foo: function () {
this.baz = 123;
}
} but not when using a "wrapper" for the action: actions: {
foo: debounce(function bar() {
this.baz = [];
}),
}
Even if I try to set the
Is there a way to fix these types? Many thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The ts-debounce library seems to have the necessary code to infer actions: {
foo: debounce(function bar() {
this.baz = [];
}),
} However, with the WIP plugin API (#416), one could add a plugin that reads an option 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 Object.keys(options.debounce).reduce((debouncedActions, action) => {
debouncedActions[action] = debounce(store[action], options.debounce[action])
return debouncedActions
}, {})
}
}) Definitely worth adding a cookbook entry once I get a first version of the plugin API out |
Beta Was this translation helpful? Give feedback.
The ts-debounce library seems to have the necessary code to infer
this
when given a function but given the type definitions that enable automaticthis
inference in Pinia by directly writing the functions inactions
, I don't think there is a way to automatically typethis
when usingHowever, with the WIP plugin API (#416), one could add a plugin that reads an option
debounce
:and a plugin that replaces actions to be denounced: