-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rpc/client): adds client types and defaults
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './client'; | ||
export * from './connection'; |