Skip to content

Commit 9256728

Browse files
authored
timers: refactor internal classes to ES2015 syntax
PR-URL: #37408 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 560cbc5 commit 9256728

1 file changed

Lines changed: 120 additions & 115 deletions

File tree

‎lib/internal/timers.js‎

Lines changed: 120 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ let timerListId = NumberMIN_SAFE_INTEGER;
136136

137137
constkRefed=Symbol('refed');
138138

139-
// Create a single linked list instance only once at startup
140-
constimmediateQueue=newImmediateList();
141-
142139
letnextExpiry=Infinity;
143140
letrefCount=0;
144141

@@ -161,140 +158,148 @@ function initAsyncResource(resource, type) {
161158
if(initHooksExist())
162159
emitInit(asyncId,type,triggerAsyncId,resource);
163160
}
164-
165-
// Timer constructor function.
166-
// The entire prototype is defined in lib/timers.js
167-
functionTimeout(callback,after,args,isRepeat,isRefed){
168-
after*=1;// Coalesce to number or NaN
169-
if(!(after>=1&&after<=TIMEOUT_MAX)){
170-
if(after>TIMEOUT_MAX){
171-
process.emitWarning(`${after} does not fit into`+
172-
' a 32-bit signed integer.'+
173-
'\nTimeout duration was set to 1.',
174-
'TimeoutOverflowWarning');
161+
classTimeout{
162+
// Timer constructor function.
163+
// The entire prototype is defined in lib/timers.js
164+
constructor(callback,after,args,isRepeat,isRefed){
165+
after*=1;// Coalesce to number or NaN
166+
if(!(after>=1&&after<=TIMEOUT_MAX)){
167+
if(after>TIMEOUT_MAX){
168+
process.emitWarning(`${after} does not fit into`+
169+
' a 32-bit signed integer.'+
170+
'\nTimeout duration was set to 1.',
171+
'TimeoutOverflowWarning');
172+
}
173+
after=1;// Schedule on next tick, follows browser behavior
175174
}
176-
after=1;// Schedule on next tick, follows browser behavior
177-
}
178175

179-
this._idleTimeout=after;
180-
this._idlePrev=this;
181-
this._idleNext=this;
182-
this._idleStart=null;
183-
// This must be set to null first to avoid function tracking
184-
// on the hidden class, revisit in V8 versions after 6.2
185-
this._onTimeout=null;
186-
this._onTimeout=callback;
187-
this._timerArgs=args;
188-
this._repeat=isRepeat ? after : null;
189-
this._destroyed=false;
190-
191-
if(isRefed)
192-
incRefCount();
193-
this[kRefed]=isRefed;
194-
this[kHasPrimitive]=false;
195-
196-
initAsyncResource(this,'Timeout');
197-
}
176+
this._idleTimeout=after;
177+
this._idlePrev=this;
178+
this._idleNext=this;
179+
this._idleStart=null;
180+
// This must be set to null first to avoid function tracking
181+
// on the hidden class, revisit in V8 versions after 6.2
182+
this._onTimeout=null;
183+
this._onTimeout=callback;
184+
this._timerArgs=args;
185+
this._repeat=isRepeat ? after : null;
186+
this._destroyed=false;
198187

199-
// Make sure the linked list only shows the minimal necessary information.
200-
Timeout.prototype[inspect.custom]=function(_,options){
201-
returninspect(this,{
202-
...options,
203-
// Only inspect one level.
204-
depth: 0,
205-
// It should not recurse.
206-
customInspect: false
207-
});
208-
};
188+
if(isRefed)
189+
incRefCount();
190+
this[kRefed]=isRefed;
191+
this[kHasPrimitive]=false;
209192

210-
Timeout.prototype.refresh=function(){
211-
if(this[kRefed])
212-
active(this);
213-
else
214-
unrefActive(this);
193+
initAsyncResource(this,'Timeout');
194+
}
215195

216-
returnthis;
217-
};
196+
// Make sure the linked list only shows the minimal necessary information.
197+
[inspect.custom](_,options){
198+
returninspect(this,{
199+
...options,
200+
// Only inspect one level.
201+
depth: 0,
202+
// It should not recurse.
203+
customInspect: false
204+
});
205+
}
218206

219-
Timeout.prototype.unref=function(){
220-
if(this[kRefed]){
221-
this[kRefed]=false;
222-
if(!this._destroyed)
223-
decRefCount();
207+
refresh(){
208+
if(this[kRefed])
209+
active(this);
210+
else
211+
unrefActive(this);
212+
213+
returnthis;
224214
}
225-
returnthis;
226-
};
227215

228-
Timeout.prototype.ref=function(){
229-
if(!this[kRefed]){
230-
this[kRefed]=true;
231-
if(!this._destroyed)
232-
incRefCount();
216+
unref(){
217+
if(this[kRefed]){
218+
this[kRefed]=false;
219+
if(!this._destroyed)
220+
decRefCount();
221+
}
222+
returnthis;
233223
}
234-
returnthis;
235-
};
236224

237-
Timeout.prototype.hasRef=function(){
238-
returnthis[kRefed];
239-
};
225+
ref(){
226+
if(!this[kRefed]){
227+
this[kRefed]=true;
228+
if(!this._destroyed)
229+
incRefCount();
230+
}
231+
returnthis;
232+
}
240233

241-
functionTimersList(expiry,msecs){
242-
this._idleNext=this;// Create the list with the linkedlist properties to
243-
this._idlePrev=this;// Prevent any unnecessary hidden class changes.
244-
this.expiry=expiry;
245-
this.id=timerListId++;
246-
this.msecs=msecs;
247-
this.priorityQueuePosition=null;
234+
hasRef(){
235+
returnthis[kRefed];
236+
}
248237
}
249238

250-
// Make sure the linked list only shows the minimal necessary information.
251-
TimersList.prototype[inspect.custom]=function(_,options){
252-
returninspect(this,{
253-
...options,
254-
// Only inspect one level.
255-
depth: 0,
256-
// It should not recurse.
257-
customInspect: false
258-
});
259-
};
239+
classTimersList{
240+
constructor(expiry,msecs){
241+
this._idleNext=this;// Create the list with the linkedlist properties to
242+
this._idlePrev=this;// Prevent any unnecessary hidden class changes.
243+
this.expiry=expiry;
244+
this.id=timerListId++;
245+
this.msecs=msecs;
246+
this.priorityQueuePosition=null;
247+
}
260248

261-
// A linked list for storing `setImmediate()` requests
262-
functionImmediateList(){
263-
this.head=null;
264-
this.tail=null;
249+
// Make sure the linked list only shows the minimal necessary information.
250+
[inspect.custom](_,options){
251+
returninspect(this,{
252+
...options,
253+
// Only inspect one level.
254+
depth: 0,
255+
// It should not recurse.
256+
customInspect: false
257+
});
258+
}
265259
}
266260

267-
// Appends an item to the end of the linked list, adjusting the current tail's
268-
// next pointer and the item's previous pointer where applicable
269-
ImmediateList.prototype.append=function(item){
270-
if(this.tail!==null){
271-
this.tail._idleNext=item;
272-
item._idlePrev=this.tail;
273-
}else{
274-
this.head=item;
261+
// A linked list for storing `setImmediate()` requests
262+
classImmediateList{
263+
constructor(){
264+
this.head=null;
265+
this.tail=null;
275266
}
276-
this.tail=item;
277-
};
278267

279-
// Removes an item from the linked list, adjusting the pointers of adjacent
280-
// items and the linked list's head or tail pointers as necessary
281-
ImmediateList.prototype.remove=function(item){
282-
if(item._idleNext){
283-
item._idleNext._idlePrev=item._idlePrev;
268+
// Appends an item to the end of the linked list, adjusting the current tail's
269+
// next pointer and the item's previous pointer where applicable
270+
append(item){
271+
if(this.tail!==null){
272+
this.tail._idleNext=item;
273+
item._idlePrev=this.tail;
274+
}else{
275+
this.head=item;
276+
}
277+
this.tail=item;
284278
}
285279

286-
if(item._idlePrev){
287-
item._idlePrev._idleNext=item._idleNext;
288-
}
280+
// Removes an item from the linked list, adjusting the pointers of adjacent
281+
// items and the linked list's head or tail pointers as necessary
282+
remove(item){
283+
if(item._idleNext){
284+
item._idleNext._idlePrev=item._idlePrev;
285+
}
289286

290-
if(item===this.head)
291-
this.head=item._idleNext;
292-
if(item===this.tail)
293-
this.tail=item._idlePrev;
287+
if(item._idlePrev){
288+
item._idlePrev._idleNext=item._idleNext;
289+
}
294290

295-
item._idleNext=null;
296-
item._idlePrev=null;
297-
};
291+
if(item===this.head)
292+
this.head=item._idleNext;
293+
if(item===this.tail)
294+
this.tail=item._idlePrev;
295+
296+
item._idleNext=null;
297+
item._idlePrev=null;
298+
}
299+
}
300+
301+
// Create a single linked list instance only once at startup
302+
constimmediateQueue=newImmediateList();
298303

299304
functionincRefCount(){
300305
if(refCount++===0)

0 commit comments

Comments
 (0)