diff options
Diffstat (limited to 'packages/subproviders/src/subproviders/redundant_rpc.ts')
-rw-r--r-- | packages/subproviders/src/subproviders/redundant_rpc.ts | 84 |
1 files changed, 42 insertions, 42 deletions
diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_rpc.ts index a3cb463a8..92d7ae8ae 100644 --- a/packages/subproviders/src/subproviders/redundant_rpc.ts +++ b/packages/subproviders/src/subproviders/redundant_rpc.ts @@ -7,46 +7,46 @@ import { JSONRPCPayload } from '../types'; import { Subprovider } from './subprovider'; export class RedundantRPCSubprovider extends Subprovider { - private _rpcs: RpcSubprovider[]; - private static async _firstSuccessAsync( - rpcs: RpcSubprovider[], - payload: JSONRPCPayload, - next: () => void, - ): Promise<any> { - let lastErr: Error | undefined; - for (const rpc of rpcs) { - try { - const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next); - return data; - } catch (err) { - lastErr = err; - continue; - } - } - if (!_.isUndefined(lastErr)) { - throw lastErr; - } - } - constructor(endpoints: string[]) { - super(); - this._rpcs = _.map(endpoints, endpoint => { - return new RpcSubprovider({ - rpcUrl: endpoint, - }); - }); - } - // tslint:disable-next-line:async-suffix - public async handleRequest( - payload: JSONRPCPayload, - next: () => void, - end: (err: Error | null, data?: any) => void, - ): Promise<void> { - const rpcsCopy = this._rpcs.slice(); - try { - const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next); - end(null, data); - } catch (err) { - end(err); - } - } + private _rpcs: RpcSubprovider[]; + private static async _firstSuccessAsync( + rpcs: RpcSubprovider[], + payload: JSONRPCPayload, + next: () => void, + ): Promise<any> { + let lastErr: Error | undefined; + for (const rpc of rpcs) { + try { + const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next); + return data; + } catch (err) { + lastErr = err; + continue; + } + } + if (!_.isUndefined(lastErr)) { + throw lastErr; + } + } + constructor(endpoints: string[]) { + super(); + this._rpcs = _.map(endpoints, endpoint => { + return new RpcSubprovider({ + rpcUrl: endpoint, + }); + }); + } + // tslint:disable-next-line:async-suffix + public async handleRequest( + payload: JSONRPCPayload, + next: () => void, + end: (err: Error | null, data?: any) => void, + ): Promise<void> { + const rpcsCopy = this._rpcs.slice(); + try { + const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next); + end(null, data); + } catch (err) { + end(err); + } + } } |