Skip to content

Commit

Permalink
feat(client): add minTimeout option
Browse files Browse the repository at this point in the history
actually fix apollographql#295 completely
  • Loading branch information
jedwards1211 committed Oct 17, 2019
1 parent 1978726 commit 0f39b0d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ ReactDOM.render(
- `url: string` : url that the client will connect to, starts with `ws://` or `wss://`
- `options?: Object` : optional, object to modify default client behavior
* `timeout?: number` : how long the client should wait in ms for a keep-alive message from the server (default 30000 ms), this parameter is ignored if the server does not send keep-alive messages. This will also be used to calculate the max connection time per connect/reconnect
* `minTimeout?: number`: the minimum amount of time the client should wait for a connection to be made (default 1000 ms)
* `lazy?: boolean` : use to set lazy mode - connects only when first subscription created, and delay the socket initialization
* `connectionParams?: Object | Function | Promise<Object>` : object that will be available as first argument of `onConnect` (in server side), if passed a function - it will call it and send the return value, if function returns as promise - it will wait until it resolves and send the resolved value.
* `reconnect?: boolean` : automatic reconnect in case of connection error
Expand Down Expand Up @@ -277,7 +278,7 @@ ReactDOM.render(
- => Returns an `off` method to cancel the event subscription.

#### `onReconnecting(callback, thisContext) => Function` - shorthand for `.on('reconnecting', ...)`
- `callback: Function`: function to be called when websocket starts it's reconnection
- `callback: Function`: function to be called when websocket starts it's reconnection
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

Expand Down
8 changes: 6 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getOperationAST } from 'graphql/utilities/getOperationAST';
import $$observable from 'symbol-observable';

import { GRAPHQL_WS } from './protocol';
import { WS_TIMEOUT } from './defaults';
import { MIN_WS_TIMEOUT, WS_TIMEOUT } from './defaults';
import MessageTypes from './message-types';

export interface Observer<T> {
Expand Down Expand Up @@ -60,6 +60,7 @@ export type ConnectionParamsOptions = ConnectionParams | Function | Promise<Conn

export interface ClientOptions {
connectionParams?: ConnectionParamsOptions;
minTimeout?: number;
timeout?: number;
reconnect?: boolean;
reconnectionAttempts?: number;
Expand All @@ -74,6 +75,7 @@ export class SubscriptionClient {
private url: string;
private nextOperationId: number;
private connectionParams: Function;
private minWsTimeout: number;
private wsTimeout: number;
private unsentMessagesQueue: Array<any>; // queued messages while websocket is opening.
private reconnect: boolean;
Expand Down Expand Up @@ -104,6 +106,7 @@ export class SubscriptionClient {
const {
connectionCallback = undefined,
connectionParams = {},
minTimeout = MIN_WS_TIMEOUT,
timeout = WS_TIMEOUT,
reconnect = false,
reconnectionAttempts = Infinity,
Expand All @@ -121,6 +124,7 @@ export class SubscriptionClient {
this.url = url;
this.operations = {};
this.nextOperationId = 0;
this.minWsTimeout = minTimeout;
this.wsTimeout = timeout;
this.unsentMessagesQueue = [];
this.reconnect = reconnect;
Expand Down Expand Up @@ -349,7 +353,7 @@ export class SubscriptionClient {
}

private createMaxConnectTimeGenerator() {
const minValue = 1000;
const minValue = this.minWsTimeout;
const maxValue = this.wsTimeout;

return new Backoff({
Expand Down
2 changes: 2 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const MIN_WS_TIMEOUT = 1000;
const WS_TIMEOUT = 30000;

export {
MIN_WS_TIMEOUT,
WS_TIMEOUT,
};

0 comments on commit 0f39b0d

Please sign in to comment.