Skip to content

Commit

Permalink
feat(rpc-client): adds client option types and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 25, 2019
1 parent 2280b41 commit 18b3e1b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/rpc-client/src/client/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RPCClientOptions } from './types';

export function defaultOptions(
options?: RPCClientOptions | null
): Required<RPCClientOptions> {
if (!options) options = {};
const timeouts = options.timeouts || {};

return {
attempts: options.attempts ? Math.max(0, options.attempts) : 0,
...options,
timeouts: {
connect: Object.hasOwnProperty.call(timeouts, 'connect')
? Math.max(0, timeouts.connect || 0)
: 7500,
response: Object.hasOwnProperty.call(timeouts, 'response')
? Math.max(0, timeouts.response || 0)
: 30000
},
policies: {
subscribe: 'fail',
unsubscribe: 'complete',
...options.policies
}
};
}
50 changes: 50 additions & 0 deletions packages/rpc-client/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type RPCClientStatus = 'pending' | 'open' | 'close' | 'complete';

export interface RPCClientOptions {
/**
* Attempt connection an arbitrary number of times on failure. `0` for *infinity.* Default: `0`.
*/
attempts?: number;
/**
* Client's timeouts.
*/
timeouts?: RPCClientTimeouts;
/**
* Client's policies.
*/
policies?: RPCClientPolicies;
}

export interface RPCClientTimeouts {
/**
* Connection timeout in milliseconds. It will cause all pending requests to fail. `0` for *infinity.* Default: `7500`.
*/
connect?: number;
/**
* Timeout for request's first result when connected in milliseconds. `0` for *infinity.* It will cause all pending requests to fail. Default: `30000`.
*/
response?: number;
}

export interface RPCClientPolicies {
/**
* On subscriptions:
* - `'fail'`: behaves normally, failing on connection timeout and errors, and response timeouts and errors.
* - `'await-connection'`: it will only fail for response timeouts and errors, waiting for reconnection. Once connection is available, it will refetch. If `reconnect` has a limit, it will fail once it is reached too.
* - `'no-fail'`: It will only fail if `reconnect` has a limit and it is reached. When response timeouts and errors occur, they will be notified as global errors, and the request will be sent yet again.
* Default: `'fail'`.
*/
subscribe?: SubscribePolicy;
/**
* After a subscription observable is subscribed to at least once, when it is unsubscribed:
* - `'complete'`: will send an *unsubscribe* signal to the server and any future subscription will instantly complete, without any values being streamed.
* - `'refetch'`: will send an *unsubscribe* signal to the server so it can free up resources, and re-subscribe if it is subscribed to again.
* - `'keep-alive'`: will never send an *unsubscribe* signal to the server. Not recommended.
* Default: `'complete'`.
*/
unsubscribe?: UnsubscribePolicy;
}

export type SubscribePolicy = 'fail' | 'await-connection' | 'no-fail';

export type UnsubscribePolicy = 'complete' | 'refetch' | 'keep-alive';

0 comments on commit 18b3e1b

Please sign in to comment.