Skip to content

Commit

Permalink
Simplify API for on and once
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent 507ec08 commit 7de5093
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 174 deletions.
5 changes: 2 additions & 3 deletions d.ts/homey.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ type Elements = Element[];
type EventDetails = { [key: string]: any };
type EventListener = (event: Event) => any;
type EventType = string;
type EventTypes = string;
type PredicateFn = (element: Element) => boolean;
type Selector = string;

Expand Down Expand Up @@ -56,6 +55,6 @@ export function remove(element: Element): boolean;
* Events
*/

export function on(element: Element, eventTypes: EventTypes, listener: EventListener): () => void;
export function once(element: Element, eventTypes: EventTypes, listener: EventListener): () => void;
export function on(element: Element, eventType: EventType, listener: EventListener): () => void;
export function once(element: Element, eventType: EventType, listener: EventListener): () => void;
export function dispatch(element: Element, eventType: EventType, listener: EventListener): boolean;
77 changes: 0 additions & 77 deletions spec/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,81 +76,4 @@ describe("on", () => {
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith(listenedEvent);
});

describe("when given space-separated events types list", () => {
it("adds listener for DOM events", () => {
on(subject, "click dblclick", listener);

expect(listener).not.toHaveBeenCalled();

const events = [createEvent("click"), createEvent("dblclick")];

events.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(events.length);
});

it("adds listener for custom events", () => {
on(subject, "foo bar", listener);

expect(listener).not.toHaveBeenCalled();

const events = [createEvent("foo"), createEvent("bar")];

events.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(events.length);
});

it("adds listener which called on each event trigger", () => {
on(subject, "click custom", listener);

expect(listener).not.toHaveBeenCalled();

for (let i = 0; i < 5; i += 1) {
const events = [createEvent("click"), createEvent("custom")];

// eslint-disable-next-line
events.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});
}

expect(listener).toHaveBeenCalledTimes(10);
});

it("returns function for removing listener for all event types", () => {
const off = on(subject, "click custom", listener);

expect(listener).not.toHaveBeenCalled();

const listenedEvents = [createEvent("click"), createEvent("custom")];

listenedEvents.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(listenedEvents.length);

off();

[createEvent("click"), createEvent("custom")].forEach(event => {
subject.dispatchEvent(event);
});

expect(listener).toHaveBeenCalledTimes(listenedEvents.length);
});
});
});
74 changes: 0 additions & 74 deletions spec/once.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,78 +72,4 @@ describe("once", () => {

expect(listener).not.toHaveBeenCalled();
});

describe("when given space-separated events types list", () => {
it("adds listener for DOM events", () => {
once(subject, "click dblclick", listener);

expect(listener).not.toHaveBeenCalled();

const events = [createEvent("click"), createEvent("dblclick")];

events.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(events.length);
});

it("adds listener for custom events", () => {
once(subject, "foo bar", listener);

expect(listener).not.toHaveBeenCalled();

const events = [createEvent("foo"), createEvent("bar")];

events.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(events.length);
});

it("adds listener which called on first trigger for each event", () => {
once(subject, "click custom", listener);

expect(listener).not.toHaveBeenCalled();

const listenedEvents = [createEvent("click"), createEvent("custom")];

listenedEvents.forEach(event => {
subject.dispatchEvent(event);

expect(listener).toHaveBeenCalledWith(event);
});

expect(listener).toHaveBeenCalledTimes(listenedEvents.length);

const unlistenedEvents = [createEvent("click"), createEvent("custom")];

unlistenedEvents.forEach(event => {
subject.dispatchEvent(event);
});

expect(listener).toHaveBeenCalledTimes(listenedEvents.length);
});

it("returns function for removing listener for all event types", () => {
const off = once(subject, "click custom", listener);

expect(listener).not.toHaveBeenCalled();

off();

const events = [createEvent("click"), createEvent("custom")];

events.forEach(event => {
subject.dispatchEvent(event);
});

expect(listener).not.toHaveBeenCalled();
});
});
});
30 changes: 10 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ type EventListener = (event: Event) => mixed;

type EventType = string;

type EventTypes = string;

type PredicateFn = (element: Element) => boolean;

type Selector = string;
Expand Down Expand Up @@ -206,36 +204,28 @@ function remove(element: Element): boolean {

function on(
element: Element,
eventTypes: EventTypes,
eventType: EventType,
listener: EventListener
): () => void {
const offs = eventTypes.split(" ").map(eventType => {
element.addEventListener(eventType, listener);

return () => element.removeEventListener(eventType, listener);
});
element.addEventListener(eventType, listener);

return () => offs.forEach(off => off());
return () => element.removeEventListener(eventType, listener);
}

function once(
element: Element,
eventTypes: EventTypes,
eventType: EventType,
listener: EventListener
): () => void {
const offs = eventTypes.split(" ").map(eventType => {
const wrappedListener = event => {
element.removeEventListener(eventType, wrappedListener);

listener(event);
};
function wrappedListener(event) {
element.removeEventListener(eventType, wrappedListener);

element.addEventListener(eventType, wrappedListener);
listener(event);
}

return () => element.removeEventListener(eventType, wrappedListener);
});
element.addEventListener(eventType, wrappedListener);

return () => offs.forEach(off => off());
return () => element.removeEventListener(eventType, wrappedListener);
}

function dispatch(
Expand Down

0 comments on commit 7de5093

Please sign in to comment.