<commit_before>// @flow

/**
 * An asynchronous WebSocket client.
 * @example
 * // Set up connection.
 * const webSocketClient = new WebSocketClient;
 * // Connect.
 * await webSocketClient.connect("ws://www.example.com/", 9000);
 * // Send is synchronous.
 * webSocketClient.send("Hello!");
 * // Receive is asynchronous.
 * console.log(await webSocketClient.receive());
 * // Close the connection.
 * await webSocketClient.disconnect();
 */
export default class WebSocketClient {

    /** @private */
    _socket: WebSocket;

    /** @private */
    _closeEvent: ?CloseEvent;
    
    // The following data structures enable the asynchronous operation 
    // of receive() and _setupListenersOnConnect().

    /** @private */
    _receiveCallbacksQueue: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }>;

    /** @private */
    _receiveDataQueue: Array<any>;
    
    /** @public */
    constructor() {
        this._reset();
    }

    /* public properties */
    
    /**
     * Whether a connection is currently active.
     * @returns true if the connection is active.
     */
    get connected(): boolean {
        // Checking != null also checks against undefined.
        return this._socket != null && this._socket.readyState === WebSocket.OPEN;
    }
    
    /**
     * The number of messages available to receive.
     * @returns The number of queued messages that can be retrieved with {@link WebSocketClient#receive}
     */
    get dataAvailable(): number {
        return this._receiveDataQueue.length;
    }

    /**
     * Sets up a WebSocket connection to specified url. Resolves when the 
     * connection is established. Can be called again to reconnect to any url.
     */
    async connect(url: string, protocols?: string): Promise<void> {
        await this.disconnect();
        this._reset();

        this._socket = new WebSocket(url, protocols);
        this._socket.binaryType = 'arraybuffer';

        await this._setupListenersOnConnect();
    }

    /**
     * Send data through the websocket.
     */
    send(data: any) {
        if (!this.connected) {
            throw this._closeEvent || new Error('Not connected.');
        }
        <commit_msg>Update docs<commit_after>// @flow

/**
 * An asynchronous WebSocket client.
 * @example
 * // Set up connection.
 * const webSocketClient = new WebSocketClient;
 * // Connect.
 * await webSocketClient.connect("ws://www.example.com/", 9000);
 * // Send is synchronous.
 * webSocketClient.send("Hello!");
 * // Receive is asynchronous.
 * console.log(await webSocketClient.receive());
 * // Close the connection.
 * await webSocketClient.disconnect();
 */
export default class WebSocketClient {

    /** @private */
    _socket: WebSocket;

    /** @private */
    _closeEvent: ?CloseEvent;
    
    // The following data structures enable the asynchronous operation 
    // of receive() and _setupListenersOnConnect().

    /** @private */
    _receiveCallbacksQueue: Array<{ resolve: (data: any) => void, reject: (reason: any) => void }>;

    /** @private */
    _receiveDataQueue: Array<any>;
    
    /** @public */
    constructor() {
        this._reset();
    }

    /* public properties */
    
    /**
     * Whether a connection is currently active.
     * @returns true if the connection is active.
     */
    get connected(): boolean {
        // Checking != null also checks against undefined.
        return this._socket != null && this._socket.readyState === WebSocket.OPEN;
    }
    
    /**
     * The number of messages available to receive.
     * @returns The number of queued messages that can be retrieved with {@link #receive}
     */
    get dataAvailable(): number {
        return this._receiveDataQueue.length;
    }

    /**
     * Sets up a WebSocket connection to specified url. Resolves when the 
     * connection is established. Can be called again to reconnect to any url.
     */
    async connect(url: string, protocols?: string): Promise<void> {
        await this.disconnect();
        this._reset();

        this._socket = new WebSocket(url, protocols);
        this._socket.binaryType = 'arraybuffer';

        await this._setupListenersOnConnect();
    }

    /**
     * Send data through the websocket.
     */
    send(data: any) {
        if (!this.connected) {
            throw this._closeEvent || new Error('Not connected.');
        }
        
{'fraction_non_alphanumeric': 0.12582330229389052, 'fraction_numerical': 0.0018169429934135816, 'mean_word_length': 2.435257410296412, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324235', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1168, 'n_words': 359}