Skip to content

Commit

Permalink
feat(rpc/client): adds client types and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 28, 2019
1 parent bc545c6 commit 01985b2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/rpc/src/client/RPCClient/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RPCClientOptions } from '../types';
import { DataInput, DataOutput } from '~/types';

export function createDefaults(): Required<RPCClientOptions> {
return {
responseTimeout: 30000,
subscribePolicy: 'fail',
unsubscribePolicy: 'complete',
parser: {
serialize(data: object): DataOutput {
return JSON.stringify(data);
},
deserialize(data: DataInput): object {
return JSON.parse(String(data));
}
}
};
}
1 change: 1 addition & 0 deletions packages/rpc/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types';
35 changes: 35 additions & 0 deletions packages/rpc/src/client/types/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { RPCClientConnectionStatus } from './connection';
import { DataParser } from '~/types';

export type RPCClientStatus = RPCClientConnectionStatus | 'complete';

export interface RPCClientOptions {
/**
* Timeout for request's first result when connected in milliseconds. `0` for *infinity.* It will cause all pending requests to fail. Default: `30000`.
*/
responseTimeout?: number;
/**
* 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'`.
*/
subscribePolicy?: 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'`.
*/
unsubscribePolicy?: UnsubscribePolicy;
/**
* Serializer and deserializer
*/
parser?: DataParser;
}

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

export type UnsubscribePolicy = 'complete' | 'refetch' | 'keep-alive';
24 changes: 24 additions & 0 deletions packages/rpc/src/client/types/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Observable } from 'rxjs';
import { DataInput, DataOutput } from '~/types';

export interface RPCClientConnection {
status: RPCClientConnectionStatus;
actions: RPCClientConnectionActions;
/**
* Must only emit *data* while its status is `'open'` and after the *open* event is emitted.
* Must *complete* if the connection will not be recovered.
*/
events$: Observable<RPCClientConnectionEvent>;
}

export interface RPCClientConnectionActions {
send: (data: DataOutput) => void | Promise<void>;
close: () => void;
}

export type RPCClientConnectionStatus = 'open' | 'close';

export type RPCClientConnectionEvent =
| { event: 'open' }
| { event: 'close'; data: Error | null }
| { event: 'data'; data: DataInput };
2 changes: 2 additions & 0 deletions packages/rpc/src/client/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './client';
export * from './connection';

0 comments on commit 01985b2

Please sign in to comment.