holepunchto / pear

combined Peer-to-Peer (P2P) Runtime, Development & Deployment tool
https://docs.pears.com/
Apache License 2.0
115 stars 11 forks source link

Ensure worker close gracefully with the same behaviours in both Terminal and Desktop apps #459

Open maidh91 opened 2 days ago

maidh91 commented 2 days ago

App Type

Scenarios

How to test

Sample Parent code

const pipe = Pear.worker.run('../worker-code')
pipe.on('data', (data) => {
  console.log(data.toString())
})
pipe.on('end', () => {
  console.log('end')
})
pipe.on('close', () => {
  console.log('close')
})
pipe.write('hello')
await new Promise((resolve) => setTimeout(resolve, 2000))
// pipe.end()
// pipe.destroy()
await new Promise((resolve) => setTimeout(resolve, 20000))

Sample Worker code

import fs from 'bare-fs'
const pipe = Pear.worker.pipe()
pipe.on('data', (data) => {
  pipe.write('world')
  fs.writeFileSync('/tmp/test-data', data.toString())
})
pipe.on('end', () => {
  fs.writeFileSync('/tmp/test-end', 'end')
})
pipe.on('close', () => {
  fs.writeFileSync('/tmp/test-close', 'close')
})
await new Promise((resolve) => setTimeout(resolve, 2000))
// pipe.end()
// pipe.destroy()