https://github.com/yeungkc/message-port-wrapper
https://github.com/yeungkc/message-port-wrapper
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/yeungkc/message-port-wrapper
- Owner: YeungKC
- License: mit
- Created: 2022-04-10T10:27:31.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-18T12:04:08.000Z (about 1 year ago)
- Last Synced: 2025-03-30T04:26:36.446Z (3 months 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`.