Skip to content

Commit

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

declare type ManagerOptions = $Shape<{
path: string,
reconnection: boolean,
reconnectionAttempts: number,
reconnectionDelay: number,
reconnectionDelayMax: number,
randomizationFactor: number,
timeout: number,
parser: any
}>;

declare type LookupOptions = $Shape<{ forceNew: boolean }>;

declare type SocketOptions = $Shape<{ query: string }>;

declare class Emitter {
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 class Manager extends Emitter {
static (uri?: string, opts?: ManagerOptions): this;
reconnection(boolean): this;
reconnectionAttempts(number): this;
reconnectionDelay(number): this;
randomizationFactor(number): this;
reconnectionDelayMax(number): this;
timeout(number): this;
open(fn?: (err?: Error) => void, opts?: ManagerOptions): this;
connect(fn: any, opts: any): this;
socket(namespace: string, opts?: SocketOptions): Socket;
}

declare export class Socket extends Emitter {
static (io: Manager, nsp: string, opts?: SocketOptions): this;
open(): this;
connect(): this;
send(...args: mixed[]): this;
emit(event: string, ...args: mixed[]): this; // overrides Emitter#emit
close(): this;
disconnect(): this;
compress(boolean): this;
io: Manager;
}

declare export var protocol: 4;

declare type Lookup = (uri?: string, opts?: LookupOptions) => Socket;
declare export var connect: Lookup;
declare export default Lookup
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io from "socket.io-client";

io("foo");
io("foo", {});
io("foo", { forceNew: true });

const socket = io("foo")
.open()
.connect()
.send({ foo: "bar" }, { bar: "baz" })
.emit("bar", { foo: "bar" }) // overrides Emitter#emit
.close()
.disconnect()
.compress(true)
.on("foo", () => {})
.addEventListener("foo", () => {})
.once("foo", () => {})
.off("foo", () => {})
.removeListener("foo", () => {})
.removeAllListeners("foo", () => {})
.removeEventListener("foo", () => {});

const callbacks = socket.listeners("foo");
const hasFooListener = socket.hasListeners("foo");

const manager = socket.io
.reconnection(true)
.reconnectionAttempts(5)
.reconnectionDelay(5)
.randomizationFactor(5)
.reconnectionDelayMax(5)
.timeout(5)
.open(err => {}, {})
.connect(() => {}, {})
.on("foo", () => {})
.addEventListener("foo", () => {})
.once("foo", () => {})
.off("foo", () => {})
.removeListener("foo", () => {})
.removeAllListeners("foo", () => {})
.removeEventListener("foo", () => {})
.emit("foo", { bar: "baz" });

manager.socket("/baz", {});

// $ExpectError
io("foo", { invalid_key: 5 });

// $ExpectError
io(5);

0 comments on commit dfb2df4

Please sign in to comment.