From a81e8c1ea080984ddd6d5e58c154b866ee44c14c Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Sun, 1 Nov 2020 00:32:28 +0100 Subject: [PATCH] fix: Drop TypeScript DOM lib dependency --- src/client.ts | 22 +++++++++++++++++++--- src/types.ts | 9 +++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/client.ts b/src/client.ts index 902125c3..1be4495d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -20,12 +20,28 @@ export type EventConnected = 'connected'; // connected = socket opened + acknowl export type EventClosed = 'closed'; export type Event = EventConnecting | EventConnected | EventClosed; +/** + * The argument is actually the `WebSocket`, but to avoid bundling DOM typings + * because the client can run in Node env too, you should assert + * the websocket type during implementation. + */ +export type EventConnectedHandler = (socket: unknown) => void; + +export type EventConnectingHandler = () => void; + +/** + * The argument is actually the websocket `CloseEvent`, but to avoid + * bundling DOM typings because the client can run in Node env too, + * you should assert the websocket type during implementation. + */ +export type EventClosedHandler = (event: unknown) => void; + export type EventListener = E extends EventConnecting - ? () => void + ? EventConnectingHandler : E extends EventConnected - ? (socket: WebSocket) => void + ? EventConnectedHandler : E extends EventClosed - ? (event: CloseEvent) => void + ? EventClosedHandler : never; type CancellerRef = { current: (() => void) | null }; diff --git a/src/types.ts b/src/types.ts index 43fbc351..2777fcd9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,8 +24,13 @@ export interface Disposable { export interface Sink { /** Next value arriving. */ next(value: T): void; - /** An error that has occured. Calling this function "closes" the sink. */ - error(error: Error | CloseEvent | readonly GraphQLError[]): void; + /** + * An error that has occured. Calling this function "closes" the sink. + * The error can be also `CloseEvent`, but to avoid bundling DOM typings + * because the client can run in Node env too, you should assert + * the close event type during implementation. + */ + error(error: Error | readonly GraphQLError[] | unknown): void; /** The sink has completed. This function "closes" the sink. */ complete(): void; }