@@ -47,7 +47,6 @@ if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
47
47
// value = list
48
48
var lists = { } ;
49
49
50
-
51
50
// the main function - creates lists on demand and the watchers associated
52
51
// with them.
53
52
function insert ( item , msecs ) {
@@ -151,6 +150,7 @@ exports.enroll = function(item, msecs) {
151
150
exports . active = function ( item ) {
152
151
var msecs = item . _idleTimeout ;
153
152
if ( msecs >= 0 ) {
153
+
154
154
var list = lists [ msecs ] ;
155
155
if ( ! list || L . isEmpty ( list ) ) {
156
156
insert ( item , msecs ) ;
@@ -176,9 +176,7 @@ exports.setTimeout = function(callback, after) {
176
176
after = 1 ; // schedule on next tick, follows browser behaviour
177
177
}
178
178
179
- timer = { _idleTimeout : after } ;
180
- timer . _idlePrev = timer ;
181
- timer . _idleNext = timer ;
179
+ timer = new Timeout ( after ) ;
182
180
183
181
if ( arguments . length <= 2 ) {
184
182
timer . _onTimeout = callback ;
@@ -209,7 +207,7 @@ exports.setTimeout = function(callback, after) {
209
207
exports . clearTimeout = function ( timer ) {
210
208
if ( timer && ( timer . ontimeout || timer . _onTimeout ) ) {
211
209
timer . ontimeout = timer . _onTimeout = null ;
212
- if ( timer instanceof Timer ) {
210
+ if ( timer instanceof Timer || timer instanceof Timeout ) {
213
211
timer . close ( ) ; // for after === 0
214
212
} else {
215
213
exports . unenroll ( timer ) ;
@@ -245,3 +243,37 @@ exports.clearInterval = function(timer) {
245
243
timer . close ( ) ;
246
244
}
247
245
} ;
246
+
247
+ var Timeout = function ( after ) {
248
+ this . _idleTimeout = after ;
249
+ this . _idlePrev = this ;
250
+ this . _idleNext = this ;
251
+ this . _when = Date . now ( ) + after ;
252
+ } ;
253
+
254
+ Timeout . prototype . unref = function ( ) {
255
+ if ( ! this . _handle ) {
256
+ exports . unenroll ( this ) ;
257
+ this . _handle = new Timer ( ) ;
258
+ this . _handle . ontimeout = this . _onTimeout ;
259
+ this . _handle . start ( this . _when - Date . now ( ) , 0 ) ;
260
+ this . _handle . unref ( ) ;
261
+ } else {
262
+ this . _handle . unref ( ) ;
263
+ }
264
+ } ;
265
+
266
+ Timeout . prototype . ref = function ( ) {
267
+ if ( this . _handle )
268
+ this . _handle . ref ( ) ;
269
+ } ;
270
+
271
+ Timeout . prototype . close = function ( ) {
272
+ this . _onTimeout = null ;
273
+ if ( this . _handle ) {
274
+ this . _handle . ontimeout = null ;
275
+ this . _handle . close ( ) ;
276
+ } else {
277
+ exports . unenroll ( this ) ;
278
+ }
279
+ } ;
0 commit comments