Skip to content
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

doc: add active, enroll and unenroll #11736

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions doc/api/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,57 @@ added: v0.0.1

Cancels a `Timeout` object created by [`setTimeout()`][].

## Manual Timers

It is possible, but not recommended, to make any object a timer. Under special
circumstances this can be advantageous. However, in general practice, using
this API is strongly discouraged. This is due to the fact that when using this API
it is important to track timer enrollment state. A memory leak will occur if a
timer is enrolled but never unenrolled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph can be simplified a bit...

It is possible (but not recommended) to make any object a timer. In general
practice, however, using this API is **strongly discouraged** due to the potential
for memory leaks should such timers fail to be properly unenrolled.


*Note*: This API is considered *unsafe*. Despite being public, it is subject
to change at any time.

Example:

```js
'use strict';

const timers = require('timers');
const atimer = {
_onTimeout: function() {
console.log('timeout');
}
};

timers.enroll(atimer, 1000); // make the `atimer` object a timer
timers.active(atimer); // start the timer
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should include a clear example showing the timer being unenroll()'d per the warning above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does show it. Line 183, in the callback from the timer completion.


### active(object)

* `object` {Timer} A timer created by [`enroll()`][] to start.

Starts a timer that was created by [`enroll()`][]. If the object has an
`_onTimeout` method, it will be invoked once the timer has completed.

Once a timer has been started, it can be canceled with [`unenroll()`][].

### enroll(object, delay)

* `object` {object} An object to decorate as a timer.
* `delay` {number} The number of milliseconds to wait before the timer completes.

Decorate an object as a timer and add it to a list of timers to be processed
via [the Node.js Event Loop][]. Once decorated, the timer can be started with
[`active()`][].

### unenroll(object)

* `object` {Timer} A timer that has been started by [`active()`][].

Cancel a timer and reset its state.


[the Node.js Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick
[`TypeError`]: errors.html#errors_class_typeerror
Expand All @@ -171,3 +222,6 @@ Cancels a `Timeout` object created by [`setTimeout()`][].
[`setImmediate()`]: timers.html#timers_setimmediate_callback_args
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args
[`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args
[`active()`]: timers.html#timers_active_object
[`enroll()`]: timers.html#timers_enroll_object_delay
[`unenroll()`]: timers.html#timers_unenroll_object