|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | + |
| 5 | +const{ |
| 6 | + strictEqual, |
| 7 | +}=require('assert'); |
| 8 | + |
| 9 | +// Manually ported from: wpt@dom/events/AddEventListenerOptions-signal.any.js |
| 10 | + |
| 11 | +{ |
| 12 | +// Passing an AbortSignal to addEventListener does not prevent |
| 13 | +// removeEventListener |
| 14 | +letcount=0; |
| 15 | +functionhandler(){ |
| 16 | +count++; |
| 17 | +} |
| 18 | +constet=newEventTarget(); |
| 19 | +constcontroller=newAbortController(); |
| 20 | +et.addEventListener('test',handler,{signal: controller.signal}); |
| 21 | +et.dispatchEvent(newEvent('test')); |
| 22 | +strictEqual(count,1,'Adding a signal still adds a listener'); |
| 23 | +et.dispatchEvent(newEvent('test')); |
| 24 | +strictEqual(count,2,'The listener was not added with the once flag'); |
| 25 | +controller.abort(); |
| 26 | +et.dispatchEvent(newEvent('test')); |
| 27 | +strictEqual(count,2,'Aborting on the controller removes the listener'); |
| 28 | +et.addEventListener('test',handler,{signal: controller.signal}); |
| 29 | +et.dispatchEvent(newEvent('test')); |
| 30 | +strictEqual(count,2,'Passing an aborted signal never adds the handler'); |
| 31 | +} |
| 32 | + |
| 33 | +{ |
| 34 | +// Passing an AbortSignal to addEventListener works with the once flag |
| 35 | +letcount=0; |
| 36 | +functionhandler(){ |
| 37 | +count++; |
| 38 | +} |
| 39 | +constet=newEventTarget(); |
| 40 | +constcontroller=newAbortController(); |
| 41 | +et.addEventListener('test',handler,{signal: controller.signal}); |
| 42 | +et.removeEventListener('test',handler); |
| 43 | +et.dispatchEvent(newEvent('test')); |
| 44 | +strictEqual(count,0,'The listener was still removed'); |
| 45 | +} |
| 46 | + |
| 47 | +{ |
| 48 | +// Removing a once listener works with a passed signal |
| 49 | +letcount=0; |
| 50 | +functionhandler(){ |
| 51 | +count++; |
| 52 | +} |
| 53 | +constet=newEventTarget(); |
| 54 | +constcontroller=newAbortController(); |
| 55 | +constoptions={signal: controller.signal,once: true}; |
| 56 | +et.addEventListener('test',handler,options); |
| 57 | +controller.abort(); |
| 58 | +et.dispatchEvent(newEvent('test')); |
| 59 | +strictEqual(count,0,'The listener was still removed'); |
| 60 | +} |
| 61 | + |
| 62 | +{ |
| 63 | +letcount=0; |
| 64 | +functionhandler(){ |
| 65 | +count++; |
| 66 | +} |
| 67 | +constet=newEventTarget(); |
| 68 | +constcontroller=newAbortController(); |
| 69 | +constoptions={signal: controller.signal,once: true}; |
| 70 | +et.addEventListener('test',handler,options); |
| 71 | +et.removeEventListener('test',handler); |
| 72 | +et.dispatchEvent(newEvent('test')); |
| 73 | +strictEqual(count,0,'The listener was still removed'); |
| 74 | +} |
| 75 | + |
| 76 | +{ |
| 77 | +// Passing an AbortSignal to multiple listeners |
| 78 | +letcount=0; |
| 79 | +functionhandler(){ |
| 80 | +count++; |
| 81 | +} |
| 82 | +constet=newEventTarget(); |
| 83 | +constcontroller=newAbortController(); |
| 84 | +constoptions={signal: controller.signal,once: true}; |
| 85 | +et.addEventListener('first',handler,options); |
| 86 | +et.addEventListener('second',handler,options); |
| 87 | +controller.abort(); |
| 88 | +et.dispatchEvent(newEvent('first')); |
| 89 | +et.dispatchEvent(newEvent('second')); |
| 90 | +strictEqual(count,0,'The listener was still removed'); |
| 91 | +} |
| 92 | + |
| 93 | +{ |
| 94 | +// Passing an AbortSignal to addEventListener works with the capture flag |
| 95 | +letcount=0; |
| 96 | +functionhandler(){ |
| 97 | +count++; |
| 98 | +} |
| 99 | +constet=newEventTarget(); |
| 100 | +constcontroller=newAbortController(); |
| 101 | +constoptions={signal: controller.signal,capture: true}; |
| 102 | +et.addEventListener('test',handler,options); |
| 103 | +controller.abort(); |
| 104 | +et.dispatchEvent(newEvent('test')); |
| 105 | +strictEqual(count,0,'The listener was still removed'); |
| 106 | +} |
| 107 | + |
| 108 | +{ |
| 109 | +// Aborting from a listener does not call future listeners |
| 110 | +letcount=0; |
| 111 | +functionhandler(){ |
| 112 | +count++; |
| 113 | +} |
| 114 | +constet=newEventTarget(); |
| 115 | +constcontroller=newAbortController(); |
| 116 | +constoptions={signal: controller.signal}; |
| 117 | +et.addEventListener('test',()=>{ |
| 118 | +controller.abort(); |
| 119 | +},options); |
| 120 | +et.addEventListener('test',handler,options); |
| 121 | +et.dispatchEvent(newEvent('test')); |
| 122 | +strictEqual(count,0,'The listener was still removed'); |
| 123 | +} |
| 124 | + |
| 125 | +{ |
| 126 | +// Adding then aborting a listener in another listener does not call it |
| 127 | +letcount=0; |
| 128 | +functionhandler(){ |
| 129 | +count++; |
| 130 | +} |
| 131 | +constet=newEventTarget(); |
| 132 | +constcontroller=newAbortController(); |
| 133 | +et.addEventListener('test',()=>{ |
| 134 | +et.addEventListener('test',handler,{signal: controller.signal}); |
| 135 | +controller.abort(); |
| 136 | +},{signal: controller.signal}); |
| 137 | +et.dispatchEvent(newEvent('test')); |
| 138 | +strictEqual(count,0,'The listener was still removed'); |
| 139 | +} |
| 140 | + |
| 141 | +{ |
| 142 | +// Aborting from a nested listener should remove it |
| 143 | +constet=newEventTarget(); |
| 144 | +constac=newAbortController(); |
| 145 | +letcount=0; |
| 146 | +et.addEventListener('foo',()=>{ |
| 147 | +et.addEventListener('foo',()=>{ |
| 148 | +count++; |
| 149 | +if(count>5)ac.abort(); |
| 150 | +et.dispatchEvent(newEvent('foo')); |
| 151 | +},{signal: ac.signal}); |
| 152 | +et.dispatchEvent(newEvent('foo')); |
| 153 | +},{once: true}); |
| 154 | +et.dispatchEvent(newEvent('foo')); |
| 155 | +} |
0 commit comments