MCPcopy Create free account
hub / github.com/angular/angular / patchEventTarget

Function patchEventTarget

packages/zone.js/lib/common/events.ts:201–906  ·  view source on GitHub ↗
(
  _global: any,
  api: _ZonePrivate,
  apis: any[],
  patchOptions?: PatchEventTargetOptions,
)

Source from the content-addressed store, hash-verified

199}
200
201export function patchEventTarget(
202 _global: any,
203 api: _ZonePrivate,
204 apis: any[],
205 patchOptions?: PatchEventTargetOptions,
206) {
207 const ADD_EVENT_LISTENER = (patchOptions && patchOptions.add) || ADD_EVENT_LISTENER_STR;
208 const REMOVE_EVENT_LISTENER = (patchOptions && patchOptions.rm) || REMOVE_EVENT_LISTENER_STR;
209
210 const LISTENERS_EVENT_LISTENER = (patchOptions && patchOptions.listeners) || 'eventListeners';
211 const REMOVE_ALL_LISTENERS_EVENT_LISTENER =
212 (patchOptions && patchOptions.rmAll) || 'removeAllListeners';
213
214 const zoneSymbolAddEventListener = zoneSymbol(ADD_EVENT_LISTENER);
215
216 const ADD_EVENT_LISTENER_SOURCE = '.' + ADD_EVENT_LISTENER + ':';
217
218 const PREPEND_EVENT_LISTENER = 'prependListener';
219 const PREPEND_EVENT_LISTENER_SOURCE = '.' + PREPEND_EVENT_LISTENER + ':';
220
221 const invokeTask = function (task: any, target: any, event: Event): Error | undefined {
222 // for better performance, check isRemoved which is set
223 // by removeEventListener
224 if (task.isRemoved) {
225 return;
226 }
227 const delegate = task.callback;
228 if (typeof delegate === 'object' && delegate.handleEvent) {
229 // create the bind version of handleEvent when invoke
230 task.callback = (event: Event) => delegate.handleEvent(event);
231 task.originalDelegate = delegate;
232 }
233 // invoke static task.invoke
234 // need to try/catch error here, otherwise, the error in one event listener
235 // will break the executions of the other event listeners. Also error will
236 // not remove the event listener when `once` options is true.
237 let error;
238 try {
239 task.invoke(task, target, [event]);
240 } catch (err: any) {
241 error = err;
242 }
243 const options = task.options;
244 if (options && typeof options === 'object' && options.once) {
245 // if options.once is true, after invoke once remove listener here
246 // only browser need to do this, nodejs eventEmitter will cal removeListener
247 // inside EventEmitter.once
248 const delegate = task.originalDelegate ? task.originalDelegate : task.callback;
249 target[REMOVE_EVENT_LISTENER].call(target, event.type, delegate, options);
250 }
251 return error;
252 };
253
254 function globalCallback(context: unknown, event: Event, isCapture: boolean) {
255 // https://github.com/angular/zone.js/issues/911, in IE, sometimes
256 // event will be undefined, so we need to use window.event
257 event = event || _global.event;
258 if (!event) {

Callers 2

patchEventEmitterMethodsFunction · 0.90
browser.spec.tsFile · 0.90

Calls 1

patchEventTargetMethodsFunction · 0.85

Tested by

no test coverage detected