Skip to content

Commit

Permalink
Move once to events namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 4, 2017
1 parent a94acf0 commit 385700e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion spec/once.spec.js → spec/events/once.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { once } from "../src";
import { once } from "../../src";

describe("once", () => {
let subject;
Expand Down
21 changes: 21 additions & 0 deletions src/events/once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* @flow */

import type { EventListener, EventType } from "../types";

function once(
element: Element,
eventType: EventType,
listener: EventListener
): () => void {
function wrappedListener(event) {
element.removeEventListener(eventType, wrappedListener);

listener(event);
}

element.addEventListener(eventType, wrappedListener);

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

export default once;
21 changes: 2 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* @flow */

import type { EventType, Selector } from "./types";
import type { EventListener, EventType, Selector } from "./types";

import { html, body } from "./aliases";
import addClass from "./css/add-class";
import hasClass from "./css/has-class";
import removeClass from "./css/remove-class";
import toggleClass from "./css/toggle-class";
import on from "./events/on";
import once from "./events/once";
import query from "./queries/query";
import queryAll from "./queries/query-all";
import matches from "./traversing/matches";
Expand All @@ -20,8 +21,6 @@ import parentsBy from "./traversing/parents-by";

type EventDetails = { [key: string]: mixed };

type EventListener = (event: Event) => mixed;

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

/* Dataset */
Expand Down Expand Up @@ -117,22 +116,6 @@ function remove(element: Element): boolean {

/* Events */

function once(
element: Element,
eventType: EventType,
listener: EventListener
): () => void {
function wrappedListener(event) {
element.removeEventListener(eventType, wrappedListener);

listener(event);
}

element.addEventListener(eventType, wrappedListener);

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

function delegate(
element: Element,
selector: Selector,
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export type CSSClass = string;

export type Elements = Array<Element>;

export type EventListener = (event: Event) => mixed;

export type EventType = string;

export type Predicate = (element: Element) => boolean;
Expand Down

0 comments on commit 385700e

Please sign in to comment.