|
| 1 | +export const messageSync: 0; |
| 2 | +export const messageQueryAwareness: 3; |
| 3 | +export const messageAwareness: 1; |
| 4 | +export const messageAuth: 2; |
| 5 | +/** |
| 6 | + * Websocket Provider for Yjs. Creates a websocket connection to sync the shared document. |
| 7 | + * The document name is attached to the provided url. I.e. the following example |
| 8 | + * creates a websocket connection to http://localhost:1234/my-document-name |
| 9 | + * |
| 10 | + * @example |
| 11 | + * import * as Y from 'yjs' |
| 12 | + * import { WebsocketProvider } from 'y-websocket' |
| 13 | + * const doc = new Y.Doc() |
| 14 | + * const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc) |
| 15 | + * |
| 16 | + * @extends {ObservableV2<{ 'connection-close': (event: CloseEvent | null, provider: WebsocketProvider) => any, 'status': (event: { status: 'connected' | 'disconnected' | 'connecting' }) => any, 'connection-error': (event: Event, provider: WebsocketProvider) => any, 'sync': (state: boolean) => any }>} |
| 17 | + */ |
| 18 | +export class WebsocketProvider extends ObservableV2<{ |
| 19 | + 'connection-close': (event: CloseEvent | null, provider: WebsocketProvider) => any; |
| 20 | + status: (event: { |
| 21 | + status: 'connected' | 'disconnected' | 'connecting'; |
| 22 | + }) => any; |
| 23 | + 'connection-error': (event: Event, provider: WebsocketProvider) => any; |
| 24 | + sync: (state: boolean) => any; |
| 25 | +}> { |
| 26 | + /** |
| 27 | + * @param {string} serverUrl |
| 28 | + * @param {string} roomname |
| 29 | + * @param {Y.Doc} doc |
| 30 | + * @param {object} opts |
| 31 | + * @param {boolean} [opts.connect] |
| 32 | + * @param {awarenessProtocol.Awareness} [opts.awareness] |
| 33 | + * @param {Object<string,string>} [opts.params] specify url parameters |
| 34 | + * @param {Array<string>} [opts.protocols] specify websocket protocols |
| 35 | + * @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill |
| 36 | + * @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds |
| 37 | + * @param {number} [opts.maxBackoffTime] Maximum amount of time to wait before trying to reconnect (we try to reconnect using exponential backoff) |
| 38 | + * @param {number} [opts.minRetryInterval] Minimum mount of time to wait between reconnects if the previous connection succeeded but got closed right after. |
| 39 | + * @param {boolean} [opts.disableBc] Disable cross-tab BroadcastChannel communication |
| 40 | + */ |
| 41 | + constructor(serverUrl: string, roomname: string, doc: Y.Doc, { connect, awareness, params, protocols, WebSocketPolyfill, resyncInterval, maxBackoffTime, minRetryInterval, disableBc }?: { |
| 42 | + connect?: boolean | undefined; |
| 43 | + awareness?: awarenessProtocol.Awareness | undefined; |
| 44 | + params?: { |
| 45 | + [x: string]: string; |
| 46 | + } | undefined; |
| 47 | + protocols?: string[] | undefined; |
| 48 | + WebSocketPolyfill?: { |
| 49 | + new (url: string | URL, protocols?: string | string[] | undefined): WebSocket; |
| 50 | + prototype: WebSocket; |
| 51 | + readonly CLOSED: number; |
| 52 | + readonly CLOSING: number; |
| 53 | + readonly CONNECTING: number; |
| 54 | + readonly OPEN: number; |
| 55 | + } | undefined; |
| 56 | + resyncInterval?: number | undefined; |
| 57 | + maxBackoffTime?: number | undefined; |
| 58 | + minRetryInterval?: number | undefined; |
| 59 | + disableBc?: boolean | undefined; |
| 60 | + }); |
| 61 | + serverUrl: string; |
| 62 | + bcChannel: string; |
| 63 | + maxBackoffTime: number; |
| 64 | + minRetryInterval: number; |
| 65 | + /** |
| 66 | + * The specified url parameters. This can be safely updated. The changed parameters will be used |
| 67 | + * when a new connection is established. |
| 68 | + * @type {Object<string,string>} |
| 69 | + */ |
| 70 | + params: { |
| 71 | + [x: string]: string; |
| 72 | + }; |
| 73 | + protocols: string[]; |
| 74 | + roomname: string; |
| 75 | + doc: Y.Doc; |
| 76 | + _WS: { |
| 77 | + new (url: string | URL, protocols?: string | string[] | undefined): WebSocket; |
| 78 | + prototype: WebSocket; |
| 79 | + readonly CLOSED: number; |
| 80 | + readonly CLOSING: number; |
| 81 | + readonly CONNECTING: number; |
| 82 | + readonly OPEN: number; |
| 83 | + }; |
| 84 | + awareness: awarenessProtocol.Awareness; |
| 85 | + wsconnected: boolean; |
| 86 | + wsconnecting: boolean; |
| 87 | + bcconnected: boolean; |
| 88 | + disableBc: boolean; |
| 89 | + wsUnsuccessfulReconnects: number; |
| 90 | + lastConnectAt: any; |
| 91 | + messageHandlers: ((arg0: encoding.Encoder, arg1: decoding.Decoder, arg2: WebsocketProvider, arg3: boolean, arg4: number) => void)[]; |
| 92 | + /** |
| 93 | + * @type {boolean} |
| 94 | + */ |
| 95 | + _synced: boolean; |
| 96 | + /** |
| 97 | + * @type {WebSocket?} |
| 98 | + */ |
| 99 | + ws: WebSocket | null; |
| 100 | + wsLastMessageReceived: number; |
| 101 | + /** |
| 102 | + * Whether to connect to other peers or not |
| 103 | + * @type {boolean} |
| 104 | + */ |
| 105 | + shouldConnect: boolean; |
| 106 | + /** |
| 107 | + * @type {number} |
| 108 | + */ |
| 109 | + _resyncInterval: number; |
| 110 | + /** |
| 111 | + * @param {ArrayBuffer} data |
| 112 | + * @param {any} origin |
| 113 | + */ |
| 114 | + _bcSubscriber: (data: ArrayBuffer, origin: any) => void; |
| 115 | + /** |
| 116 | + * Listens to Yjs updates and sends them to remote peers (ws and broadcastchannel) |
| 117 | + * @param {Uint8Array} update |
| 118 | + * @param {any} origin |
| 119 | + */ |
| 120 | + _updateHandler: (update: Uint8Array, origin: any) => void; |
| 121 | + /** |
| 122 | + * @param {any} changed |
| 123 | + * @param {any} _origin |
| 124 | + */ |
| 125 | + _awarenessUpdateHandler: ({ added, updated, removed }: any, _origin: any) => void; |
| 126 | + _exitHandler: () => void; |
| 127 | + _checkInterval: any; |
| 128 | + get url(): string; |
| 129 | + set synced(arg: boolean); |
| 130 | + /** |
| 131 | + * @type {boolean} |
| 132 | + */ |
| 133 | + get synced(): boolean; |
| 134 | + connectBc(): void; |
| 135 | + disconnectBc(): void; |
| 136 | + disconnect(): void; |
| 137 | + connect(): void; |
| 138 | +} |
| 139 | +import { ObservableV2 } from "lib0/observable"; |
| 140 | +import * as Y from "yjs"; |
| 141 | +import * as awarenessProtocol from "y-protocols/awareness"; |
| 142 | +import * as encoding from "lib0/encoding"; |
| 143 | +import * as decoding from "lib0/decoding"; |
0 commit comments