-
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: expose timer classes #8192
Conversation
This allows for determining whether an arbitrary object is a timer object, using instanceof.
@bengl This will also need some docs in the form that timer classes cannot "just be" constructed usefully, and sub-classing is not especially useful either. I do think it is a good idea though. I ran into this the other day, my workaround code... :/ loggingTimer.constructor.name === 'Timeout' |
The doc update sounds a bit intimidating. It might be a better idea to expose something like |
@cjihrig That might be better. We could probably also do some detection for |
+1 to a simple |
There are precedents for classes that are exposed and yet of little use when manually constructed or extended. For example,
I can definitely add something like |
We could create and expose a fake class and use Symbol.hasInstance to make it return true on timer objects. |
|
-1. Why would would you ever need this? If you create a timer, you know it's a timer. If you don't, you shouldn't care. |
may be its good to know when serializing objects, to skip? |
Sometimes you want test assertions for this. it IS useful. |
Can you give an example? In browser these are simply numbers and it seems to be ok |
If we export -1. Also, |
Btw, work-around: const Immediate = setImmediate(() => {}).constructor;
const Timeout = setTimeout(() => {}).constructor; I would highly recommend against using that though, as that actually relies on implementation details and is not portable between Node.js and browsers (that is important, as the |
@sindresorhus That's true for many other classes too. That sounds like a big can of worms versus just exposing these two classes with the understanding that @vkurchatkin Objects can be passed around between modules. The types of the objects being passed in to exported functions aren't always a given.
They are documented to some degree.
This would be an API addition, by exposing classes whose instances we already provide to users, making it semver-minor, but yes, Locked seems stricter than semver here. I'm not sure if there's any precedent for changing a locked API in this kind of way (exposing classes that are already being used/returned). It's also worth noting that the
Yep! I'm doing something like this right now, and it works fine. It would be quite helpful to have them directly exposed though.
Since |
@bengl you need to know that object is a timer only if you want to cancel it. I can't imagine a function that simultaneously accepts timers and something else, and cancels a timer if it is. In practice it seems to be useful only for writing bad code You can also thing of it as an internal detail: you get an object with |
Well, they are documented as being internal, without any information on how to use them or manually instantiate objects of that type, which one would expect if the constructors are exported. Documentation changes would still be needed if those are exported.
Which falls under «API changes» imo, noting the full description of the stability level:
Between «security», «performance», «bug fixes», and «API changes», this looks much more like an API change to me.
And I view that as an issue of our process or our documentation. See #6528. |
A a user, the It seems like trusting JS's reflection ( |
c133999
to
83c7a88
Compare
Feel free to reopen this PR if you get back to working on it. |
Note that some of my comments above are not applicable now, because |
Checklist
make -j4 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
timers
Description of change
This allows for determining whether an arbitrary object is a timer object, using
instanceof
. For example, code intent on unreffing or clearing timers passed to it may want to check that the are, in fact, timers first.Test included. Docs already covered timer classes.