2021-12-22 00:24:07 +09:00
|
|
|
const { parentPort } = require('worker_threads')
|
|
|
|
|
|
|
|
const native = require('../index')
|
|
|
|
|
2023-03-05 17:51:06 +09:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2023-04-15 16:37:01 +09:00
|
|
|
break
|
|
|
|
case 'constructor':
|
|
|
|
let ellie
|
|
|
|
for (let i = 0; i < 10000; i++) {
|
|
|
|
ellie = new native.Animal(native.Kind.Cat, 'Ellie')
|
|
|
|
}
|
|
|
|
parentPort.postMessage(ellie.name)
|
2023-03-05 17:51:06 +09:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unknown message type: ${type}`)
|
|
|
|
}
|
|
|
|
})
|