23
23
24
24
const {
25
25
MathTrunc,
26
- Promise ,
26
+ Object ,
27
27
} = primordials ;
28
28
29
- const {
30
- codes : { ERR_INVALID_ARG_TYPE }
31
- } = require ( 'internal/errors' ) ;
32
-
33
- let DOMException ;
34
-
35
29
const {
36
30
immediateInfo,
37
31
toggleImmediateRef
@@ -40,13 +34,13 @@ const L = require('internal/linkedlist');
40
34
const {
41
35
async_id_symbol,
42
36
Timeout,
37
+ Immediate,
43
38
decRefCount,
44
39
immediateInfoFields : {
45
40
kCount,
46
41
kRefCount
47
42
} ,
48
43
kRefed,
49
- initAsyncResource,
50
44
getTimerDuration,
51
45
timerListMap,
52
46
timerListQueue,
@@ -64,6 +58,8 @@ let debug = require('internal/util/debuglog').debuglog('timer', (fn) => {
64
58
} ) ;
65
59
const { validateCallback } = require ( 'internal/validators' ) ;
66
60
61
+ let timersPromises ;
62
+
67
63
const {
68
64
destroyHooksExist,
69
65
// The needed emit*() functions.
@@ -124,12 +120,6 @@ function enroll(item, msecs) {
124
120
* DOM-style timers
125
121
*/
126
122
127
- function lazyDOMException ( message ) {
128
- if ( DOMException === undefined )
129
- DOMException = internalBinding ( 'messaging' ) . DOMException ;
130
- return new DOMException ( message ) ;
131
- }
132
-
133
123
function setTimeout ( callback , after , arg1 , arg2 , arg3 ) {
134
124
validateCallback ( callback ) ;
135
125
@@ -160,44 +150,14 @@ function setTimeout(callback, after, arg1, arg2, arg3) {
160
150
return timeout ;
161
151
}
162
152
163
- setTimeout [ customPromisify ] = function ( after , value , options = { } ) {
164
- const args = value !== undefined ? [ value ] : value ;
165
- if ( options == null || typeof options !== 'object' ) {
166
- return Promise . reject (
167
- new ERR_INVALID_ARG_TYPE (
168
- 'options' ,
169
- 'Object' ,
170
- options ) ) ;
153
+ Object . defineProperty ( setTimeout , customPromisify , {
154
+ enumerable : true ,
155
+ get ( ) {
156
+ if ( ! timersPromises )
157
+ timersPromises = require ( 'timers/promises' ) ;
158
+ return timersPromises . setTimeout ;
171
159
}
172
- const { signal } = options ;
173
- if ( signal !== undefined &&
174
- ( signal === null ||
175
- typeof signal !== 'object' ||
176
- ! ( 'aborted' in signal ) ) ) {
177
- return Promise . reject (
178
- new ERR_INVALID_ARG_TYPE (
179
- 'options.signal' ,
180
- 'AbortSignal' ,
181
- signal ) ) ;
182
- }
183
- // TODO(@jasnell): If a decision is made that this cannot be backported
184
- // to 12.x, then this can be converted to use optional chaining to
185
- // simplify the check.
186
- if ( signal && signal . aborted )
187
- return Promise . reject ( lazyDOMException ( 'AbortError' ) ) ;
188
- return new Promise ( ( resolve , reject ) => {
189
- const timeout = new Timeout ( resolve , after , args , false , true ) ;
190
- insert ( timeout , timeout . _idleTimeout ) ;
191
- if ( signal ) {
192
- signal . addEventListener ( 'abort' , ( ) => {
193
- if ( ! timeout . _destroyed ) {
194
- clearTimeout ( timeout ) ;
195
- reject ( lazyDOMException ( 'AbortError' ) ) ;
196
- }
197
- } , { once : true } ) ;
198
- }
199
- } ) ;
200
- } ;
160
+ } ) ;
201
161
202
162
function clearTimeout ( timer ) {
203
163
if ( timer && timer . _onTimeout ) {
@@ -248,46 +208,6 @@ Timeout.prototype.close = function() {
248
208
return this ;
249
209
} ;
250
210
251
- const Immediate = class Immediate {
252
- constructor ( callback , args ) {
253
- this . _idleNext = null ;
254
- this . _idlePrev = null ;
255
- this . _onImmediate = callback ;
256
- this . _argv = args ;
257
- this . _destroyed = false ;
258
- this [ kRefed ] = false ;
259
-
260
- initAsyncResource ( this , 'Immediate' ) ;
261
-
262
- this . ref ( ) ;
263
- immediateInfo [ kCount ] ++ ;
264
-
265
- immediateQueue . append ( this ) ;
266
- }
267
-
268
- ref ( ) {
269
- if ( this [ kRefed ] === false ) {
270
- this [ kRefed ] = true ;
271
- if ( immediateInfo [ kRefCount ] ++ === 0 )
272
- toggleImmediateRef ( true ) ;
273
- }
274
- return this ;
275
- }
276
-
277
- unref ( ) {
278
- if ( this [ kRefed ] === true ) {
279
- this [ kRefed ] = false ;
280
- if ( -- immediateInfo [ kRefCount ] === 0 )
281
- toggleImmediateRef ( false ) ;
282
- }
283
- return this ;
284
- }
285
-
286
- hasRef ( ) {
287
- return ! ! this [ kRefed ] ;
288
- }
289
- } ;
290
-
291
211
function setImmediate ( callback , arg1 , arg2 , arg3 ) {
292
212
validateCallback ( callback ) ;
293
213
@@ -314,42 +234,15 @@ function setImmediate(callback, arg1, arg2, arg3) {
314
234
return new Immediate ( callback , args ) ;
315
235
}
316
236
317
- setImmediate [ customPromisify ] = function ( value , options = { } ) {
318
- if ( options == null || typeof options !== 'object' ) {
319
- return Promise . reject (
320
- new ERR_INVALID_ARG_TYPE (
321
- 'options' ,
322
- 'Object' ,
323
- options ) ) ;
237
+ Object . defineProperty ( setImmediate , customPromisify , {
238
+ enumerable : true ,
239
+ get ( ) {
240
+ if ( ! timersPromises )
241
+ timersPromises = require ( 'timers/promises' ) ;
242
+ return timersPromises . setImmediate ;
324
243
}
325
- const { signal } = options ;
326
- if ( signal !== undefined &&
327
- ( signal === null ||
328
- typeof signal !== 'object' ||
329
- ! ( 'aborted' in signal ) ) ) {
330
- return Promise . reject (
331
- new ERR_INVALID_ARG_TYPE (
332
- 'options.signal' ,
333
- 'AbortSignal' ,
334
- signal ) ) ;
335
- }
336
- // TODO(@jasnell): If a decision is made that this cannot be backported
337
- // to 12.x, then this can be converted to use optional chaining to
338
- // simplify the check.
339
- if ( signal && signal . aborted )
340
- return Promise . reject ( lazyDOMException ( 'AbortError' ) ) ;
341
- return new Promise ( ( resolve , reject ) => {
342
- const immediate = new Immediate ( resolve , [ value ] ) ;
343
- if ( signal ) {
344
- signal . addEventListener ( 'abort' , ( ) => {
345
- if ( ! immediate . _destroyed ) {
346
- clearImmediate ( immediate ) ;
347
- reject ( lazyDOMException ( 'AbortError' ) ) ;
348
- }
349
- } , { once : true } ) ;
350
- }
351
- } ) ;
352
- } ;
244
+ } ) ;
245
+
353
246
354
247
function clearImmediate ( immediate ) {
355
248
if ( ! immediate || immediate . _destroyed )
0 commit comments