Skip to content

Commit

Permalink
Add capturing support to delegate function
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent 1b35ee7 commit b53f527
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion d.ts/homey.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export function remove(element: Element): boolean;

export function on(element: Element, eventType: EventType, listener: EventListener, useCapture?: boolean): () => void;
export function once(element: Element, eventType: EventType, listener: EventListener, useCapture?: boolean): () => void;
export function delegate(element: Element, selector: Selector, eventType: EventType, listener: EventListener): () => void;
export function delegate(element: Element, selector: Selector, eventType: EventType, listener: EventListener, useCapture?: boolean): () => void;
export function dispatch(element: Element, eventType: EventType, listener: EventListener): boolean;
66 changes: 66 additions & 0 deletions spec/events/delegate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,70 @@ describe("delegate", () => {
expect(listener).not.toHaveBeenCalled();
});
});

describe("supports events capturing", () => {
it("doesn't use capturing by default", () => {
useFixture(`
<div class="parent">
<div class="root">
<div class="child"></child>
</div>
</div>
`);

const parent = document.querySelector(".parent");
const subject = document.querySelector(".root");
const child = document.querySelector(".child");
const parentListener = jasmine.createSpy("parentListener");
const subjectListener = jasmine.createSpy("subjectListener");

delegate(parent, ".child", "click", parentListener);
delegate(subject, ".child", "click", subjectListener);

expect(parentListener).not.toHaveBeenCalled();
expect(subjectListener).not.toHaveBeenCalled();

const event = createEvent("click");

child.dispatchEvent(event);

expect(parentListener).toHaveBeenCalledTimes(1);
expect(parentListener).toHaveBeenCalledWith(event);
expect(subjectListener).toHaveBeenCalledTimes(1);
expect(subjectListener).toHaveBeenCalledWith(event);
expect(subjectListener).toHaveBeenCalledBefore(parentListener);
});

it("uses capturing when flag given", () => {
useFixture(`
<div class="parent">
<div class="root">
<div class="child"></child>
</div>
</div>
`);

const parent = document.querySelector(".parent");
const subject = document.querySelector(".root");
const child = document.querySelector(".child");
const parentListener = jasmine.createSpy("parentListener");
const subjectListener = jasmine.createSpy("subjectListener");

delegate(parent, ".child", "click", parentListener, true);
delegate(subject, ".child", "click", subjectListener, true);

expect(parentListener).not.toHaveBeenCalled();
expect(subjectListener).not.toHaveBeenCalled();

const event = createEvent("click");

child.dispatchEvent(event);

expect(parentListener).toHaveBeenCalledTimes(1);
expect(parentListener).toHaveBeenCalledWith(event);
expect(subjectListener).toHaveBeenCalledTimes(1);
expect(subjectListener).toHaveBeenCalledWith(event);
expect(parentListener).toHaveBeenCalledBefore(subjectListener);
});
});
});
5 changes: 3 additions & 2 deletions src/events/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function delegate(
element: Element,
selector: Selector,
eventType: EventType,
listener: EventListener
listener: EventListener,
useCapture?: boolean = false
): () => void {
function wrappedListener(event) {
if (!(event.target instanceof Element)) {
Expand All @@ -35,7 +36,7 @@ function delegate(
}
}

return on(element, eventType, wrappedListener);
return on(element, eventType, wrappedListener, useCapture);
}

export default delegate;

0 comments on commit b53f527

Please sign in to comment.