Skip to content

Commit

Permalink
docs: use listener and generate [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 31, 2020
1 parent a81e8c1 commit 7f5aeda
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
9 changes: 6 additions & 3 deletions docs/interfaces/_client_.clientoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ ___
`Optional` **generateID**: undefined \| () => [ID](../modules/_types_.md#id)

A custom ID generator for identifying subscriptions.
The default uses the `crypto` module in the global scope
which is present for modern browsers. However, if
it can't be found, `Math.random` would be used instead.

The default generates a v4 UUID to be used as the ID using `Math`
as the random number generator. Supply your own generator
in case you need more uniqueness.

Reference: https://stackoverflow.com/a/2117523/709884

___

Expand Down
7 changes: 5 additions & 2 deletions docs/interfaces/_types_.sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ ___

### error

**error**(`error`: Error \| CloseEvent \| readonly GraphQLError[]): void
**error**(`error`: Error \| readonly GraphQLError[] \| unknown): 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.

#### Parameters:

Name | Type |
------ | ------ |
`error` | Error \| CloseEvent \| readonly GraphQLError[] |
`error` | Error \| readonly GraphQLError[] \| unknown |

**Returns:** void

Expand Down
31 changes: 30 additions & 1 deletion docs/modules/_client_.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

* [Event](_client_.md#event)
* [EventClosed](_client_.md#eventclosed)
* [EventClosedListener](_client_.md#eventclosedlistener)
* [EventConnected](_client_.md#eventconnected)
* [EventConnectedListener](_client_.md#eventconnectedlistener)
* [EventConnecting](_client_.md#eventconnecting)
* [EventConnectingListener](_client_.md#eventconnectinglistener)
* [EventListener](_client_.md#eventlistener)

### Functions
Expand All @@ -37,21 +40,47 @@ ___

___

### EventClosedListener

Ƭ **EventClosedListener**: (event: unknown) => 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.

___

### EventConnected

Ƭ **EventConnected**: \"connected\"

___

### EventConnectedListener

Ƭ **EventConnectedListener**: (socket: unknown) => void

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.

___

### EventConnecting

Ƭ **EventConnecting**: \"connecting\"

___

### EventConnectingListener

Ƭ **EventConnectingListener**: () => void

___

### EventListener

Ƭ **EventListener**\<E>: E *extends* EventConnecting ? () => void : E *extends* EventConnected ? (socket: WebSocket) => void : E *extends* EventClosed ? (event: CloseEvent) => void : never
Ƭ **EventListener**\<E>: E *extends* EventConnecting ? EventConnectingListener : E *extends* EventConnected ? EventConnectedListener : E *extends* EventClosed ? EventClosedListener : never

#### Type parameters:

Expand Down
18 changes: 9 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ export type Event = EventConnecting | EventConnected | EventClosed;
* 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 EventConnectedListener = (socket: unknown) => void;

export type EventConnectingHandler = () => void;
export type EventConnectingListener = () => 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 EventClosedListener = (event: unknown) => void;

export type EventListener<E extends Event> = E extends EventConnecting
? EventConnectingHandler
? EventConnectingListener
: E extends EventConnected
? EventConnectedHandler
? EventConnectedListener
: E extends EventClosed
? EventClosedHandler
? EventClosedListener
: never;

type CancellerRef = { current: (() => void) | null };
Expand Down Expand Up @@ -429,7 +429,7 @@ export function createClient(options: ClientOptions): Client {
const id = generateID();
const cancellerRef: CancellerRef = { current: null };

const messageHandler = ({ data }: MessageEvent) => {
const messageListener = ({ data }: MessageEvent) => {
const message = memoParseMessage(data);
switch (message.type) {
case MessageType.Next: {
Expand Down Expand Up @@ -473,7 +473,7 @@ export function createClient(options: ClientOptions): Client {
const [socket, throwOnCloseOrWaitForCancel] = await connect(
cancellerRef,
);
socket.addEventListener('message', messageHandler);
socket.addEventListener('message', messageListener);

socket.send(
stringifyMessage<MessageType.Subscribe>({
Expand All @@ -495,7 +495,7 @@ export function createClient(options: ClientOptions): Client {
);
});

socket.removeEventListener('message', messageHandler);
socket.removeEventListener('message', messageListener);

// cancelled, shouldnt try again
return;
Expand Down

0 comments on commit 7f5aeda

Please sign in to comment.