Cleanup types

This commit is contained in:
HenryNguyen5 2018-04-23 20:50:43 -04:00
parent bbfb084bdc
commit 570b3c42b9
4 changed files with 22 additions and 12 deletions

View File

@ -35,12 +35,12 @@ const clientRecieve = receiveOnChannelFactory((res, rej, id) =>
matchOnId(id, handleServerResponse(res, rej)) matchOnId(id, handleServerResponse(res, rej))
); );
export const createClientRpcHandler = <T = any, R = any>( export const createClientRpcHandler = <Payload = any, Ret = any>(
target: any, target: any,
channel: EnclaveEvents, channel: EnclaveEvents,
sender: any, sender: any,
receiver: any receiver: any
) => (payload: T): Promise<R> => { ) => (payload: Payload): Promise<Ret> => {
const id = genId(); const id = genId();
const sendingChannel = createRpcRequestedEv(channel); const sendingChannel = createRpcRequestedEv(channel);
const receivingChannel = createRpcProcessedEv(channel); const receivingChannel = createRpcProcessedEv(channel);

View File

@ -1,8 +1,13 @@
import { SignRawTransactionParams, ElectronInjectedGlobals, EnclaveEvents } from './types'; import {
SignRawTransactionParams,
ElectronInjectedGlobals,
EnclaveEvents,
SignRawTransaction
} from './types';
import { createClientRpcHandler } from './client-utils'; import { createClientRpcHandler } from './client-utils';
import EventEmitter from 'events'; import EventEmitter from 'events';
const myEE = new EventEmitter(); const eventEmitter = new EventEmitter();
function isElectronEnv() { function isElectronEnv() {
return process.env.BUILD_ELECTRON; return process.env.BUILD_ELECTRON;
} }
@ -18,6 +23,11 @@ export function signRawTransaction(params: SignRawTransactionParams) {
w.signRawTransaction(params); w.signRawTransaction(params);
} else { } else {
// otherwise create an rpc event handler based on "events" package // otherwise create an rpc event handler based on "events" package
createClientRpcHandler(myEE, EnclaveEvents.SIGN_RAW_TRANSACTION, myEE.emit, myEE.on); createClientRpcHandler<SignRawTransactionParams, SignRawTransaction>(
eventEmitter,
EnclaveEvents.SIGN_RAW_TRANSACTION,
eventEmitter.emit,
eventEmitter.on
);
} }
} }

View File

@ -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 // 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 // cb instead handles the receiving event, and on success/failure emits on EnclaveEvent channel with its response
const serverRecieve = <T = any>(cb: any) => receiveOnChannelFactory<RpcEventServer, T>(() => cb); const serverRecieve = (cb: any) => receiveOnChannelFactory<RpcEventServer, void, void>(() => cb);
// create a receiving channel for incoming events of Enclave events type // create a receiving channel for incoming events of Enclave events type
// calls cb when a the channel has an event emitted to it // calls cb when a the channel has an event emitted to it
export const createServerRpcHandler = <T = any>( export const createServerRpcHandler = (
target: any, target: any,
channel: EnclaveEvents, channel: EnclaveEvents,
receiver: any, receiver: any,
cb: any cb: any
) => { ) => {
const receivingChannel = createRpcRequestedEv(channel); const receivingChannel = createRpcRequestedEv(channel);
serverRecieve<T>(cb)(receivingChannel, target, receiver); serverRecieve(cb)(receivingChannel, target, receiver);
}; };

View File

@ -15,15 +15,15 @@ export const createRpcProcessedEv = (e: EnclaveEvents) => `${e}-processed`;
type Resolve<T = any> = (value?: T | PromiseLike<T>) => void; type Resolve<T = any> = (value?: T | PromiseLike<T>) => void;
type Reject<T = any> = (reason?: T) => void; type Reject<T = any> = (reason?: T) => void;
export type ReceiveCb<Arguments = any, Ret = any> = ( export type ReceiveCb<Arguments, Ret> = (
res: Resolve, res: Resolve,
rej: Reject, rej: Reject,
id?: number id?: number
) => RpcEventHandler<Arguments, Ret>; ) => RpcEventHandler<Arguments, Ret>;
export const receiveOnChannelFactory = <Arguments = any, Ret = any, K = any>( export const receiveOnChannelFactory = <CbArgs = any, CbRet = any, Ret = any>(
cb: ReceiveCb<Arguments, Ret> cb: ReceiveCb<CbArgs, CbRet>
) => (channel: string, target: any, on: any, id?: number): Promise<K> => ) => (channel: string, target: any, on: any, id?: number): Promise<Ret> =>
new Promise((res, rej) => on.call(target, channel, cb(res, rej, id))); new Promise((res, rej) => on.call(target, channel, cb(res, rej, id)));
export const sendOnChannel = ( export const sendOnChannel = (