1- // Flags: --expose-internals
1+ // Flags: --expose-internals --no-warnings
22'use strict' ;
33
44const common = require ( '../common' ) ;
@@ -248,6 +248,117 @@ async function nodeEventTarget() {
248248 clearInterval ( interval ) ;
249249}
250250
251+ async function abortableOnBefore ( ) {
252+ const ee = new EventEmitter ( ) ;
253+ const ac = new AbortController ( ) ;
254+ ac . abort ( ) ;
255+ [ 1 , { } , null , false , 'hi' ] . forEach ( ( signal ) => {
256+ assert . throws ( ( ) => on ( ee , 'foo' , { signal } ) , {
257+ code : 'ERR_INVALID_ARG_TYPE'
258+ } ) ;
259+ } ) ;
260+ assert . throws ( ( ) => on ( ee , 'foo' , { signal : ac . signal } ) , {
261+ name : 'AbortError'
262+ } ) ;
263+ }
264+
265+ async function eventTargetAbortableOnBefore ( ) {
266+ const et = new EventTarget ( ) ;
267+ const ac = new AbortController ( ) ;
268+ ac . abort ( ) ;
269+ [ 1 , { } , null , false , 'hi' ] . forEach ( ( signal ) => {
270+ assert . throws ( ( ) => on ( et , 'foo' , { signal } ) , {
271+ code : 'ERR_INVALID_ARG_TYPE'
272+ } ) ;
273+ } ) ;
274+ assert . throws ( ( ) => on ( et , 'foo' , { signal : ac . signal } ) , {
275+ name : 'AbortError'
276+ } ) ;
277+ }
278+
279+ async function abortableOnAfter ( ) {
280+ const ee = new EventEmitter ( ) ;
281+ const ac = new AbortController ( ) ;
282+
283+ const i = setInterval ( ( ) => ee . emit ( 'foo' , 'foo' ) , 10 ) ;
284+
285+ async function foo ( ) {
286+ for await ( const f of on ( ee , 'foo' , { signal : ac . signal } ) ) {
287+ assert . strictEqual ( f , 'foo' ) ;
288+ }
289+ }
290+
291+ foo ( ) . catch ( common . mustCall ( ( error ) => {
292+ assert . strictEqual ( error . name , 'AbortError' ) ;
293+ } ) ) . finally ( ( ) => {
294+ clearInterval ( i ) ;
295+ } ) ;
296+
297+ process . nextTick ( ( ) => ac . abort ( ) ) ;
298+ }
299+
300+ async function eventTargetAbortableOnAfter ( ) {
301+ const et = new EventTarget ( ) ;
302+ const ac = new AbortController ( ) ;
303+
304+ const i = setInterval ( ( ) => et . dispatchEvent ( new Event ( 'foo' ) ) , 10 ) ;
305+
306+ async function foo ( ) {
307+ for await ( const f of on ( et , 'foo' , { signal : ac . signal } ) ) {
308+ assert ( f ) ;
309+ }
310+ }
311+
312+ foo ( ) . catch ( common . mustCall ( ( error ) => {
313+ assert . strictEqual ( error . name , 'AbortError' ) ;
314+ } ) ) . finally ( ( ) => {
315+ clearInterval ( i ) ;
316+ } ) ;
317+
318+ process . nextTick ( ( ) => ac . abort ( ) ) ;
319+ }
320+
321+ async function eventTargetAbortableOnAfter2 ( ) {
322+ const et = new EventTarget ( ) ;
323+ const ac = new AbortController ( ) ;
324+
325+ const i = setInterval ( ( ) => et . dispatchEvent ( new Event ( 'foo' ) ) , 10 ) ;
326+
327+ async function foo ( ) {
328+ for await ( const f of on ( et , 'foo' , { signal : ac . signal } ) ) {
329+ assert ( f ) ;
330+ // Cancel after a single event has been triggered.
331+ ac . abort ( ) ;
332+ }
333+ }
334+
335+ foo ( ) . catch ( common . mustCall ( ( error ) => {
336+ assert . strictEqual ( error . name , 'AbortError' ) ;
337+ } ) ) . finally ( ( ) => {
338+ clearInterval ( i ) ;
339+ } ) ;
340+ }
341+
342+ async function abortableOnAfterDone ( ) {
343+ const ee = new EventEmitter ( ) ;
344+ const ac = new AbortController ( ) ;
345+
346+ const i = setInterval ( ( ) => ee . emit ( 'foo' , 'foo' ) , 1 ) ;
347+ let count = 0 ;
348+
349+ async function foo ( ) {
350+ for await ( const f of on ( ee , 'foo' , { signal : ac . signal } ) ) {
351+ assert . strictEqual ( f [ 0 ] , 'foo' ) ;
352+ if ( ++ count === 5 )
353+ break ;
354+ }
355+ ac . abort ( ) ; // No error will occur
356+ }
357+
358+ foo ( ) . finally ( ( ) => {
359+ clearInterval ( i ) ;
360+ } ) ;
361+ }
251362
252363async function run ( ) {
253364 const funcs = [
@@ -260,7 +371,13 @@ async function run() {
260371 iterableThrow ,
261372 eventTarget ,
262373 errorListenerCount ,
263- nodeEventTarget
374+ nodeEventTarget ,
375+ abortableOnBefore ,
376+ abortableOnAfter ,
377+ eventTargetAbortableOnBefore ,
378+ eventTargetAbortableOnAfter ,
379+ eventTargetAbortableOnAfter2 ,
380+ abortableOnAfterDone
264381 ] ;
265382
266383 for ( const fn of funcs ) {
0 commit comments