napi-rs/examples/napi/__test__/worker.js

42 lines
1,005 B
JavaScript

const { parentPort } = require('worker_threads')
const native = require('../index')
parentPort.on('message', ({ type }) => {
switch (type) {
case 'require':
parentPort.postMessage(
native.Animal.withKind(native.Kind.Cat).whoami() + native.DEFAULT_COST,
)
break
case 'async:buffer':
Promise.all(
Array.from({ length: 100 }).map(() =>
native.bufferPassThrough(Buffer.from([1, 2, 3])),
),
)
.then(() => {
parentPort.postMessage('done')
})
.catch((e) => {
throw e
})
break
case 'async:arraybuffer':
Promise.all(
Array.from({ length: 100 }).map(() =>
native.arrayBufferPassThrough(Uint8Array.from([1, 2, 3])),
),
)
.then(() => {
parentPort.postMessage('done')
})
.catch((e) => {
throw e
})
break
default:
throw new TypeError(`Unknown message type: ${type}`)
}
})