ifvisible.js icon indicating copy to clipboard operation
ifvisible.js copied to clipboard

Memory leak with idle checking

Open jacwright opened this issue 6 years ago • 4 comments

When idle checking is on, every mouse move, keystroke, and scroll event calls wake which iterates through an array of old timers and clears them (though only effectively clearing the last timer which would be the only one ever active). This array grows unbounded with each event. An array should not be used here.

Current:

trackIdleStatus = ->
    timer = []
    wakeUp = ->
      timer.map(clearTimeout);
      ifvisible.wakeup()  if status isnt "active"
      idleStartedTime = +(new Date())
      timer.push setTimeout(->
        ifvisible.idle()  if status is "active"
      , idleTime)
...

Fix:

trackIdleStatus = ->
    timer = null # No array, just a reference to the last timeout id
    wakeUp = ->
      clearTimeout(timer); # Clear previous timeout. A noop if the timeout already ran
      ifvisible.wakeup()  if status isnt "active"
      idleStartedTime = +(new Date())
      timer = setTimeout(->
        ifvisible.idle()  if status is "active"
      , idleTime)
...

jacwright avatar Feb 16 '19 15:02 jacwright

When page is idle and mousemove to 'wake up', ifvisible.wakeUp(wakeUp) and 'mousemove' clear the same timer and set 2 timer. So one of the timers won't be cleared and it will always trigger 'idle' callback. That's what i found in v1.0.6. :(

AbelMakihara avatar Feb 22 '19 08:02 AbelMakihara

@AbelMakihara good catch. Here is an updated fix:

Fix:

trackIdleStatus = ->
    timer = null # No array, just a reference to the last timeout id
    wakeUp = ->
      return ifvisible.wakeup()  if status isnt "active"
      clearTimeout(timer); # Clear previous timeout. A noop if the timeout already ran
      idleStartedTime = +(new Date())
      timer = setTimeout(->
        ifvisible.idle()  if status is "active"
      , idleTime)
...

jacwright avatar Apr 02 '19 16:04 jacwright

is still on maintain ?

woowalker avatar Aug 11 '20 07:08 woowalker

I do believe this to be fixed in my fork https://github.com/rosskevin/ifvisible. If not please PR a test (expanded tests are present there)

rosskevin avatar Jun 18 '22 02:06 rosskevin