Skip to content

Commit

Permalink
fix(provider): rename to ClientInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Jul 24, 2020
1 parent 2124382 commit 3bfe849
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
21 changes: 21 additions & 0 deletions src/ClientInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface Arguments {
readonly method: string;
readonly params?: readonly unknown[] | object;
}

export type RequestArguments = Arguments;

export type NotificationArguments = Arguments;

export type JSONRPCMessage = RequestArguments | NotificationArguments;

export interface IClient {
request(args: RequestArguments): Promise<unknown>;
notify(args: NotificationArguments): Promise<unknown>;
}

export interface JSONRpcError extends Error {
message: string;
code: number;
data?: unknown;
}
4 changes: 2 additions & 2 deletions src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ProviderRpcError } from "./ProviderInterface";
import { JSONRpcError } from "./ClientInterface";

export const ERR_TIMEOUT = 7777;
export const ERR_MISSIING_ID = 7878;
export const ERR_UNKNOWN = 7979;

export class JSONRPCError extends Error implements ProviderRpcError{
export class JSONRPCError extends Error implements JSONRpcError {
public message: string;
public code: number;
public data: any;
Expand Down
14 changes: 0 additions & 14 deletions src/ProviderInterface.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IJSONRPCRequest, IJSONRPCNotification, IBatchRequest } from "./Request"
import { JSONRPCError } from "./Error";
import StrictEventEmitter from "strict-event-emitter-types";
import { EventEmitter } from "events";
import { ProviderRequestArguments, Provider } from "./ProviderInterface";
import { RequestArguments } from "./ClientInterface";

export type RequestChannel = StrictEventEmitter<EventEmitter, IRequestEvents>;

Expand All @@ -17,7 +17,7 @@ export interface IRequestEvents {
* If a transport fails, or times out, move on to the next.
*/

class RequestManager implements Provider {
class RequestManager {
public transports: Transport[];
public connectPromise: Promise<any>;
public batch: IBatchRequest[] = [];
Expand All @@ -44,11 +44,11 @@ class RequestManager implements Provider {
return this.transports[0];
}

public async request(args: ProviderRequestArguments, notification: boolean = false, timeout?: number): Promise<any> {
public async request(requestObject: RequestArguments, notification: boolean = false, timeout?: number): Promise<any> {
const internalID = (++this.lastId).toString();
const id = notification ? null : internalID;
// naively grab first transport and use it
const payload = {request: this.makeRequest(args.method, args.params || [], id) , internalID};
const payload = {request: this.makeRequest(requestObject.method, requestObject.params || [], id) , internalID};
if (this.batchStarted) {
const result = new Promise((resolve, reject) => {
this.batch.push({ resolve, reject, request: payload });
Expand Down
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import WebSocketTransport from "./transports/WebSocketTransport";
import PostMessageWindowTransport from "./transports/PostMessageWindowTransport";
import PostMessageIframeTransport from "./transports/PostMessageIframeTransport";
import { JSONRPCError } from "./Error";
import { Provider, ProviderRequestArguments } from "./ProviderInterface";
import { IClient, RequestArguments, NotificationArguments } from "./ClientInterface";

/**
* OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and
Expand All @@ -21,7 +21,7 @@ import { Provider, ProviderRequestArguments } from "./ProviderInterface";
* ```
*
*/
class Client implements Provider {
class Client implements IClient {
public requestManager: RequestManager;
constructor(requestManager: RequestManager) {
this.requestManager = requestManager;
Expand Down Expand Up @@ -71,13 +71,17 @@ class Client implements Provider {
* @example
* myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar'));
*/
public async request(requestObject: ProviderRequestArguments, timeout?: number) {
await this.requestManager.connectPromise;
public async request(requestObject: RequestArguments, timeout?: number) {
if (this.requestManager.connectPromise) {
await this.requestManager.connectPromise;
}
return this.requestManager.request(requestObject, false, timeout);
}

public async notify(requestObject: ProviderRequestArguments) {
await this.requestManager.connectPromise;
public async notify(requestObject: NotificationArguments) {
if (this.requestManager.connectPromise) {
await this.requestManager.connectPromise;
}
return this.requestManager.request(requestObject, true);
}

Expand Down

0 comments on commit 3bfe849

Please sign in to comment.