21
21
22
22
'use strict' ;
23
23
24
+ const { internalBinding } = require ( 'internal/bootstrap/loaders' ) ;
24
25
const {
25
- Timer : TimerWrap ,
26
+ getLibuvNow ,
26
27
setupTimers,
27
- } = process . binding ( 'timer_wrap' ) ;
28
+ scheduleTimer,
29
+ toggleTimerRef,
30
+ immediateInfo,
31
+ toggleImmediateRef
32
+ } = internalBinding ( 'timers' ) ;
28
33
const L = require ( 'internal/linkedlist' ) ;
29
34
const PriorityQueue = require ( 'internal/priority_queue' ) ;
30
35
const {
@@ -53,8 +58,9 @@ const kCount = 0;
53
58
const kRefCount = 1 ;
54
59
const kHasOutstanding = 2 ;
55
60
56
- const [ immediateInfo , toggleImmediateRef ] =
57
- setupTimers ( processImmediate , processTimers ) ;
61
+ // Call into C++ to assign callbacks that are responsible for processing
62
+ // Immediates and TimerLists.
63
+ setupTimers ( processImmediate , processTimers ) ;
58
64
59
65
// HOW and WHY the timers implementation works the way it does.
60
66
//
@@ -156,47 +162,38 @@ function setPosition(node, pos) {
156
162
node . priorityQueuePosition = pos ;
157
163
}
158
164
159
- let handle = null ;
160
165
let nextExpiry = Infinity ;
161
166
162
167
let timerListId = Number . MIN_SAFE_INTEGER ;
163
168
let refCount = 0 ;
164
169
165
170
function incRefCount ( ) {
166
171
if ( refCount ++ === 0 )
167
- handle . ref ( ) ;
172
+ toggleTimerRef ( true ) ;
168
173
}
169
174
170
175
function decRefCount ( ) {
171
176
if ( -- refCount === 0 )
172
- handle . unref ( ) ;
173
- }
174
-
175
- function createHandle ( refed ) {
176
- debug ( 'initial run, creating TimerWrap handle' ) ;
177
- handle = new TimerWrap ( ) ;
178
- if ( ! refed )
179
- handle . unref ( ) ;
177
+ toggleTimerRef ( false ) ;
180
178
}
181
179
182
180
// Schedule or re-schedule a timer.
183
181
// The item must have been enroll()'d first.
184
182
const active = exports . active = function ( item ) {
185
- insert ( item , true , TimerWrap . now ( ) ) ;
183
+ insert ( item , true , getLibuvNow ( ) ) ;
186
184
} ;
187
185
188
186
// Internal APIs that need timeouts should use `_unrefActive()` instead of
189
187
// `active()` so that they do not unnecessarily keep the process open.
190
188
exports . _unrefActive = function ( item ) {
191
- insert ( item , false , TimerWrap . now ( ) ) ;
189
+ insert ( item , false , getLibuvNow ( ) ) ;
192
190
} ;
193
191
194
192
195
193
// The underlying logic for scheduling or re-scheduling a timer.
196
194
//
197
195
// Appends a timer onto the end of an existing timers list, or creates a new
198
- // TimerWrap backed list if one does not already exist for the specified timeout
199
- // duration.
196
+ // list if one does not already exist for the specified timeout duration.
200
197
function insert ( item , refed , start ) {
201
198
const msecs = item . _idleTimeout ;
202
199
if ( msecs < 0 || msecs === undefined )
@@ -213,9 +210,7 @@ function insert(item, refed, start) {
213
210
queue . insert ( list ) ;
214
211
215
212
if ( nextExpiry > expiry ) {
216
- if ( handle === null )
217
- createHandle ( refed ) ;
218
- handle . start ( msecs ) ;
213
+ scheduleTimer ( msecs ) ;
219
214
nextExpiry = expiry ;
220
215
}
221
216
}
@@ -252,32 +247,23 @@ function processTimers(now) {
252
247
253
248
let list , ran ;
254
249
while ( list = queue . peek ( ) ) {
255
- if ( list . expiry > now )
256
- break ;
250
+ if ( list . expiry > now ) {
251
+ nextExpiry = list . expiry ;
252
+ return refCount > 0 ? nextExpiry : - nextExpiry ;
253
+ }
257
254
if ( ran )
258
255
runNextTicks ( ) ;
256
+ else
257
+ ran = true ;
259
258
listOnTimeout ( list , now ) ;
260
- ran = true ;
261
259
}
262
-
263
- if ( refCount > 0 )
264
- handle . ref ( ) ;
265
- else
266
- handle . unref ( ) ;
267
-
268
- if ( list !== undefined ) {
269
- nextExpiry = list . expiry ;
270
- handle . start ( Math . max ( nextExpiry - TimerWrap . now ( ) , 1 ) ) ;
271
- }
272
-
273
- return true ;
260
+ return 0 ;
274
261
}
275
262
276
263
function listOnTimeout ( list , now ) {
277
264
const msecs = list . msecs ;
278
265
279
266
debug ( 'timeout callback %d' , msecs ) ;
280
- debug ( 'now: %d' , now ) ;
281
267
282
268
var diff , timer ;
283
269
while ( timer = L . peek ( list ) ) {
@@ -336,7 +322,7 @@ function listOnTimeout(list, now) {
336
322
// 4.7) what is in this smaller function.
337
323
function tryOnTimeout ( timer , start ) {
338
324
if ( start === undefined && timer . _repeat )
339
- start = TimerWrap . now ( ) ;
325
+ start = getLibuvNow ( ) ;
340
326
try {
341
327
ontimeout ( timer ) ;
342
328
} finally {
@@ -474,7 +460,7 @@ function ontimeout(timer) {
474
460
}
475
461
476
462
function rearm ( timer , start ) {
477
- // // Do not re-arm unenroll'd or closed timers.
463
+ // Do not re-arm unenroll'd or closed timers.
478
464
if ( timer . _idleTimeout === - 1 )
479
465
return ;
480
466
0 commit comments