Skip to content

Commit

Permalink
app worker teardown iteration (#397)
Browse files Browse the repository at this point in the history
* app worker teardown

* ..

* endend

* end end

* fixed

* fixed
  • Loading branch information
davidmarkclements committed Oct 30, 2024
1 parent 0aa91dd commit b5b667a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
19 changes: 11 additions & 8 deletions gui/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const IPC = require('pear-ipc')
const ReadyResource = require('ready-resource')
const Worker = require('../lib/worker')
const constants = require('../constants')

const kMap = Symbol('pear.gui.map')
const kCtrl = Symbol('pear.gui.ctrl')

Expand Down Expand Up @@ -380,7 +379,9 @@ class App {
appReady = false
static root = unixPathResolve(resolve(__dirname, '..'))

constructor (state, ipc) {
constructor (gui) {
const { state, ipc } = gui
this.gui = gui
this.state = state
this.ipc = ipc
this.contextMenu = null
Expand Down Expand Up @@ -647,16 +648,18 @@ class App {
resolve(false)
}
})

const unloaders = PearGUI.ctrls().map((ctrl) => {
const pipes = [...this.gui.pipes]
const closingPipes = pipes.map((pipe) => new Promise((resolve) => { pipe.once('close', resolve) }))
const unloaders = [closingPipes, ...PearGUI.ctrls().map((ctrl) => {
const closed = () => ctrl.closed
if (!ctrl.unload) {
if (ctrl.unloader) return ctrl.unloader.then(closed, closed)
return ctrl.close()
}
ctrl.unload({ type: 'close' })
return ctrl.unloader.then(closed, closed)
})
})]
for (const pipe of pipes) pipe.end()
const unloading = Promise.all(unloaders)
unloading.then(clear, clear)
const result = await Promise.race([timeout, unloading])
Expand Down Expand Up @@ -1498,8 +1501,8 @@ class PearGUI extends ReadyResource {
electron.ipcMain.handle('versions', (evt, ...args) => this.versions(...args))
electron.ipcMain.handle('restart', (evt, ...args) => this.restart(...args))

electron.ipcMain.on('workerRun', (evt, link) => {
const pipe = this.worker.run(link)
electron.ipcMain.on('workerRun', (evt, link, args) => {
const pipe = this.worker.run(link, args)
const id = this.pipes.alloc(pipe)
pipe.on('close', () => {
this.pipes.free(id)
Expand Down Expand Up @@ -1546,7 +1549,7 @@ class PearGUI extends ReadyResource {
}

async app () {
const app = new App(this.state, this.ipc)
const app = new App(this)
this.once('close', async () => { app.quit() })
await app.start()
return app
Expand Down
3 changes: 2 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Worker {
}

run (link, args = []) {
if (isElectronRenderer) return this.#ipc.workerRun(link)
if (isElectronRenderer) return this.#ipc.workerRun(link, args)
args = [...this.#args(link), ...args]
const sp = spawn(constants.RUNTIME, args, {
stdio: ['inherit', 'inherit', 'inherit', 'overlapped'],
Expand All @@ -64,6 +64,7 @@ class Worker {
return null
}
const pipe = new Pipe(fd)
pipe.on('end', () => pipe.end())
this.#pipe = pipe
pipe.once('close', () => {
// allow close event to propagate between processes before exiting:
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/worker/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const pipe = Pear.worker.pipe()

let i = 0

let interval = null
pipe.on('data', (data) => {
const str = data.toString()
if (str === 'ping') {
setInterval(() => pipe.write((i++).toString()), 2000)
interval = setInterval(() => pipe.write((i++).toString()), 2000)
}
if (str === 'exit') {
clearInterval(interval)
Pear.exit()
}
})

0 comments on commit b5b667a

Please sign in to comment.