<commit_before>import { replicateRxCollection, RxReplicationState } from '../replication';
import {
    WebsocketClientOptions,
    WebsocketMessageResponseType,
    WebsocketMessageType
} from './websocket-types';

import ReconnectingWebSocket from 'reconnecting-websocket';
import {
    WebSocket as IsomorphicWebSocket
} from 'isomorphic-ws';
import {
    getFromMapOrThrow,
    randomCouchString
} from '../../util';
import {
    filter,
    map,
    Subject,
    firstValueFrom,
    BehaviorSubject
} from 'rxjs';
import {
    RxDatabase,
    RxError,
    RxReplicationWriteToMasterRow
} from '../../types';
import { newRxError } from '../../rx-error';

export type WebsocketWithRefCount = {
    url: string;
    socket: ReconnectingWebSocket;
    refCount: number;
    openPromise: Promise<void>;
    connected$: BehaviorSubject<boolean>;
    message$: Subject<WebsocketMessageResponseType>;
    error$: Subject<RxError>
};

/**
 * Reuse the same socket even when multiple
 * collection replicate with the same server at once.
 */
export const WEBSOCKET_BY_CACHE_KEY: Map<string, WebsocketWithRefCount> = new Map();
export async function getWebSocket(
    url: string,
    database: RxDatabase<any, any, any>
): Promise<WebsocketWithRefCount> {
    /**
     * Also use the database token as cache-key
     * to make it easier to test and debug
     * multi-instance setups.
     */
    const cacheKey = url + '|||' + database.token;

    let has = WEBSOCKET_BY_CACHE_KEY.get(cacheKey);
    if (!has) {
        const wsClient = new ReconnectingWebSocket(
            url,
            undefined,
            {
                WebSocket: IsomorphicWebSocket
            }
        );

        const connected$ = new BehaviorSubject<boolean>(false);
        const openPromise = new Promise<void>(res => {
            wsClient.onopen = () => {<commit_msg>FIX websocket<commit_after>import { replicateRxCollection, RxReplicationState } from '../replication';
import {
    WebsocketClientOptions,
    WebsocketMessageResponseType,
    WebsocketMessageType
} from './websocket-types';

import ReconnectingWebSocket from 'reconnecting-websocket';
import {
    WebSocket as IsomorphicWebSocket
} from 'isomorphic-ws';
import {
    getFromMapOrThrow,
    randomCouchString
} from '../../util';
import {
    filter,
    map,
    Subject,
    firstValueFrom,
    BehaviorSubject
} from 'rxjs';
import {
    RxDatabase,
    RxError,
    RxReplicationWriteToMasterRow
} from '../../types';
import { newRxError } from '../../rx-error';

export type WebsocketWithRefCount = {
    url: string;
    socket: ReconnectingWebSocket;
    refCount: number;
    openPromise: Promise<void>;
    connected$: BehaviorSubject<boolean>;
    message$: Subject<WebsocketMessageResponseType>;
    error$: Subject<RxError>
};

/**
 * Reuse the same socket even when multiple
 * collection replicate with the same server at once.
 */
export const WEBSOCKET_BY_CACHE_KEY: Map<string, WebsocketWithRefCount> = new Map();
export async function getWebSocket(
    url: string,
    database: RxDatabase<any, any, any>
): Promise<WebsocketWithRefCount> {
    /**
     * Also use the database token as cache-key
     * to make it easier to test and debug
     * multi-instance setups.
     */
    const cacheKey = url + '|||' + database.token;

    let has = WEBSOCKET_BY_CACHE_KEY.get(cacheKey);
    if (!has) {
        const wsClient = new ReconnectingWebSocket(
            url,
            [],
            {
                WebSocket: IsomorphicWebSocket
            }
        );

        const connected$ = new BehaviorSubject<boolean>(false);
        const openPromise = new Promise<void>(res => {
            wsClient.onopen = () => {
{'fraction_non_alphanumeric': 0.10586011342155009, 'fraction_numerical': 0.0, 'mean_word_length': 2.9742489270386265, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '251391', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1060, 'n_words': 274}