Skip to content

Commit

Permalink
Add library definition for component-emitter (flow-typed#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdogan authored and gantoine committed Nov 13, 2017
1 parent 55bef7f commit 021abc9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare module "component-emitter" {
declare type Callback = (...args: mixed[]) => void;

declare class Emitter {
static (base?: {}): this;
on(event: string, cb: Callback): this;
addEventListener(event: string, cb: Callback): this;

once(event: string, cb: Callback): this;

off(event: string, cb: Callback): this;
removeListener(event: string, cb: Callback): this;
removeAllListeners(event: string, cb: Callback): this;
removeEventListener(event: string, cb: Callback): this;

emit(event: string, payload: mixed): this;
listeners(event: string): Callback[];
hasListeners(event: string): boolean;
}

declare export default typeof Emitter
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Emitter from "component-emitter";

Emitter();
Emitter({});
Emitter({ foo: "bar" });

const e = Emitter();
e ===
e
.on("on", ok => {})
.addEventListener("addEventListener", ok => {})
.once("once", ok => {})
.off("off", ok => {})
.removeListener("removeListener", ok => {})
.removeAllListeners("removeAllListeners", ok => {})
.removeEventListener("removeEventListener", ok => {})
.emit("foo", { bar: "baz" });

e.listeners("foo").length;
e.hasListeners("foo");

// $ExpectError
Emitter(5);

0 comments on commit 021abc9

Please sign in to comment.