UNPKG

@hocuspocus/common

Version:

shared code for multiple Hocuspocus packages

114 lines (113 loc) 5.3 kB
import type { Event, MessageEvent } from "ws"; import { Awareness } from "y-protocols/awareness"; import * as Y from "yjs"; import EventEmitter from "./EventEmitter.ts"; import type { CompleteHocuspocusProviderWebsocketConfiguration } from "./HocuspocusProviderWebsocket.ts"; import { HocuspocusProviderWebsocket } from "./HocuspocusProviderWebsocket.ts"; import type { ConstructableOutgoingMessage, onAuthenticatedParameters, onAuthenticationFailedParameters, onAwarenessChangeParameters, onAwarenessUpdateParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onSyncedParameters, onUnsyncedChangesParameters } from "./types.ts"; export type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, "name">> & Partial<CompleteHocuspocusProviderConfiguration> & (Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">> | Required<Pick<CompleteHocuspocusProviderConfiguration, "websocketProvider">>); export interface CompleteHocuspocusProviderConfiguration { /** * The identifier/name of your document */ name: string; /** * The actual Y.js document */ document: Y.Doc; /** * An Awareness instance to keep the presence state of all clients. * * You can disable sharing awareness information by passing `null`. * Note that having no awareness information shared across all connections will break our ping checks * and thus trigger reconnects. You should always have at least one Provider with enabled awareness per * socket connection, or ensure that the Provider receives messages before running into `HocuspocusProviderWebsocket.messageReconnectTimeout`. */ awareness: Awareness | null; /** * A token that’s sent to the backend for authentication purposes. */ token: string | (() => string) | (() => Promise<string>) | null; /** * Hocuspocus websocket provider */ websocketProvider: HocuspocusProviderWebsocket; /** * Force syncing the document in the defined interval. */ forceSyncInterval: false | number; onAuthenticated: (data: onAuthenticatedParameters) => void; onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void; onOpen: (data: onOpenParameters) => void; onConnect: () => void; onMessage: (data: onMessageParameters) => void; onOutgoingMessage: (data: onOutgoingMessageParameters) => void; onSynced: (data: onSyncedParameters) => void; onDisconnect: (data: onDisconnectParameters) => void; onClose: (data: onCloseParameters) => void; onDestroy: () => void; onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void; onAwarenessChange: (data: onAwarenessChangeParameters) => void; onStateless: (data: onStatelessParameters) => void; onUnsyncedChanges: (data: onUnsyncedChangesParameters) => void; } export declare class AwarenessError extends Error { code: number; } export declare class HocuspocusProvider extends EventEmitter { configuration: CompleteHocuspocusProviderConfiguration; isSynced: boolean; unsyncedChanges: number; isAuthenticated: boolean; authorizedScope: string | undefined; manageSocket: boolean; private _isAttached; intervals: any; constructor(configuration: HocuspocusProviderConfiguration); boundDocumentUpdateHandler: (update: Uint8Array, origin: any) => void; boundAwarenessUpdateHandler: ({ added, updated, removed }: any, origin: any) => void; boundPageHide: () => void; boundOnOpen: (event: Event) => Promise<void>; boundOnClose: () => void; forwardConnect: (e: any) => this; forwardClose: (e: any) => this; forwardDisconnect: (e: any) => this; forwardDestroy: (e: any) => this; setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void; get document(): Y.Doc; get isAttached(): boolean; get awareness(): Awareness | null; get hasUnsyncedChanges(): boolean; private resetUnsyncedChanges; incrementUnsyncedChanges(): void; decrementUnsyncedChanges(): void; forceSync(): void; pageHide(): void; registerEventListeners(): void; sendStateless(payload: string): void; documentUpdateHandler(update: Uint8Array, origin: any): void; awarenessUpdateHandler({ added, updated, removed }: any, origin: any): void; /** * Indicates whether a first handshake with the server has been established * * Note: this does not mean all updates from the client have been persisted to the backend. For this, * use `hasUnsyncedChanges`. */ get synced(): boolean; set synced(state: boolean); receiveStateless(payload: string): void; connect(): Promise<unknown>; disconnect(): void; onOpen(event: Event): Promise<void>; getToken(): Promise<string | null>; startSync(): void; send(message: ConstructableOutgoingMessage, args: any): void; onMessage(event: MessageEvent): void; onClose(): void; destroy(): void; detach(): void; attach(): void; permissionDeniedHandler(reason: string): void; authenticatedHandler(scope: string): void; setAwarenessField(key: string, value: any): void; }