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))
);
export const createClientRpcHandler = <T = any, R = any>(
export const createClientRpcHandler = <Payload = any, Ret = any>(
target: any,
channel: EnclaveEvents,
sender: any,
receiver: any
) => (payload: T): Promise<R> => {
) => (payload: Payload): Promise<Ret> => {
const id = genId();
const sendingChannel = createRpcRequestedEv(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 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<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
// 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
// calls cb when a the channel has an event emitted to it
export const createServerRpcHandler = <T = any>(
export const createServerRpcHandler = (
target: any,
channel: EnclaveEvents,
receiver: any,
cb: any
) => {
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 Reject<T = any> = (reason?: T) => void;
export type ReceiveCb<Arguments = any, Ret = any> = (
export type ReceiveCb<Arguments, Ret> = (
res: Resolve,
rej: Reject,
id?: number
) => RpcEventHandler<Arguments, Ret>;
export const receiveOnChannelFactory = <Arguments = any, Ret = any, K = any>(
cb: ReceiveCb<Arguments, Ret>
) => (channel: string, target: any, on: any, id?: number): Promise<K> =>
export const receiveOnChannelFactory = <CbArgs = any, CbRet = any, Ret = any>(
cb: ReceiveCb<CbArgs, CbRet>
) => (channel: string, target: any, on: any, id?: number): Promise<Ret> =>
new Promise((res, rej) => on.call(target, channel, cb(res, rej, id)));
export const sendOnChannel = (