2121
2222'use strict' ;
2323
24+ const { internalBinding } = require ( 'internal/bootstrap/loaders' ) ;
2425const {
25- Timer : TimerWrap ,
26+ getLibuvNow ,
2627 setupTimers,
27- } = process . binding ( 'timer_wrap' ) ;
28+ scheduleTimer,
29+ toggleTimerRef,
30+ immediateInfo,
31+ toggleImmediateRef
32+ } = internalBinding ( 'timers' ) ;
2833const L = require ( 'internal/linkedlist' ) ;
2934const PriorityQueue = require ( 'internal/priority_queue' ) ;
3035const {
@@ -53,8 +58,9 @@ const kCount = 0;
5358const kRefCount = 1 ;
5459const kHasOutstanding = 2 ;
5560
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 ) ;
5864
5965// HOW and WHY the timers implementation works the way it does.
6066//
@@ -156,47 +162,38 @@ function setPosition(node, pos) {
156162 node . priorityQueuePosition = pos ;
157163}
158164
159- let handle = null ;
160165let nextExpiry = Infinity ;
161166
162167let timerListId = Number . MIN_SAFE_INTEGER ;
163168let refCount = 0 ;
164169
165170function incRefCount ( ) {
166171 if ( refCount ++ === 0 )
167- handle . ref ( ) ;
172+ toggleTimerRef ( true ) ;
168173}
169174
170175function decRefCount ( ) {
171176 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 ) ;
180178}
181179
182180// Schedule or re-schedule a timer.
183181// The item must have been enroll()'d first.
184182const active = exports . active = function ( item ) {
185- insert ( item , true , TimerWrap . now ( ) ) ;
183+ insert ( item , true , getLibuvNow ( ) ) ;
186184} ;
187185
188186// Internal APIs that need timeouts should use `_unrefActive()` instead of
189187// `active()` so that they do not unnecessarily keep the process open.
190188exports . _unrefActive = function ( item ) {
191- insert ( item , false , TimerWrap . now ( ) ) ;
189+ insert ( item , false , getLibuvNow ( ) ) ;
192190} ;
193191
194192
195193// The underlying logic for scheduling or re-scheduling a timer.
196194//
197195// 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.
200197function insert ( item , refed , start ) {
201198 const msecs = item . _idleTimeout ;
202199 if ( msecs < 0 || msecs === undefined )
@@ -213,9 +210,7 @@ function insert(item, refed, start) {
213210 queue . insert ( list ) ;
214211
215212 if ( nextExpiry > expiry ) {
216- if ( handle === null )
217- createHandle ( refed ) ;
218- handle . start ( msecs ) ;
213+ scheduleTimer ( msecs ) ;
219214 nextExpiry = expiry ;
220215 }
221216 }
@@ -252,32 +247,23 @@ function processTimers(now) {
252247
253248 let list , ran ;
254249 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+ }
257254 if ( ran )
258255 runNextTicks ( ) ;
256+ else
257+ ran = true ;
259258 listOnTimeout ( list , now ) ;
260- ran = true ;
261259 }
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 ;
274261}
275262
276263function listOnTimeout ( list , now ) {
277264 const msecs = list . msecs ;
278265
279266 debug ( 'timeout callback %d' , msecs ) ;
280- debug ( 'now: %d' , now ) ;
281267
282268 var diff , timer ;
283269 while ( timer = L . peek ( list ) ) {
@@ -336,7 +322,7 @@ function listOnTimeout(list, now) {
336322// 4.7) what is in this smaller function.
337323function tryOnTimeout ( timer , start ) {
338324 if ( start === undefined && timer . _repeat )
339- start = TimerWrap . now ( ) ;
325+ start = getLibuvNow ( ) ;
340326 try {
341327 ontimeout ( timer ) ;
342328 } finally {
@@ -474,7 +460,7 @@ function ontimeout(timer) {
474460}
475461
476462function rearm ( timer , start ) {
477- // // Do not re-arm unenroll'd or closed timers.
463+ // Do not re-arm unenroll'd or closed timers.
478464 if ( timer . _idleTimeout === - 1 )
479465 return ;
480466
0 commit comments