Skip to content

Commit

Permalink
fix: Drop TypeScript DOM lib dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 31, 2020
1 parent ef1bc8f commit a81e8c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/client.ts
Expand Up @@ -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 Event> = 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 };
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Expand Up @@ -24,8 +24,13 @@ export interface Disposable {
export interface Sink<T = unknown> {
/** 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;
}

0 comments on commit a81e8c1

Please sign in to comment.