-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
timers: make timeout.refresh() a public API #20298
Conversation
doc/api/timers.md
Outdated
|
||
Sets the timer's start time to the current time, and reschedules the timer to | ||
call its callback at the previously specified duration adjusted to the current | ||
time. This is useful for refreshing a timeout without allocating a new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit/question: Should timeout
be timer
in this sentence? Basically, if it refers to the same thing as timer
in the previous sentence, let's pick one word and stick with it. If it refers to something different, and the definitions are not clear elsewhere in the doc, let's add them?
doc/api/timers.md
Outdated
@@ -61,6 +61,21 @@ timer is active. Each of the `Timeout` objects returned by these functions | |||
export both `timeout.ref()` and `timeout.unref()` functions that can be used to | |||
control this default behavior. | |||
|
|||
### timeout.refresh() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The methods should be arranged alphabetically, so this entry should come after timeout.ref()
, not before it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of small doc nits, but LGTM otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. The only question is whether we want to make it refreshTimeout(timeout)
in case the standards people ever want to add similar functionality? (Since it's IMO useful.)
lib/internal/timers.js
Outdated
@@ -82,7 +80,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) { | |||
initAsyncResource(this, 'Timeout'); | |||
} | |||
|
|||
Timeout.prototype[refreshFnSymbol] = function refresh() { | |||
Timeout.prototype.refresh = function refresh() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesn't need to be named. The name that V8 generates is nicer.
doc/api/timers.md
Outdated
time. This is useful for refreshing a timeout without allocating a new | ||
JavaScript object. | ||
|
||
Using this on a timer that has already called its callback **will reactivate** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs the emphasis.
This diverges further from browsers do - I'm also not sure what purpose this serves over creating a new timeout? Can someone explain? |
I don't really think it does. We already return an object. Adding more methods at this point doesn't make a difference. (Or a helper function like
Performance. clearTimeout + setTimeout back to back are slow. |
Can't we fix that? |
Not really. Even ignoring that, it's still double the work to mark a timeout as disposed, free it up for GC, allocate a new timeout, enlist it, etc. There's a reason we used to use |
Less memory pressure for persistent connections with timeouts. It is without question useful - we do this internally, and the ecosystem also does it because of that. Also, this functionality is technically already available, but in a way that should be deprecated after this. ( (Browsers don't care for having many connections open, so this doesn't really impact them as much anyways.) |
aabdf30
to
fb26f1d
Compare
Thanks for clarifying. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've proposed some tiny changes to the returned values system in this doc: #20310 If that PR is landed, it would be good to rebase and conform)
A bit confused about the labels – @Fishrock123 This doesn’t target the v10.x branch? @BridgeAR I think adding the (@benjamingr If you do think that this doesn’t need further discussion, feel free to say so/re-add the label.) |
We're debating on adding this to lolex in sinonjs/fake-timers#187 ^ If anyone has strong feelings about what we should do - LMK. lolex is pretty widely used (everyone using sinon and soon jest). |
* addons: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) #20668 * assert: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) #20485 * crypto: - The `authTagLength` option has been made more flexible (Tobias Nießen) #20235) #20039 * esm: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) #20403 * http: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) #20075 #20611 * module: - add --preserve-symlinks-main (David Goldstein) #19911 * timers: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) #20298 * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) #20639 - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) #19377 - Support for multi-threaded embedding has been improved. (Anna Henningsen) #20542 #20539 #20541 PR-URL: #20724
* addons: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) #20668 * assert: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) #20485 * crypto: - The `authTagLength` option has been made more flexible (Tobias Nießen) #20235) #20039 * esm: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) #20403 * http: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) #20075 #20611 * module: - add --preserve-symlinks-main (David Goldstein) #19911 * timers: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) #20298 * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) #20639 - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) #19377 - Support for multi-threaded embedding has been improved. (Anna Henningsen) #20542 #20539 #20541 PR-URL: #20724
* addons: - Fixed a memory leak for users of `AsyncResource` and N-API. (Michael Dawson) #20668 * assert: - The `error` parameter of `assert.throws()` can be an object containing regular expressions now. (Ruben Bridgewater) #20485 * crypto: - The `authTagLength` option has been made more flexible (Tobias Nießen) #20235) #20039 * esm: - Builtin modules (e.g. `fs`) now provide named exports in ES6 modules. (Gus Caplan) #20403 * http: - Handling of `close` and `aborted` events has been made more consistent. (Robert Nagy) #20075 #20611 * module: - add --preserve-symlinks-main (David Goldstein) #19911 * timers: - `timeout.refresh()` has been added to the public API. (Jeremiah Senkpiel) #20298 * Embedder support: - Functions for creating V8 `Isolate` and `Context` objects with Node.js-specific behaviour have been added to the API. (Allen Yonghuang Wang) #20639 - Node.js `Environment`s clean up resources before exiting now. (Anna Henningsen) #19377 - Support for multi-threaded embedding has been improved. (Anna Henningsen) #20542 #20539 #20541 PR-URL: #20724
Another nail in the coffin here, farewell ye ol C-style apis. These apis caused numerous other issues that required far too many safeguards. This gets us one step closer to not having to worry about those issues anymore. Refs: nodejs#18066 Refs: nodejs#20298 PR-URL: nodejs#26760
Another nail in the coffin here, farewell ye ol C-style apis. These apis caused numerous other issues that required far too many safeguards. This gets us one step closer to not having to worry about those issues anymore. Refs: nodejs#18066 Refs: nodejs#20298 PR-URL: nodejs#26760 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Another nail in the coffin here, farewell ye ol C-style apis. These apis caused numerous other issues that required far too many safeguards. This gets us one step closer to not having to worry about those issues anymore. Refs: #18066 Refs: #20298 PR-URL: #26760 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Another nail in the coffin here, farewell ye ol C-style apis. These apis caused numerous other issues that required far too many safeguards. This gets us one step closer to not having to worry about those issues anymore. Refs: #18066 Refs: #20298 PR-URL: #26760 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Originally added in bb5575a discussions such as #20261 show the usefulness of this API to the Node.js ecosystem.
cc @nodejs/timers
CI: https://ci.nodejs.org/job/node-test-pull-request/14496/
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesNote to self: Reword the commit to be
timeout.refresh()