@@ -81,14 +81,11 @@ const {
8181 Symbol,
8282} = primordials ;
8383
84+ const binding = internalBinding ( 'timers' ) ;
8485const {
85- scheduleTimer,
86- toggleTimerRef,
87- getLibuvNow,
8886 immediateInfo,
8987 timeoutInfo,
90- toggleImmediateRef
91- } = internalBinding ( 'timers' ) ;
88+ } = binding ;
9289
9390const {
9491 getDefaultTriggerAsyncId,
@@ -306,13 +303,17 @@ class ImmediateList {
306303const immediateQueue = new ImmediateList ( ) ;
307304
308305function incRefCount ( ) {
309- if ( timeoutInfo [ 0 ] ++ === 0 )
310- toggleTimerRef ( true ) ;
306+ if ( timeoutInfo [ 0 ] ++ === 0 ) {
307+ // We need to use the binding as the receiver for fast API calls.
308+ binding . toggleTimerRef ( true ) ;
309+ }
311310}
312311
313312function decRefCount ( ) {
314- if ( -- timeoutInfo [ 0 ] === 0 )
315- toggleTimerRef ( false ) ;
313+ if ( -- timeoutInfo [ 0 ] === 0 ) {
314+ // We need to use the binding as the receiver for fast API calls.
315+ binding . toggleTimerRef ( false ) ;
316+ }
316317}
317318
318319// Schedule or re-schedule a timer.
@@ -356,7 +357,8 @@ function insertGuarded(item, refed, start) {
356357 item [ kRefed ] = refed ;
357358}
358359
359- function insert ( item , msecs , start = getLibuvNow ( ) ) {
360+ // We need to use the binding as the receiver for fast API calls.
361+ function insert ( item , msecs , start = binding . getLibuvNow ( ) ) {
360362 // Truncate so that accuracy of sub-millisecond timers is not assumed.
361363 msecs = MathTrunc ( msecs ) ;
362364 item . _idleStart = start ;
@@ -370,7 +372,8 @@ function insert(item, msecs, start = getLibuvNow()) {
370372 timerListQueue . insert ( list ) ;
371373
372374 if ( nextExpiry > expiry ) {
373- scheduleTimer ( msecs ) ;
375+ // We need to use the binding as the receiver for fast API calls.
376+ binding . scheduleTimer ( msecs ) ;
374377 nextExpiry = expiry ;
375378 }
376379 }
@@ -559,8 +562,10 @@ function getTimerCallbacks(runNextTicks) {
559562 emitBefore ( asyncId , timer [ trigger_async_id_symbol ] , timer ) ;
560563
561564 let start ;
562- if ( timer . _repeat )
563- start = getLibuvNow ( ) ;
565+ if ( timer . _repeat ) {
566+ // We need to use the binding as the receiver for fast API calls.
567+ start = binding . getLibuvNow ( ) ;
568+ }
564569
565570 try {
566571 const args = timer . _timerArgs ;
@@ -627,17 +632,22 @@ class Immediate {
627632 ref ( ) {
628633 if ( this [ kRefed ] === false ) {
629634 this [ kRefed ] = true ;
630- if ( immediateInfo [ kRefCount ] ++ === 0 )
631- toggleImmediateRef ( true ) ;
635+
636+ if ( immediateInfo [ kRefCount ] ++ === 0 ) {
637+ // We need to use the binding as the receiver for fast API calls.
638+ binding . toggleImmediateRef ( true ) ;
639+ }
632640 }
633641 return this ;
634642 }
635643
636644 unref ( ) {
637645 if ( this [ kRefed ] === true ) {
638646 this [ kRefed ] = false ;
639- if ( -- immediateInfo [ kRefCount ] === 0 )
640- toggleImmediateRef ( false ) ;
647+ if ( -- immediateInfo [ kRefCount ] === 0 ) {
648+ // We need to use the binding as the receiver for fast API calls.
649+ binding . toggleImmediateRef ( false ) ;
650+ }
641651 }
642652 return this ;
643653 }
0 commit comments