events.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. /**
  9. * @fileoverview
  10. * @suppress {missingRequire}
  11. */
  12. import {ADD_EVENT_LISTENER_STR, attachOriginToPatched, FALSE_STR, isNode, ObjectGetPrototypeOf, REMOVE_EVENT_LISTENER_STR, TRUE_STR, ZONE_SYMBOL_PREFIX, zoneSymbol} from './utils';
  13. /** @internal **/
  14. interface EventTaskData extends TaskData {
  15. // use global callback or not
  16. readonly useG?: boolean;
  17. }
  18. let passiveSupported = false;
  19. if (typeof window !== 'undefined') {
  20. try {
  21. const options = Object.defineProperty({}, 'passive', {
  22. get: function() {
  23. passiveSupported = true;
  24. }
  25. });
  26. window.addEventListener('test', options, options);
  27. window.removeEventListener('test', options, options);
  28. } catch (err) {
  29. passiveSupported = false;
  30. }
  31. }
  32. // an identifier to tell ZoneTask do not create a new invoke closure
  33. const OPTIMIZED_ZONE_EVENT_TASK_DATA: EventTaskData = {
  34. useG: true
  35. };
  36. export const zoneSymbolEventNames: any = {};
  37. export const globalSources: any = {};
  38. const EVENT_NAME_SYMBOL_REGX = /^__zone_symbol__(\w+)(true|false)$/;
  39. const IMMEDIATE_PROPAGATION_SYMBOL = ('__zone_symbol__propagationStopped');
  40. export interface PatchEventTargetOptions {
  41. // validateHandler
  42. vh?: (nativeDelegate: any, delegate: any, target: any, args: any) => boolean;
  43. // addEventListener function name
  44. add?: string;
  45. // removeEventListener function name
  46. rm?: string;
  47. // prependEventListener function name
  48. prepend?: string;
  49. // listeners function name
  50. listeners?: string;
  51. // removeAllListeners function name
  52. rmAll?: string;
  53. // useGlobalCallback flag
  54. useG?: boolean;
  55. // check duplicate flag when addEventListener
  56. chkDup?: boolean;
  57. // return target flag when addEventListener
  58. rt?: boolean;
  59. // event compare handler
  60. diff?: (task: any, delegate: any) => boolean;
  61. // support passive or not
  62. supportPassive?: boolean;
  63. // get string from eventName (in nodejs, eventName maybe Symbol)
  64. eventNameToString?: (eventName: any) => string;
  65. }
  66. export function patchEventTarget(
  67. _global: any, apis: any[], patchOptions?: PatchEventTargetOptions) {
  68. const ADD_EVENT_LISTENER = (patchOptions && patchOptions.add) || ADD_EVENT_LISTENER_STR;
  69. const REMOVE_EVENT_LISTENER = (patchOptions && patchOptions.rm) || REMOVE_EVENT_LISTENER_STR;
  70. const LISTENERS_EVENT_LISTENER = (patchOptions && patchOptions.listeners) || 'eventListeners';
  71. const REMOVE_ALL_LISTENERS_EVENT_LISTENER =
  72. (patchOptions && patchOptions.rmAll) || 'removeAllListeners';
  73. const zoneSymbolAddEventListener = zoneSymbol(ADD_EVENT_LISTENER);
  74. const ADD_EVENT_LISTENER_SOURCE = '.' + ADD_EVENT_LISTENER + ':';
  75. const PREPEND_EVENT_LISTENER = 'prependListener';
  76. const PREPEND_EVENT_LISTENER_SOURCE = '.' + PREPEND_EVENT_LISTENER + ':';
  77. const invokeTask = function(task: any, target: any, event: Event) {
  78. // for better performance, check isRemoved which is set
  79. // by removeEventListener
  80. if (task.isRemoved) {
  81. return;
  82. }
  83. const delegate = task.callback;
  84. if (typeof delegate === 'object' && delegate.handleEvent) {
  85. // create the bind version of handleEvent when invoke
  86. task.callback = (event: Event) => delegate.handleEvent(event);
  87. task.originalDelegate = delegate;
  88. }
  89. // invoke static task.invoke
  90. task.invoke(task, target, [event]);
  91. const options = task.options;
  92. if (options && typeof options === 'object' && options.once) {
  93. // if options.once is true, after invoke once remove listener here
  94. // only browser need to do this, nodejs eventEmitter will cal removeListener
  95. // inside EventEmitter.once
  96. const delegate = task.originalDelegate ? task.originalDelegate : task.callback;
  97. target[REMOVE_EVENT_LISTENER].call(target, event.type, delegate, options);
  98. }
  99. };
  100. // global shared zoneAwareCallback to handle all event callback with capture = false
  101. const globalZoneAwareCallback = function(event: Event) {
  102. // https://github.com/angular/zone.js/issues/911, in IE, sometimes
  103. // event will be undefined, so we need to use window.event
  104. event = event || _global.event;
  105. if (!event) {
  106. return;
  107. }
  108. // event.target is needed for Samsung TV and SourceBuffer
  109. // || global is needed https://github.com/angular/zone.js/issues/190
  110. const target: any = this || event.target || _global;
  111. const tasks = target[zoneSymbolEventNames[event.type][FALSE_STR]];
  112. if (tasks) {
  113. // invoke all tasks which attached to current target with given event.type and capture = false
  114. // for performance concern, if task.length === 1, just invoke
  115. if (tasks.length === 1) {
  116. invokeTask(tasks[0], target, event);
  117. } else {
  118. // https://github.com/angular/zone.js/issues/836
  119. // copy the tasks array before invoke, to avoid
  120. // the callback will remove itself or other listener
  121. const copyTasks = tasks.slice();
  122. for (let i = 0; i < copyTasks.length; i++) {
  123. if (event && (event as any)[IMMEDIATE_PROPAGATION_SYMBOL] === true) {
  124. break;
  125. }
  126. invokeTask(copyTasks[i], target, event);
  127. }
  128. }
  129. }
  130. };
  131. // global shared zoneAwareCallback to handle all event callback with capture = true
  132. const globalZoneAwareCaptureCallback = function(event: Event) {
  133. // https://github.com/angular/zone.js/issues/911, in IE, sometimes
  134. // event will be undefined, so we need to use window.event
  135. event = event || _global.event;
  136. if (!event) {
  137. return;
  138. }
  139. // event.target is needed for Samsung TV and SourceBuffer
  140. // || global is needed https://github.com/angular/zone.js/issues/190
  141. const target: any = this || event.target || _global;
  142. const tasks = target[zoneSymbolEventNames[event.type][TRUE_STR]];
  143. if (tasks) {
  144. // invoke all tasks which attached to current target with given event.type and capture = false
  145. // for performance concern, if task.length === 1, just invoke
  146. if (tasks.length === 1) {
  147. invokeTask(tasks[0], target, event);
  148. } else {
  149. // https://github.com/angular/zone.js/issues/836
  150. // copy the tasks array before invoke, to avoid
  151. // the callback will remove itself or other listener
  152. const copyTasks = tasks.slice();
  153. for (let i = 0; i < copyTasks.length; i++) {
  154. if (event && (event as any)[IMMEDIATE_PROPAGATION_SYMBOL] === true) {
  155. break;
  156. }
  157. invokeTask(copyTasks[i], target, event);
  158. }
  159. }
  160. }
  161. };
  162. function patchEventTargetMethods(obj: any, patchOptions?: PatchEventTargetOptions) {
  163. if (!obj) {
  164. return false;
  165. }
  166. let useGlobalCallback = true;
  167. if (patchOptions && patchOptions.useG !== undefined) {
  168. useGlobalCallback = patchOptions.useG;
  169. }
  170. const validateHandler = patchOptions && patchOptions.vh;
  171. let checkDuplicate = true;
  172. if (patchOptions && patchOptions.chkDup !== undefined) {
  173. checkDuplicate = patchOptions.chkDup;
  174. }
  175. let returnTarget = false;
  176. if (patchOptions && patchOptions.rt !== undefined) {
  177. returnTarget = patchOptions.rt;
  178. }
  179. let proto = obj;
  180. while (proto && !proto.hasOwnProperty(ADD_EVENT_LISTENER)) {
  181. proto = ObjectGetPrototypeOf(proto);
  182. }
  183. if (!proto && obj[ADD_EVENT_LISTENER]) {
  184. // somehow we did not find it, but we can see it. This happens on IE for Window properties.
  185. proto = obj;
  186. }
  187. if (!proto) {
  188. return false;
  189. }
  190. if (proto[zoneSymbolAddEventListener]) {
  191. return false;
  192. }
  193. const eventNameToString = patchOptions && patchOptions.eventNameToString;
  194. // a shared global taskData to pass data for scheduleEventTask
  195. // so we do not need to create a new object just for pass some data
  196. const taskData: any = {};
  197. const nativeAddEventListener = proto[zoneSymbolAddEventListener] = proto[ADD_EVENT_LISTENER];
  198. const nativeRemoveEventListener = proto[zoneSymbol(REMOVE_EVENT_LISTENER)] =
  199. proto[REMOVE_EVENT_LISTENER];
  200. const nativeListeners = proto[zoneSymbol(LISTENERS_EVENT_LISTENER)] =
  201. proto[LISTENERS_EVENT_LISTENER];
  202. const nativeRemoveAllListeners = proto[zoneSymbol(REMOVE_ALL_LISTENERS_EVENT_LISTENER)] =
  203. proto[REMOVE_ALL_LISTENERS_EVENT_LISTENER];
  204. let nativePrependEventListener: any;
  205. if (patchOptions && patchOptions.prepend) {
  206. nativePrependEventListener = proto[zoneSymbol(patchOptions.prepend)] =
  207. proto[patchOptions.prepend];
  208. }
  209. function checkIsPassive(task: Task) {
  210. if (!passiveSupported && typeof taskData.options !== 'boolean' &&
  211. typeof taskData.options !== 'undefined' && taskData.options !== null) {
  212. // options is a non-null non-undefined object
  213. // passive is not supported
  214. // don't pass options as object
  215. // just pass capture as a boolean
  216. (task as any).options = !!taskData.options.capture;
  217. taskData.options = (task as any).options;
  218. }
  219. }
  220. const customScheduleGlobal = function(task: Task) {
  221. // if there is already a task for the eventName + capture,
  222. // just return, because we use the shared globalZoneAwareCallback here.
  223. if (taskData.isExisting) {
  224. return;
  225. }
  226. checkIsPassive(task);
  227. return nativeAddEventListener.call(
  228. taskData.target, taskData.eventName,
  229. taskData.capture ? globalZoneAwareCaptureCallback : globalZoneAwareCallback,
  230. taskData.options);
  231. };
  232. const customCancelGlobal = function(task: any) {
  233. // if task is not marked as isRemoved, this call is directly
  234. // from Zone.prototype.cancelTask, we should remove the task
  235. // from tasksList of target first
  236. if (!task.isRemoved) {
  237. const symbolEventNames = zoneSymbolEventNames[task.eventName];
  238. let symbolEventName;
  239. if (symbolEventNames) {
  240. symbolEventName = symbolEventNames[task.capture ? TRUE_STR : FALSE_STR];
  241. }
  242. const existingTasks = symbolEventName && task.target[symbolEventName];
  243. if (existingTasks) {
  244. for (let i = 0; i < existingTasks.length; i++) {
  245. const existingTask = existingTasks[i];
  246. if (existingTask === task) {
  247. existingTasks.splice(i, 1);
  248. // set isRemoved to data for faster invokeTask check
  249. task.isRemoved = true;
  250. if (existingTasks.length === 0) {
  251. // all tasks for the eventName + capture have gone,
  252. // remove globalZoneAwareCallback and remove the task cache from target
  253. task.allRemoved = true;
  254. task.target[symbolEventName] = null;
  255. }
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. // if all tasks for the eventName + capture have gone,
  262. // we will really remove the global event callback,
  263. // if not, return
  264. if (!task.allRemoved) {
  265. return;
  266. }
  267. return nativeRemoveEventListener.call(
  268. task.target, task.eventName,
  269. task.capture ? globalZoneAwareCaptureCallback : globalZoneAwareCallback, task.options);
  270. };
  271. const customScheduleNonGlobal = function(task: Task) {
  272. checkIsPassive(task);
  273. return nativeAddEventListener.call(
  274. taskData.target, taskData.eventName, task.invoke, taskData.options);
  275. };
  276. const customSchedulePrepend = function(task: Task) {
  277. return nativePrependEventListener.call(
  278. taskData.target, taskData.eventName, task.invoke, taskData.options);
  279. };
  280. const customCancelNonGlobal = function(task: any) {
  281. return nativeRemoveEventListener.call(task.target, task.eventName, task.invoke, task.options);
  282. };
  283. const customSchedule = useGlobalCallback ? customScheduleGlobal : customScheduleNonGlobal;
  284. const customCancel = useGlobalCallback ? customCancelGlobal : customCancelNonGlobal;
  285. const compareTaskCallbackVsDelegate = function(task: any, delegate: any) {
  286. const typeOfDelegate = typeof delegate;
  287. return (typeOfDelegate === 'function' && task.callback === delegate) ||
  288. (typeOfDelegate === 'object' && task.originalDelegate === delegate);
  289. };
  290. const compare =
  291. (patchOptions && patchOptions.diff) ? patchOptions.diff : compareTaskCallbackVsDelegate;
  292. const blackListedEvents: string[] = (Zone as any)[Zone.__symbol__('BLACK_LISTED_EVENTS')];
  293. const makeAddListener = function(
  294. nativeListener: any, addSource: string, customScheduleFn: any, customCancelFn: any,
  295. returnTarget = false, prepend = false) {
  296. return function() {
  297. const target = this || _global;
  298. const eventName = arguments[0];
  299. let delegate = arguments[1];
  300. if (!delegate) {
  301. return nativeListener.apply(this, arguments);
  302. }
  303. if (isNode && eventName === 'uncaughtException') {
  304. // don't patch uncaughtException of nodejs to prevent endless loop
  305. return nativeListener.apply(this, arguments);
  306. }
  307. // don't create the bind delegate function for handleEvent
  308. // case here to improve addEventListener performance
  309. // we will create the bind delegate when invoke
  310. let isHandleEvent = false;
  311. if (typeof delegate !== 'function') {
  312. if (!delegate.handleEvent) {
  313. return nativeListener.apply(this, arguments);
  314. }
  315. isHandleEvent = true;
  316. }
  317. if (validateHandler && !validateHandler(nativeListener, delegate, target, arguments)) {
  318. return;
  319. }
  320. const options = arguments[2];
  321. if (blackListedEvents) {
  322. // check black list
  323. for (let i = 0; i < blackListedEvents.length; i++) {
  324. if (eventName === blackListedEvents[i]) {
  325. return nativeListener.apply(this, arguments);
  326. }
  327. }
  328. }
  329. let capture;
  330. let once = false;
  331. if (options === undefined) {
  332. capture = false;
  333. } else if (options === true) {
  334. capture = true;
  335. } else if (options === false) {
  336. capture = false;
  337. } else {
  338. capture = options ? !!options.capture : false;
  339. once = options ? !!options.once : false;
  340. }
  341. const zone = Zone.current;
  342. const symbolEventNames = zoneSymbolEventNames[eventName];
  343. let symbolEventName;
  344. if (!symbolEventNames) {
  345. // the code is duplicate, but I just want to get some better performance
  346. const falseEventName =
  347. (eventNameToString ? eventNameToString(eventName) : eventName) + FALSE_STR;
  348. const trueEventName =
  349. (eventNameToString ? eventNameToString(eventName) : eventName) + TRUE_STR;
  350. const symbol = ZONE_SYMBOL_PREFIX + falseEventName;
  351. const symbolCapture = ZONE_SYMBOL_PREFIX + trueEventName;
  352. zoneSymbolEventNames[eventName] = {};
  353. zoneSymbolEventNames[eventName][FALSE_STR] = symbol;
  354. zoneSymbolEventNames[eventName][TRUE_STR] = symbolCapture;
  355. symbolEventName = capture ? symbolCapture : symbol;
  356. } else {
  357. symbolEventName = symbolEventNames[capture ? TRUE_STR : FALSE_STR];
  358. }
  359. let existingTasks = target[symbolEventName];
  360. let isExisting = false;
  361. if (existingTasks) {
  362. // already have task registered
  363. isExisting = true;
  364. if (checkDuplicate) {
  365. for (let i = 0; i < existingTasks.length; i++) {
  366. if (compare(existingTasks[i], delegate)) {
  367. // same callback, same capture, same event name, just return
  368. return;
  369. }
  370. }
  371. }
  372. } else {
  373. existingTasks = target[symbolEventName] = [];
  374. }
  375. let source;
  376. const constructorName = target.constructor['name'];
  377. const targetSource = globalSources[constructorName];
  378. if (targetSource) {
  379. source = targetSource[eventName];
  380. }
  381. if (!source) {
  382. source = constructorName + addSource +
  383. (eventNameToString ? eventNameToString(eventName) : eventName);
  384. }
  385. // do not create a new object as task.data to pass those things
  386. // just use the global shared one
  387. taskData.options = options;
  388. if (once) {
  389. // if addEventListener with once options, we don't pass it to
  390. // native addEventListener, instead we keep the once setting
  391. // and handle ourselves.
  392. taskData.options.once = false;
  393. }
  394. taskData.target = target;
  395. taskData.capture = capture;
  396. taskData.eventName = eventName;
  397. taskData.isExisting = isExisting;
  398. const data = useGlobalCallback ? OPTIMIZED_ZONE_EVENT_TASK_DATA : undefined;
  399. // keep taskData into data to allow onScheduleEventTask to access the task information
  400. if (data) {
  401. (data as any).taskData = taskData;
  402. }
  403. const task: any =
  404. zone.scheduleEventTask(source, delegate, data, customScheduleFn, customCancelFn);
  405. // should clear taskData.target to avoid memory leak
  406. // issue, https://github.com/angular/angular/issues/20442
  407. taskData.target = null;
  408. // need to clear up taskData because it is a global object
  409. if (data) {
  410. (data as any).taskData = null;
  411. }
  412. // have to save those information to task in case
  413. // application may call task.zone.cancelTask() directly
  414. if (once) {
  415. options.once = true;
  416. }
  417. if (!(!passiveSupported && typeof task.options === 'boolean')) {
  418. // if not support passive, and we pass an option object
  419. // to addEventListener, we should save the options to task
  420. task.options = options;
  421. }
  422. task.target = target;
  423. task.capture = capture;
  424. task.eventName = eventName;
  425. if (isHandleEvent) {
  426. // save original delegate for compare to check duplicate
  427. (task as any).originalDelegate = delegate;
  428. }
  429. if (!prepend) {
  430. existingTasks.push(task);
  431. } else {
  432. existingTasks.unshift(task);
  433. }
  434. if (returnTarget) {
  435. return target;
  436. }
  437. };
  438. };
  439. proto[ADD_EVENT_LISTENER] = makeAddListener(
  440. nativeAddEventListener, ADD_EVENT_LISTENER_SOURCE, customSchedule, customCancel,
  441. returnTarget);
  442. if (nativePrependEventListener) {
  443. proto[PREPEND_EVENT_LISTENER] = makeAddListener(
  444. nativePrependEventListener, PREPEND_EVENT_LISTENER_SOURCE, customSchedulePrepend,
  445. customCancel, returnTarget, true);
  446. }
  447. proto[REMOVE_EVENT_LISTENER] = function() {
  448. const target = this || _global;
  449. const eventName = arguments[0];
  450. const options = arguments[2];
  451. let capture;
  452. if (options === undefined) {
  453. capture = false;
  454. } else if (options === true) {
  455. capture = true;
  456. } else if (options === false) {
  457. capture = false;
  458. } else {
  459. capture = options ? !!options.capture : false;
  460. }
  461. const delegate = arguments[1];
  462. if (!delegate) {
  463. return nativeRemoveEventListener.apply(this, arguments);
  464. }
  465. if (validateHandler &&
  466. !validateHandler(nativeRemoveEventListener, delegate, target, arguments)) {
  467. return;
  468. }
  469. const symbolEventNames = zoneSymbolEventNames[eventName];
  470. let symbolEventName;
  471. if (symbolEventNames) {
  472. symbolEventName = symbolEventNames[capture ? TRUE_STR : FALSE_STR];
  473. }
  474. const existingTasks = symbolEventName && target[symbolEventName];
  475. if (existingTasks) {
  476. for (let i = 0; i < existingTasks.length; i++) {
  477. const existingTask = existingTasks[i];
  478. if (compare(existingTask, delegate)) {
  479. existingTasks.splice(i, 1);
  480. // set isRemoved to data for faster invokeTask check
  481. (existingTask as any).isRemoved = true;
  482. if (existingTasks.length === 0) {
  483. // all tasks for the eventName + capture have gone,
  484. // remove globalZoneAwareCallback and remove the task cache from target
  485. (existingTask as any).allRemoved = true;
  486. target[symbolEventName] = null;
  487. }
  488. existingTask.zone.cancelTask(existingTask);
  489. if (returnTarget) {
  490. return target;
  491. }
  492. return;
  493. }
  494. }
  495. }
  496. // issue 930, didn't find the event name or callback
  497. // from zone kept existingTasks, the callback maybe
  498. // added outside of zone, we need to call native removeEventListener
  499. // to try to remove it.
  500. return nativeRemoveEventListener.apply(this, arguments);
  501. };
  502. proto[LISTENERS_EVENT_LISTENER] = function() {
  503. const target = this || _global;
  504. const eventName = arguments[0];
  505. const listeners: any[] = [];
  506. const tasks =
  507. findEventTasks(target, eventNameToString ? eventNameToString(eventName) : eventName);
  508. for (let i = 0; i < tasks.length; i++) {
  509. const task: any = tasks[i];
  510. let delegate = task.originalDelegate ? task.originalDelegate : task.callback;
  511. listeners.push(delegate);
  512. }
  513. return listeners;
  514. };
  515. proto[REMOVE_ALL_LISTENERS_EVENT_LISTENER] = function() {
  516. const target = this || _global;
  517. const eventName = arguments[0];
  518. if (!eventName) {
  519. const keys = Object.keys(target);
  520. for (let i = 0; i < keys.length; i++) {
  521. const prop = keys[i];
  522. const match = EVENT_NAME_SYMBOL_REGX.exec(prop);
  523. let evtName = match && match[1];
  524. // in nodejs EventEmitter, removeListener event is
  525. // used for monitoring the removeListener call,
  526. // so just keep removeListener eventListener until
  527. // all other eventListeners are removed
  528. if (evtName && evtName !== 'removeListener') {
  529. this[REMOVE_ALL_LISTENERS_EVENT_LISTENER].call(this, evtName);
  530. }
  531. }
  532. // remove removeListener listener finally
  533. this[REMOVE_ALL_LISTENERS_EVENT_LISTENER].call(this, 'removeListener');
  534. } else {
  535. const symbolEventNames = zoneSymbolEventNames[eventName];
  536. if (symbolEventNames) {
  537. const symbolEventName = symbolEventNames[FALSE_STR];
  538. const symbolCaptureEventName = symbolEventNames[TRUE_STR];
  539. const tasks = target[symbolEventName];
  540. const captureTasks = target[symbolCaptureEventName];
  541. if (tasks) {
  542. const removeTasks = tasks.slice();
  543. for (let i = 0; i < removeTasks.length; i++) {
  544. const task = removeTasks[i];
  545. let delegate = task.originalDelegate ? task.originalDelegate : task.callback;
  546. this[REMOVE_EVENT_LISTENER].call(this, eventName, delegate, task.options);
  547. }
  548. }
  549. if (captureTasks) {
  550. const removeTasks = captureTasks.slice();
  551. for (let i = 0; i < removeTasks.length; i++) {
  552. const task = removeTasks[i];
  553. let delegate = task.originalDelegate ? task.originalDelegate : task.callback;
  554. this[REMOVE_EVENT_LISTENER].call(this, eventName, delegate, task.options);
  555. }
  556. }
  557. }
  558. }
  559. if (returnTarget) {
  560. return this;
  561. }
  562. };
  563. // for native toString patch
  564. attachOriginToPatched(proto[ADD_EVENT_LISTENER], nativeAddEventListener);
  565. attachOriginToPatched(proto[REMOVE_EVENT_LISTENER], nativeRemoveEventListener);
  566. if (nativeRemoveAllListeners) {
  567. attachOriginToPatched(proto[REMOVE_ALL_LISTENERS_EVENT_LISTENER], nativeRemoveAllListeners);
  568. }
  569. if (nativeListeners) {
  570. attachOriginToPatched(proto[LISTENERS_EVENT_LISTENER], nativeListeners);
  571. }
  572. return true;
  573. }
  574. let results: any[] = [];
  575. for (let i = 0; i < apis.length; i++) {
  576. results[i] = patchEventTargetMethods(apis[i], patchOptions);
  577. }
  578. return results;
  579. }
  580. export function findEventTasks(target: any, eventName: string): Task[] {
  581. const foundTasks: any[] = [];
  582. for (let prop in target) {
  583. const match = EVENT_NAME_SYMBOL_REGX.exec(prop);
  584. let evtName = match && match[1];
  585. if (evtName && (!eventName || evtName === eventName)) {
  586. const tasks: any = target[prop];
  587. if (tasks) {
  588. for (let i = 0; i < tasks.length; i++) {
  589. foundTasks.push(tasks[i]);
  590. }
  591. }
  592. }
  593. }
  594. return foundTasks;
  595. }
  596. export function patchEventPrototype(global: any, api: _ZonePrivate) {
  597. const Event = global['Event'];
  598. if (Event && Event.prototype) {
  599. api.patchMethod(
  600. Event.prototype, 'stopImmediatePropagation',
  601. (delegate: Function) => function(self: any, args: any[]) {
  602. self[IMMEDIATE_PROPAGATION_SYMBOL] = true;
  603. // we need to call the native stopImmediatePropagation
  604. // in case in some hybrid application, some part of
  605. // application will be controlled by zone, some are not
  606. delegate && delegate.apply(self, args);
  607. });
  608. }
  609. }