0

I have a function that already binds an argument to a listener callback. It was working great but now I need to debounce (lodash) that

I can't seem to get the binding for debounce right so that argument pin_num gets passed along to the .bind of listener callback.

here is what I've attempted

const pinDebounce = function (processor, wait, options, pin) {
  console.log(' ',processor)  // confirm it's a function
  return debounce.bind(processor.bind(this,pin),wait, options)
}

for(const pin_num in this.interrupts) {
  let pin = this.interrupts[pin_num].pin
  let edge = this.interrupts[pin_num].edge
  console.log(`yy starting interrupt on pin ${pin_num} with debounce wait/max:${this.wait},${this.maxwait}`)
  // what I had as the cb to .on which works fine
  // this.interruptProcess.bind(this,pin_num)
  // now wrap that in a debounce
  let pd = pinDebounce(this.interruptProcess,this.wait,{ 'maxWait': this.maxwait },pin_num)
  console.log(' ',pd)  // confirm it's a function
  pin.on('interrupt', pd )
  console.log('edge=',edge)
  if(!this.mock) pin.enableInterrupt(edge)
}

but getting error that my bound debounce is not a function but according to log statements it is.

[INTERRUPT-ERROR] TypeError: Expected a function
    at Function.debounce (/opt/uci/uci-examples/node_modules/lodash.debounce/index.js:144:11)
    at Gpio.emit (events.js:160:13)

https://github.com/lodash/lodash/blob/master/debounce.js

Tried a few variations but no love. So must not have something quite right. Help please

Alternatively is there a better way to get my 'pin_num' bound to the function passed to debounce?

DKebler
  • 1,216
  • 1
  • 15
  • 27

1 Answers1

0

Never forget to provide the first context/this argument to bind even if null.

return debounce.bind(null,processor.bind(this,pin),wait, options)
DKebler
  • 1,216
  • 1
  • 15
  • 27