Skip to content

Commit

Permalink
fix(pty): exit pty instances after window closed
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Aug 9, 2020
1 parent 29f2f5c commit 0594744
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 12 additions & 8 deletions main/lib/terminal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, ipcMain, BrowserWindow } = require('electron')
const { app, ipcMain } = require('electron')
const pty = require('node-pty')
const memoize = require('lodash/memoize')
const fs = require('fs')
Expand All @@ -7,6 +7,7 @@ const { getSettings } = require('./settings')

/**
* @typedef {import('node-pty').IPty} IPty
* @typedef {import('electron').WebContents} WebContents
*/

const getShells = memoize(async () => {
Expand Down Expand Up @@ -39,11 +40,11 @@ const ptyProcessMap = new Map()
/**
*
* @param {object} options
* @param {number} frameId
* @param {WebContents} webContents
* @param {string=} options.shell
* @param {string=} options.cwd
*/
async function createTerminal(frameId, { shell, cwd }) {
async function createTerminal(webContents, { shell, cwd }) {
const settings = await getSettings()
const variables = await getVariables()
const env = {
Expand Down Expand Up @@ -74,17 +75,20 @@ async function createTerminal(frameId, { shell, cwd }) {
}
const ptyProcess = pty.spawn(shell, args, options)
ptyProcess.onData(data => {
const frame = BrowserWindow.fromId(frameId)
frame.webContents.send('input-terminal', {
webContents.send('input-terminal', {
pid: ptyProcess.pid,
process: ptyProcess.process,
data,
})
})
ptyProcess.onExit(data => {
ptyProcessMap.delete(ptyProcess.pid)
const frame = BrowserWindow.fromId(frameId)
frame.webContents.send('exit-terminal', { pid: ptyProcess.pid, data })
if (!webContents.isDestroyed()) {
webContents.send('exit-terminal', { pid: ptyProcess.pid, data })
}
})
webContents.on('destroyed', () => {
ptyProcess.kill()
})
ptyProcessMap.set(ptyProcess.pid, ptyProcess)
return {
Expand All @@ -97,7 +101,7 @@ async function createTerminal(frameId, { shell, cwd }) {

function handleTerminalMessages() {
ipcMain.handle('create-terminal', (event, data) => {
return createTerminal(event.frameId, data)
return createTerminal(event.sender, data)
})
ipcMain.handle('write-terminal', (event, pid, data) => {
const ptyProcess = ptyProcessMap.get(pid)
Expand Down
2 changes: 2 additions & 0 deletions renderer/hooks/terminal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function handleTerminalMessages() {
ipcRenderer.on('input-terminal', (event, data) => {
const tabs = unref(tabsRef)
const tab = tabs.find(item => item.pid === data.pid)
if (!tab) return
const xterm = tab.xterm
xterm.write(data.data)
// TODO: performance review
Expand All @@ -149,6 +150,7 @@ export function handleTerminalMessages() {
ipcRenderer.on('exit-terminal', (event, data) => {
const tabs = unref(tabsRef)
const tab = tabs.find(item => item.pid === data.pid)
if (!tab) return
const xterm = tab.xterm
if (xterm.element) {
const observer = createResizingObserver.cache.get()
Expand Down

0 comments on commit 0594744

Please sign in to comment.