24
24
const {
25
25
ObjectCreate,
26
26
MathTrunc,
27
- Promise ,
28
- SymbolToPrimitive
27
+ Object ,
28
+ SymbolToPrimitive,
29
29
} = primordials ;
30
30
31
- const {
32
- codes : { ERR_INVALID_ARG_TYPE }
33
- } = require ( 'internal/errors' ) ;
34
-
35
- let DOMException ;
36
-
37
31
const {
38
32
immediateInfo,
39
33
toggleImmediateRef
@@ -42,14 +36,14 @@ const L = require('internal/linkedlist');
42
36
const {
43
37
async_id_symbol,
44
38
Timeout,
39
+ Immediate,
45
40
decRefCount,
46
41
immediateInfoFields : {
47
42
kCount,
48
43
kRefCount
49
44
} ,
50
45
kRefed,
51
46
kHasPrimitive,
52
- initAsyncResource,
53
47
getTimerDuration,
54
48
timerListMap,
55
49
timerListQueue,
@@ -67,6 +61,8 @@ let debug = require('internal/util/debuglog').debuglog('timer', (fn) => {
67
61
} ) ;
68
62
const { validateCallback } = require ( 'internal/validators' ) ;
69
63
64
+ let timersPromises ;
65
+
70
66
const {
71
67
destroyHooksExist,
72
68
// The needed emit*() functions.
@@ -135,12 +131,6 @@ function enroll(item, msecs) {
135
131
* DOM-style timers
136
132
*/
137
133
138
- function lazyDOMException ( message ) {
139
- if ( DOMException === undefined )
140
- DOMException = internalBinding ( 'messaging' ) . DOMException ;
141
- return new DOMException ( message ) ;
142
- }
143
-
144
134
function setTimeout ( callback , after , arg1 , arg2 , arg3 ) {
145
135
validateCallback ( callback ) ;
146
136
@@ -171,44 +161,14 @@ function setTimeout(callback, after, arg1, arg2, arg3) {
171
161
return timeout ;
172
162
}
173
163
174
- setTimeout [ customPromisify ] = function ( after , value , options = { } ) {
175
- const args = value !== undefined ? [ value ] : value ;
176
- if ( options == null || typeof options !== 'object' ) {
177
- return Promise . reject (
178
- new ERR_INVALID_ARG_TYPE (
179
- 'options' ,
180
- 'Object' ,
181
- options ) ) ;
164
+ Object . defineProperty ( setTimeout , customPromisify , {
165
+ enumerable : true ,
166
+ get ( ) {
167
+ if ( ! timersPromises )
168
+ timersPromises = require ( 'internal/timers/promises' ) ;
169
+ return timersPromises . setTimeout ;
182
170
}
183
- const { signal } = options ;
184
- if ( signal !== undefined &&
185
- ( signal === null ||
186
- typeof signal !== 'object' ||
187
- ! ( 'aborted' in signal ) ) ) {
188
- return Promise . reject (
189
- new ERR_INVALID_ARG_TYPE (
190
- 'options.signal' ,
191
- 'AbortSignal' ,
192
- signal ) ) ;
193
- }
194
- // TODO(@jasnell): If a decision is made that this cannot be backported
195
- // to 12.x, then this can be converted to use optional chaining to
196
- // simplify the check.
197
- if ( signal && signal . aborted )
198
- return Promise . reject ( lazyDOMException ( 'AbortError' ) ) ;
199
- return new Promise ( ( resolve , reject ) => {
200
- const timeout = new Timeout ( resolve , after , args , false , true ) ;
201
- insert ( timeout , timeout . _idleTimeout ) ;
202
- if ( signal ) {
203
- signal . addEventListener ( 'abort' , ( ) => {
204
- if ( ! timeout . _destroyed ) {
205
- clearTimeout ( timeout ) ;
206
- reject ( lazyDOMException ( 'AbortError' ) ) ;
207
- }
208
- } , { once : true } ) ;
209
- }
210
- } ) ;
211
- } ;
171
+ } ) ;
212
172
213
173
function clearTimeout ( timer ) {
214
174
if ( timer && timer . _onTimeout ) {
@@ -276,46 +236,6 @@ Timeout.prototype[SymbolToPrimitive] = function() {
276
236
return id ;
277
237
} ;
278
238
279
- const Immediate = class Immediate {
280
- constructor ( callback , args ) {
281
- this . _idleNext = null ;
282
- this . _idlePrev = null ;
283
- this . _onImmediate = callback ;
284
- this . _argv = args ;
285
- this . _destroyed = false ;
286
- this [ kRefed ] = false ;
287
-
288
- initAsyncResource ( this , 'Immediate' ) ;
289
-
290
- this . ref ( ) ;
291
- immediateInfo [ kCount ] ++ ;
292
-
293
- immediateQueue . append ( this ) ;
294
- }
295
-
296
- ref ( ) {
297
- if ( this [ kRefed ] === false ) {
298
- this [ kRefed ] = true ;
299
- if ( immediateInfo [ kRefCount ] ++ === 0 )
300
- toggleImmediateRef ( true ) ;
301
- }
302
- return this ;
303
- }
304
-
305
- unref ( ) {
306
- if ( this [ kRefed ] === true ) {
307
- this [ kRefed ] = false ;
308
- if ( -- immediateInfo [ kRefCount ] === 0 )
309
- toggleImmediateRef ( false ) ;
310
- }
311
- return this ;
312
- }
313
-
314
- hasRef ( ) {
315
- return ! ! this [ kRefed ] ;
316
- }
317
- } ;
318
-
319
239
function setImmediate ( callback , arg1 , arg2 , arg3 ) {
320
240
validateCallback ( callback ) ;
321
241
@@ -342,42 +262,15 @@ function setImmediate(callback, arg1, arg2, arg3) {
342
262
return new Immediate ( callback , args ) ;
343
263
}
344
264
345
- setImmediate [ customPromisify ] = function ( value , options = { } ) {
346
- if ( options == null || typeof options !== 'object' ) {
347
- return Promise . reject (
348
- new ERR_INVALID_ARG_TYPE (
349
- 'options' ,
350
- 'Object' ,
351
- options ) ) ;
265
+ Object . defineProperty ( setImmediate , customPromisify , {
266
+ enumerable : true ,
267
+ get ( ) {
268
+ if ( ! timersPromises )
269
+ timersPromises = require ( 'internal/timers/promises' ) ;
270
+ return timersPromises . setImmediate ;
352
271
}
353
- const { signal } = options ;
354
- if ( signal !== undefined &&
355
- ( signal === null ||
356
- typeof signal !== 'object' ||
357
- ! ( 'aborted' in signal ) ) ) {
358
- return Promise . reject (
359
- new ERR_INVALID_ARG_TYPE (
360
- 'options.signal' ,
361
- 'AbortSignal' ,
362
- signal ) ) ;
363
- }
364
- // TODO(@jasnell): If a decision is made that this cannot be backported
365
- // to 12.x, then this can be converted to use optional chaining to
366
- // simplify the check.
367
- if ( signal && signal . aborted )
368
- return Promise . reject ( lazyDOMException ( 'AbortError' ) ) ;
369
- return new Promise ( ( resolve , reject ) => {
370
- const immediate = new Immediate ( resolve , [ value ] ) ;
371
- if ( signal ) {
372
- signal . addEventListener ( 'abort' , ( ) => {
373
- if ( ! immediate . _destroyed ) {
374
- clearImmediate ( immediate ) ;
375
- reject ( lazyDOMException ( 'AbortError' ) ) ;
376
- }
377
- } , { once : true } ) ;
378
- }
379
- } ) ;
380
- } ;
272
+ } ) ;
273
+
381
274
382
275
function clearImmediate ( immediate ) {
383
276
if ( ! immediate || immediate . _destroyed )
0 commit comments