From 570b3c42b93f57e1e344df923e6c34736d23e60b Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Mon, 23 Apr 2018 20:50:43 -0400 Subject: [PATCH] Cleanup types --- common/libs/EthEnclave/client-utils.ts | 4 ++-- common/libs/EthEnclave/client.ts | 16 +++++++++++++--- common/libs/EthEnclave/server-utils.ts | 6 +++--- common/libs/EthEnclave/utils.ts | 8 ++++---- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/common/libs/EthEnclave/client-utils.ts b/common/libs/EthEnclave/client-utils.ts index 3c9df81b..f345b589 100644 --- a/common/libs/EthEnclave/client-utils.ts +++ b/common/libs/EthEnclave/client-utils.ts @@ -35,12 +35,12 @@ const clientRecieve = receiveOnChannelFactory((res, rej, id) => matchOnId(id, handleServerResponse(res, rej)) ); -export const createClientRpcHandler = ( +export const createClientRpcHandler = ( target: any, channel: EnclaveEvents, sender: any, receiver: any -) => (payload: T): Promise => { +) => (payload: Payload): Promise => { const id = genId(); const sendingChannel = createRpcRequestedEv(channel); const receivingChannel = createRpcProcessedEv(channel); diff --git a/common/libs/EthEnclave/client.ts b/common/libs/EthEnclave/client.ts index 47a1759f..26d311c9 100644 --- a/common/libs/EthEnclave/client.ts +++ b/common/libs/EthEnclave/client.ts @@ -1,8 +1,13 @@ -import { SignRawTransactionParams, ElectronInjectedGlobals, EnclaveEvents } from './types'; +import { + SignRawTransactionParams, + ElectronInjectedGlobals, + EnclaveEvents, + SignRawTransaction +} from './types'; import { createClientRpcHandler } from './client-utils'; import EventEmitter from 'events'; -const myEE = new EventEmitter(); +const eventEmitter = new EventEmitter(); function isElectronEnv() { return process.env.BUILD_ELECTRON; } @@ -18,6 +23,11 @@ export function signRawTransaction(params: SignRawTransactionParams) { w.signRawTransaction(params); } else { // otherwise create an rpc event handler based on "events" package - createClientRpcHandler(myEE, EnclaveEvents.SIGN_RAW_TRANSACTION, myEE.emit, myEE.on); + createClientRpcHandler( + eventEmitter, + EnclaveEvents.SIGN_RAW_TRANSACTION, + eventEmitter.emit, + eventEmitter.on + ); } } diff --git a/common/libs/EthEnclave/server-utils.ts b/common/libs/EthEnclave/server-utils.ts index 7bde8898..d419f89b 100644 --- a/common/libs/EthEnclave/server-utils.ts +++ b/common/libs/EthEnclave/server-utils.ts @@ -3,16 +3,16 @@ import { EnclaveEvents, RpcEventServer } from './types'; // uses factory component, skips the promise handling and id parameter as those are only used on client side // cb instead handles the receiving event, and on success/failure emits on EnclaveEvent channel with its response -const serverRecieve = (cb: any) => receiveOnChannelFactory(() => cb); +const serverRecieve = (cb: any) => receiveOnChannelFactory(() => cb); // create a receiving channel for incoming events of Enclave events type // calls cb when a the channel has an event emitted to it -export const createServerRpcHandler = ( +export const createServerRpcHandler = ( target: any, channel: EnclaveEvents, receiver: any, cb: any ) => { const receivingChannel = createRpcRequestedEv(channel); - serverRecieve(cb)(receivingChannel, target, receiver); + serverRecieve(cb)(receivingChannel, target, receiver); }; diff --git a/common/libs/EthEnclave/utils.ts b/common/libs/EthEnclave/utils.ts index a5e66da1..8e608e82 100644 --- a/common/libs/EthEnclave/utils.ts +++ b/common/libs/EthEnclave/utils.ts @@ -15,15 +15,15 @@ export const createRpcProcessedEv = (e: EnclaveEvents) => `${e}-processed`; type Resolve = (value?: T | PromiseLike) => void; type Reject = (reason?: T) => void; -export type ReceiveCb = ( +export type ReceiveCb = ( res: Resolve, rej: Reject, id?: number ) => RpcEventHandler; -export const receiveOnChannelFactory = ( - cb: ReceiveCb -) => (channel: string, target: any, on: any, id?: number): Promise => +export const receiveOnChannelFactory = ( + cb: ReceiveCb +) => (channel: string, target: any, on: any, id?: number): Promise => new Promise((res, rej) => on.call(target, channel, cb(res, rej, id))); export const sendOnChannel = (