Simplified IPC communication for MessagePort.
npm install message-port-wrapper> Simplified IPC communication for MessagePort.
``shell`
npm install message-port
`typescript
const wrapper1 = MessagePortWrapper(port1);
const wrapper2 = MessagePortWrapper(port2);
wrapper1.on('test', () => 'foo');
wrapper2.on('test', (data: any) => data);
const data1 = await wrapper1
const data2 = await wrapper2.call('test', 'test'); // 'test'
`
`typescript
interface CallFunc {
/**
* invoke a handler with return value
*/
}
export interface MessagePortWrapper extends CallFunc {
/**
* invoke a handler without return value
*/
send:
invoke: CallFunc;
/**
* add a handler
*/
on:
/**
* add a handler with once
*/
once:
channel: string,
handler: InvokeHandler
) => void;
/**
* remove a handler
*/
removeHandler: (channel: string) => void;
/**
* remove all handlers
*/
removeAllHandlers: () => void;
}
`
Jest tests are set up to run with npm test or yarn test`.