Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yeungkc/message-port-wrapper


https://github.com/yeungkc/message-port-wrapper

Last synced: 9 days ago
JSON representation

Awesome Lists containing this project

README

        

# MessagePort

> Simplified IPC communication for MessagePort.

## Installation

```shell
npm install message-port
```

## Usage

```typescript
const wrapper1 = MessagePortWrapper(port1);
const wrapper2 = MessagePortWrapper(port2);

wrapper1.on('test', () => 'foo');
wrapper2.on('test', (data: any) => data);

const data1 = await wrapper1('test', 'test'); // 'foo'
const data2 = await wrapper2.call('test', 'test'); // 'test'
```

## API

```typescript
interface CallFunc {
/**
* invoke a handler with return value
*/
(channel: string, data: T): Promise;
}

export interface MessagePortWrapper extends CallFunc {
/**
* invoke a handler without return value
*/
send: (channel: string, data: T) => void;
invoke: CallFunc;
/**
* add a handler
*/
on: (channel: string, handler: InvokeHandler) => void;
/**
* add a handler with once
*/
once: (
channel: string,
handler: InvokeHandler
) => void;
/**
* remove a handler
*/
removeHandler: (channel: string) => void;
/**
* remove all handlers
*/
removeAllHandlers: () => void;
}
```

## Jest

Jest tests are set up to run with `npm test` or `yarn test`.