Skip to content

[DEP0096] DeprecationWarning: timers.unenroll() question  #20261

Description

@dougwilson
  • Version: v10.0.0
  • Platform: Windows 20 64-but

I have a question on this deprecation. I am using Timers.active to start a passive timer on a socket and Timers.unenroll to stop it. I see it says I need to change unenroll to clearTimeout but nothing about Timers.active. Will clearTimeout unenroll the timer created from Timers.active ?

Activity

  1. jasnell commented on Apr 24, 2018

    @jasnell
    Member
  2. added
    timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().
    on Apr 24, 2018
  3. Fishrock123 commented on Apr 24, 2018

    @Fishrock123
    Contributor

    It will work, yes, but you should probably move to using setTimeout. (The old API causes more issues than it solves perf-wise.)

    Also, Timers.active doesn't really initialize everything properly. (That's what enroll() was for.)

    Timers.active() is not yet deprecated as it is useful to refresh existing timeouts (even those made by setTimeout in-place without allocating new resources.

    In the future I want to expose this internal function which does that in a better way but I wasn't sure what the end API should be: https://github.com/nodejs/node/blob/master/lib/internal/timers.js#L85-L96

    I could do that sooner I suppose, as the API seems fine to me. I guess browsers might also be interested in this so maybe in the distant future refreshTimeout() would be better, but I am not going to bother with trying to do web standards stuff.

  4. Fishrock123 commented on Apr 24, 2018

    @Fishrock123
    Contributor

    Maybe the deprecation should also mention that if you are using Timers.active in that way you should probably move away from it...?

  5. dougwilson commented on Apr 24, 2018

    @dougwilson
    MemberAuthor

    Maybe. I'm fine with moving, just not sure how to make the move. This came out from the "mysql" module. Is there like a guide I can read to understand the changes I need to make?

  6. dougwilson commented on Apr 24, 2018

    @dougwilson
    MemberAuthor

    I'm just basically enrolling an object to a timer and I extend the timeout every time a data chunk arrives so it is basically an inactivity timeout.

  7. Fishrock123 commented on Apr 24, 2018

    @Fishrock123
    Contributor

    This older code:

    class ThingWithTimer {
      constructor () {
        Timers.enroll(this)
      }
    
      start ()  {
       Timers.active(this)
      }
    
      refresh () {
        Timers.active(this)
      }
      
      done ()  {
        Timers.unenroll(this)
      }
    }

    Becomes something like:

    class ThingWithTimer {
      constructor () {
        this.timer = null
      }
    
      start () {
        this.timer = setTimeout()
      }
    
      refresh () {
        // Refresh timeout in-place
        Timers.active(this.timer) // Eventually, this.timer.refresh() or refreshTimeout(this.timer)
      }
    
      done () {
        clearTimeout(this.timer)
      }
    }
  8. dougwilson commented on Apr 24, 2018

    @dougwilson
    MemberAuthor

    Awesome, will try it out. Is there a way to feature detect when to use this new method vs the old method?

  9. dougwilson commented on Apr 24, 2018

    @dougwilson
    MemberAuthor

    Also I saw in the code there is Timeout.prototype[refreshFnSymbol] already. Any reason not to bring it out from behind a symbol in like 10.1? Seems odd to mix the two APIs temporarily. It would also be an easy way to feature detect from my question above: see if there is a refresh method for timers, right?

  10. Fishrock123 commented on Apr 25, 2018

    @Fishrock123
    Contributor

    Is there a way to feature detect when to use this new method vs the old method?

    Always. I think you need to be on like Node 0.8 for the full API to be realistically worse for perf. The APIs in the example exist and function correctly on all versions of Node.


    Any reason not to bring it out from behind a symbol in like 10.1?

    From the above, I wasn't sure if something like refreshTimeout(timer) would be a better API. Browsers could make use of something like that (although as stated I don't really want a part in that).

    Such an approach could also work better with #19683. (Although something like a getTimerFromId would also work.)

    Ehhh, I'll open a PR to make it public. :)

  11. Fishrock123 commented on Apr 25, 2018

    @Fishrock123
    Contributor

    Ok, PR up at #20298

  12. Trott commented on Nov 17, 2018

    @Trott
    Member

    #20298 landed and was released in 11.0.0.

    @dougwilson Can this issue be closed? Or is there more to be done here?

  13. dougwilson commented on Nov 17, 2018

    @dougwilson
    MemberAuthor

    Yes, all resolved, thank you.

  14. apurv195 commented on Nov 11, 2019

    @apurv195

    Just update the mysql library to latest version

    npm install mysql@2.16.0 --save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions