Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept EventListener callbacks #175

Merged
merged 5 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
private _zonePatched = false;
// for addEventListener/removeEventListener state
private _wrappedListeners = new WeakMap<
Function,
Function | EventListenerObject,
Map<string, Map<HTMLElement, Function>>
>();
// for event bubbling
Expand Down Expand Up @@ -178,7 +178,7 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
private addPatchedListener(
on: HTMLElement,
type: string,
listener: Function,
listener: Function | EventListenerObject,
wrappedListener: Function
): boolean {
let listener2Type = this._wrappedListeners.get(listener);
Expand All @@ -204,7 +204,7 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
private removePatchedListener(
on: HTMLElement,
type: string,
listener: Function
listener: Function | EventListenerObject
): Function | undefined {
const listener2Type = this._wrappedListeners.get(listener);
if (!listener2Type) {
Expand All @@ -227,6 +227,19 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
return patched;
}

// utility method to deal with the Function|EventListener nature of addEventListener
private _invokeListener(
listener: Function | EventListenerObject,
target: any,
dyladan marked this conversation as resolved.
Show resolved Hide resolved
args: any[]
): any {
if (typeof listener === 'function') {
return listener.apply(target, args);
} else {
return listener.handleEvent(args[0]);
}
}

/**
* This patches the addEventListener of HTMLElement to be able to
* auto instrument the click events
Expand Down Expand Up @@ -258,13 +271,13 @@ export class UserInteractionPlugin extends BasePlugin<unknown> {
plugin._eventsSpanMap.set(event, span);
}
return plugin._tracer.withSpan(span, () => {
const result = listener.apply(target, args);
const result = plugin._invokeListener(listener, target, args);
// no zone so end span immediately
span.end();
return result;
});
} else {
return listener.apply(target, args);
return plugin._invokeListener(listener, target, args);
}
};
if (plugin.addPatchedListener(this, type, listener, patchedListener)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ describe('UserInteractionPlugin', () => {
assert.strictEqual(callCount, 3);
});

it('should handle EventListener callbacks', () => {
let callCount = 0;
const listener = {
handleEvent(evt: Event) {
if (evt) {
callCount++;
}
},
};
document.body.addEventListener('EventListenerEvent', listener);
document.body.dispatchEvent(new Event('EventListenerEvent'));
assert.strictEqual(callCount, 1);
callCount = 0;
document.body.removeEventListener('EventListenerEvent', listener);
document.body.dispatchEvent(new Event('EventListenerEvent'));
assert.strictEqual(callCount, 0);
});

it('should handle task without async operation', () => {
fakeInteraction();
assert.equal(exportSpy.args.length, 1, 'should export one span');
Expand Down