Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeungkc/message-port-wrapper
https://github.com/yeungkc/message-port-wrapper
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yeungkc/message-port-wrapper
- Owner: YeungKC
- License: mit
- Created: 2022-04-10T10:27:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-18T12:04:08.000Z (5 months ago)
- Last Synced: 2024-10-18T09:59:35.408Z (19 days ago)
- Language: TypeScript
- Size: 239 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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`.