Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not debounce action within other action in Vuex

I'm trying to debounce anything within an Action, it gets swallowed in one way or another...

Take this (pseudo) code:

import { debounce } from "lodash";

const actions = {
  debounceSomeLogging ({ dispatch }, text) {
    console.log("Outside debounced function.");
    debounce(function() {
      console.log("Inside debounced function.");
      dispatch("doRealThing");
    }, 1000);
  },

  doRealThing({ commit }) {
    // Whatever
  }
}

When I call the action, I see the Outside debounced function, but I can not see the other logging and the other action does not get triggered.

Anyone have experience with this and can point me in the right direction?

like image 240
Titulum Avatar asked Dec 30 '18 11:12

Titulum


2 Answers

This should definate work

import { debounce } from "lodash";

const actions = {
  debounceSomeLogging: debounce(({ dispatch }, text) => {
    console.log("Inside debounced function.");
    dispatch("doRealThing");
  }, 1000),

  doRealThing({ commit }) {
    // Whatever
  }
}
like image 102
Peter Odetayo Avatar answered Oct 16 '22 08:10

Peter Odetayo


As nemesv pointed out in a comment, the debounce function does not call the inner function. So you need to call the debounce again, like so:

debounce(function() {
  console.log("Inside debounced function.");
  dispatch("doRealThing");
}, 1000)();

So, in short, it should look like this:

debounce(...)() instead of like this debounce(...).

like image 3
Titulum Avatar answered Oct 16 '22 06:10

Titulum