From c3293bdfe3d6536ca8b124ff2db1dddb124578b0 Mon Sep 17 00:00:00 2001 From: Ben O'Sullivan Date: Mon, 16 Jul 2018 05:34:22 +1000 Subject: [PATCH] Added typings for websocket-async (#27293) * Added typings for websocket-async * Updated in accordance with linter * Update tsconfig.json --- types/websocket-async/index.d.ts | 87 +++++++++++++++++++ types/websocket-async/tsconfig.json | 26 ++++++ types/websocket-async/tslint.json | 1 + .../websocket-async/websocket-async-tests.ts | 4 + 4 files changed, 118 insertions(+) create mode 100644 types/websocket-async/index.d.ts create mode 100644 types/websocket-async/tsconfig.json create mode 100644 types/websocket-async/tslint.json create mode 100644 types/websocket-async/websocket-async-tests.ts diff --git a/types/websocket-async/index.d.ts b/types/websocket-async/index.d.ts new file mode 100644 index 0000000000..36bb12e08e --- /dev/null +++ b/types/websocket-async/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for websocket-async 1.3 +// Project: https://github.com/jcao219/websocket-async +// Definitions by: Ben O'Sullivan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Typescript Version: 2.7 + +/** + * An asynchronous WebSocket client. + * @example + * // Set up connection. + * const webSocketClient = new WebSocketClient; + * // Connect. + * await webSocketClient.connect('ws://www.example.com/'); + * // Send is synchronous. + * webSocketClient.send('Hello!'); + * // Receive is asynchronous. + * console.log(await webSocketClient.receive()); + * // See if there are any more messages received. + * if (webSocketClient.dataAvailable !== 0) { + * console.log(await webSocketClient.receive()); + * } + * // Close the connection. + * await webSocketClient.disconnect(); + */ +declare class WebSocketClient { + _socket: WebSocket; + + _closeEvent: CloseEvent | null; + + _receiveCallbacksQueue: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }>; + + _receiveDataQueue: any[]; + + constructor(); + + /** + * Whether a connection is currently open. + * @returns true if the connection is open. + */ + connected(): boolean; + + /** + * The number of messages available to receive. + * @returns The number of queued messages that can be retrieved with {@link #receive} + */ + dataAvailable(): number; + + /** + * Sets up a WebSocket connection to specified url. Resolves when the + * connection is established. Can be called again to reconnect to any url. + */ + connect(url: string, protocols?: string): Promise; + + /** + * Send data through the websocket. + * Must be connected. See {@link #connected}. + */ + send(data: any): void; + + /** + * Asynchronously receive data from the websocket. + * Resolves immediately if there is buffered, unreceived data. + * Otherwise, resolves with the next rececived message, + * or rejects if disconnected. + * @returns A promise that resolves with the data received. + */ + receive(): Promise; + + /** + * Initiates the close handshake if there is an active connection. + * Returns a promise that will never reject. + * The promise resolves once the WebSocket connection is closed. + */ + disconnect(code?: number, reason?: string): Promise; + + /** + * Sets up the event listeners, which do the bulk of the work. + */ + private _setupListenersOnConnect(): Promise; + + /** + * Resets the receive arrays and close events, called in the constructor + */ + private _reset(): void; +} + +export default WebSocketClient; diff --git a/types/websocket-async/tsconfig.json b/types/websocket-async/tsconfig.json new file mode 100644 index 0000000000..78b5de7ea6 --- /dev/null +++ b/types/websocket-async/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5", + "es2015.promise", + "webworker" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "websocket-async-tests.ts" + ] +} diff --git a/types/websocket-async/tslint.json b/types/websocket-async/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/websocket-async/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/websocket-async/websocket-async-tests.ts b/types/websocket-async/websocket-async-tests.ts new file mode 100644 index 0000000000..f32192be95 --- /dev/null +++ b/types/websocket-async/websocket-async-tests.ts @@ -0,0 +1,4 @@ +import WebSocketClient from 'websocket-async'; + +const connection = new WebSocketClient(); +connection.connect('');