-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/parts/IpcChildWithMessagePort/IpcChildWithMessagePort.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as GetData from '../GetData/GetData.ts' | ||
import { Ipc } from '../Ipc/Ipc.ts' | ||
import * as ReadyMessage from '../ReadyMessage/ReadyMessage.ts' | ||
|
||
export const listen = ({ port }: { port: MessagePort }) => { | ||
return port | ||
} | ||
|
||
export const signal = (port: MessagePort) => { | ||
port.postMessage(ReadyMessage.readyMessage) | ||
} | ||
|
||
class IpcChildWithMessagePort extends Ipc<MessagePort> { | ||
constructor(port: MessagePort) { | ||
super(port) | ||
} | ||
|
||
override getData(event: any) { | ||
return GetData.getData(event) | ||
} | ||
|
||
override send(message: any): void { | ||
this._rawIpc.postMessage(message) | ||
} | ||
|
||
override sendAndTransfer(message: any, transfer: any): void { | ||
this._rawIpc.postMessage(message, transfer) | ||
} | ||
|
||
override dispose(): void { | ||
// ignore | ||
} | ||
|
||
override onClose(callback: any) { | ||
// ignore | ||
} | ||
|
||
override onMessage(callback: any) { | ||
this._rawIpc.addEventListener('message', callback) | ||
this._rawIpc.start() | ||
} | ||
} | ||
|
||
export const wrap = (port: MessagePort) => { | ||
return new IpcChildWithMessagePort(port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { expect, test } from '@jest/globals' | ||
import * as IpcChildWithMessagePort from '../src/parts/IpcChildWithMessagePort/IpcChildWithMessagePort.ts' | ||
|
||
test('listen - return port', async () => { | ||
const { port1, port2 } = new MessageChannel() | ||
expect(IpcChildWithMessagePort.listen({ port: port1 })).toBe(port1) | ||
port1.close() | ||
port2.close() | ||
}) | ||
|
||
test('signal', async () => { | ||
const { port1, port2 } = new MessageChannel() | ||
const promise = new Promise((resolve) => { | ||
port2.onmessage = resolve | ||
}) | ||
IpcChildWithMessagePort.signal(port1) | ||
const event = await promise | ||
// @ts-ignore | ||
expect(event.data).toBe('ready') | ||
port1.close() | ||
port2.close() | ||
}) | ||
|
||
test('wrap - send', async () => { | ||
const { port1, port2 } = new MessageChannel() | ||
const ipc = IpcChildWithMessagePort.wrap(port1) | ||
const promise = new Promise((resolve) => { | ||
port2.onmessage = resolve | ||
}) | ||
ipc.send('test') | ||
const event = await promise | ||
// @ts-ignore | ||
expect(event.data).toBe('test') | ||
port1.close() | ||
port2.close() | ||
}) | ||
|
||
test('wrap - sendAndTransfer', async () => { | ||
const { port1, port2 } = new MessageChannel() | ||
const ipc = IpcChildWithMessagePort.wrap(port1) | ||
const promise = new Promise((resolve) => { | ||
port2.onmessage = resolve | ||
}) | ||
ipc.sendAndTransfer('test', []) | ||
const event = await promise | ||
// @ts-ignore | ||
expect(event.data).toBe('test') | ||
port1.close() | ||
port2.close() | ||
}) | ||
|
||
test('wrap - onMessage', async () => { | ||
const { port1, port2 } = new MessageChannel() | ||
const ipc = IpcChildWithMessagePort.wrap(port1) | ||
const promise = new Promise((resolve) => { | ||
ipc.onMessage(resolve) | ||
}) | ||
port2.postMessage('test') | ||
const event = await promise | ||
// @ts-ignore | ||
expect(event.data).toBe('test') | ||
port1.close() | ||
port2.close() | ||
}) |