MCPcopy
hub / github.com/angular/angular / patchEventTarget

Function patchEventTarget

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

Source from the content-addressed store, hash-verified

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

Callers 2

patchEventEmitterMethodsFunction · 0.90
browser.spec.tsFile · 0.90

Calls 1

patchEventTargetMethodsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…