@@ -99,28 +99,6 @@ export type RendererInspectionConfig = $ReadOnly<{
9999 ) => void ,
100100} > ;
101101
102- // TODO?: find a better place for this type to live
103- export type EventListenerOptions = $ReadOnly < {
104- capture ?: boolean ,
105- once ?: boolean ,
106- passive ?: boolean ,
107- signal : mixed , // not yet implemented
108- } > ;
109- export type EventListenerRemoveOptions = $ReadOnly < {
110- capture ?: boolean ,
111- } > ;
112-
113- // TODO?: this will be changed in the future to be w3c-compatible and allow "EventListener" objects as well as functions.
114- export type EventListener = Function ;
115-
116- type InternalEventListeners = {
117- [ string ] : {
118- listener : EventListener ,
119- options : EventListenerOptions ,
120- invalidated : boolean ,
121- } [ ] ,
122- } ;
123-
124102// TODO: Remove this conditional once all changes have propagated.
125103if ( registerEventHandler ) {
126104 /**
@@ -137,7 +115,6 @@ class ReactFabricHostComponent {
137115 viewConfig : ViewConfig ;
138116 currentProps : Props ;
139117 _internalInstanceHandle : Object ;
140- _eventListeners : ?InternalEventListeners ;
141118
142119 constructor (
143120 tag : number ,
@@ -236,105 +213,6 @@ class ReactFabricHostComponent {
236213 setNativeProps ( stateNode . node , updatePayload ) ;
237214 }
238215 }
239-
240- // This API (addEventListener, removeEventListener) attempts to adhere to the
241- // w3 Level2 Events spec as much as possible, treating HostComponent as a DOM node.
242- //
243- // Unless otherwise noted, these methods should "just work" and adhere to the W3 specs.
244- // If they deviate in a way that is not explicitly noted here, you've found a bug!
245- //
246- // See:
247- // * https://www.w3.org/TR/DOM-Level-2-Events/events.html
248- // * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
249- // * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
250- //
251- // And notably, not implemented (yet?):
252- // * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
253- //
254- //
255- // Deviations from spec/TODOs:
256- // (1) listener must currently be a function, we do not support EventListener objects yet.
257- // (2) we do not support the `signal` option / AbortSignal yet
258- addEventListener_unstable (
259- eventType : string ,
260- listener : EventListener ,
261- options : EventListenerOptions | boolean ,
262- // $FlowFixMe[missing-local-annot]
263- ) {
264- if ( typeof eventType !== 'string' ) {
265- throw new Error ( 'addEventListener_unstable eventType must be a string' ) ;
266- }
267- if ( typeof listener !== 'function' ) {
268- throw new Error ( 'addEventListener_unstable listener must be a function' ) ;
269- }
270-
271- // The third argument is either boolean indicating "captures" or an object.
272- const optionsObj =
273- typeof options === 'object' && options !== null ? options : { } ;
274- const capture =
275- ( typeof options === 'boolean' ? options : optionsObj . capture ) || false ;
276- const once = optionsObj . once || false ;
277- const passive = optionsObj . passive || false ;
278- const signal = null ; // TODO: implement signal/AbortSignal
279-
280- /* $FlowFixMe the old version of Flow doesn't have a good way to define an
281- * empty exact object. */
282- const eventListeners : InternalEventListeners = this . _eventListeners || { } ;
283- if ( this . _eventListeners == null ) {
284- this . _eventListeners = eventListeners ;
285- }
286-
287- const namedEventListeners = eventListeners [ eventType ] || [ ] ;
288- if ( eventListeners [ eventType ] == null ) {
289- eventListeners [ eventType ] = namedEventListeners ;
290- }
291-
292- namedEventListeners . push ( {
293- listener : listener ,
294- invalidated : false ,
295- options : {
296- capture : capture ,
297- once : once ,
298- passive : passive ,
299- signal : signal ,
300- } ,
301- } ) ;
302- }
303-
304- // See https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
305- removeEventListener_unstable (
306- eventType : string ,
307- listener : EventListener ,
308- options : EventListenerRemoveOptions | boolean ,
309- ) {
310- // eventType and listener must be referentially equal to be removed from the listeners
311- // data structure, but in "options" we only check the `capture` flag, according to spec.
312- // That means if you add the same function as a listener with capture set to true and false,
313- // you must also call removeEventListener twice with capture set to true/false.
314- const optionsObj =
315- typeof options === 'object' && options !== null ? options : { } ;
316- const capture =
317- ( typeof options === 'boolean' ? options : optionsObj . capture ) || false ;
318-
319- // If there are no event listeners or named event listeners, we can bail early - our
320- // job is already done.
321- const eventListeners = this . _eventListeners ;
322- if ( ! eventListeners ) {
323- return;
324- }
325- const namedEventListeners = eventListeners [ eventType ] ;
326- if ( ! namedEventListeners ) {
327- return ;
328- }
329-
330- // TODO: optimize this path to make remove cheaper
331- eventListeners [ eventType ] = namedEventListeners . filter ( listenerObj => {
332- return ! (
333- listenerObj . listener === listener &&
334- listenerObj . options . capture === capture
335- ) ;
336- } ) ;
337- }
338216}
339217
340218// $FlowFixMe[class-object-subtyping] found when upgrading Flow
0 commit comments