Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Terminate processes when closing panes, tabs, or application. Fixes #256
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed Mar 9, 2016
1 parent 2d57366 commit e95aa7b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const executors: Dictionary<(i: Job, a: string[]) => void> = {
setTimeout(() => job.session.clearJobs(), 0);
},
exit: (job: Job, args: string[]): void => {
job.session.remove();
job.session.close();
},
export: (job: Job, args: string[]): void => {
if (args.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default class Session extends EmitterWithUniqueID {
this.createJob();
}

remove(): void {
this.application.removeSession(this);
close(): void {
this.application.closeSession(this);
}

get directory(): string {
Expand Down
3 changes: 2 additions & 1 deletion src/main/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ if (app.dock) {
app.on("open-file", (event: Event, file: string) => getMainWindow().webContents.send("change-working-directory", file))
.on("ready", getMainWindow)
.on("activate", getMainWindow)
.on("mainWindow-all-closed", () => process.platform === "darwin" || app.quit());
.on("mainWindow-all-closed", () => process.platform === "darwin" || app.quit())
.on("will-quit", () => getMainWindow().webContents.send("will-quit"));

ipcMain.on("quit", app.quit);

Expand Down
43 changes: 27 additions & 16 deletions src/views/1_ApplicationComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class ApplicationComponent extends React.Component<{}, State> {
constructor(props: {}) {
super(props);

this.createTab();
this.addTab();
this.state = {sessions: this.activeTab.sessions};

$(window).resize(() => {
Expand All @@ -27,9 +27,9 @@ export default class ApplicationComponent extends React.Component<{}, State> {
}
});

ipcRenderer.on("change-working-directory", (event: Event, directory: string) =>
this.activeTab.activeSession().directory = directory
);
ipcRenderer
.on("change-working-directory", (event: Event, directory: string) => this.activeTab.activeSession().directory = directory)
.on("will-quit", () => this.close());
}

handleKeyDown(event: KeyboardEvent) {
Expand All @@ -47,7 +47,7 @@ export default class ApplicationComponent extends React.Component<{}, State> {
}

if (event.ctrlKey && event.keyCode === KeyCode.D) {
this.removeSession(this.activeTab.activeSession());
this.closeSession(this.activeTab.activeSession());

this.setState({sessions: this.activeTab.sessions});

Expand All @@ -72,7 +72,7 @@ export default class ApplicationComponent extends React.Component<{}, State> {

if (event.metaKey && event.keyCode === KeyCode.T) {
if (this.tabs.length < 9) {
this.createTab();
this.addTab();
this.setState({sessions: this.activeTab.sessions});
} else {
shell.beep();
Expand All @@ -82,7 +82,7 @@ export default class ApplicationComponent extends React.Component<{}, State> {
}

if (event.metaKey && event.keyCode === KeyCode.W) {
this.removeTab(this.activeTab);
this.closeTab(this.activeTab);
this.setState({sessions: this.activeTab.sessions});

event.stopPropagation();
Expand Down Expand Up @@ -121,9 +121,9 @@ export default class ApplicationComponent extends React.Component<{}, State> {

let sessions = this.state.sessions.map(session =>
<SessionComponent session={session}
key={session.id}
isActive={session === this.activeTab.activeSession()}
activate={() => {
key={session.id}
isActive={session === this.activeTab.activeSession()}
activate={() => {
this.activeTab.activateSession(session);
this.setState({ sessions: this.activeTab.sessions });
}}>
Expand All @@ -138,14 +138,14 @@ export default class ApplicationComponent extends React.Component<{}, State> {
);
}

public removeSession(sessionToRemove: Session) {
public closeSession(sessionToClose: Session) {
for (const tab of this.tabs) {
for (const session of tab.sessions) {
if (session === sessionToRemove) {
tab.removeSession(session);
if (session === sessionToClose) {
tab.closeSession(session);

if (tab.sessions.length === 0) {
this.removeTab(tab);
this.closeTab(tab);
}

this.setState({sessions: this.activeTab.sessions});
Expand All @@ -162,12 +162,23 @@ export default class ApplicationComponent extends React.Component<{}, State> {
return this.tabs[this.activeTabIndex];
}

private createTab(): void {
private addTab(): void {
this.tabs.push(new Tab(this));
this.activeTabIndex = this.tabs.length - 1;
}

private removeTab(tab: Tab): void {
private close(): void {
// Can't use forEach here because closeTab changes the array being iterated.
while (this.tabs.length) {
this.closeTab(this.tabs[0]);
}
}

private closeTab(tab: Tab): void {
// Can't use forEach here because closeSession changes the array being iterated.
while (tab.sessions.length) {
tab.closeSession(tab.sessions[0]);
}
_.pull(this.tabs, tab);

if (this.tabs.length === 0) {
Expand Down
7 changes: 5 additions & 2 deletions src/views/TabComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ export class Tab {
this.activeSessionIndex = this.sessions.length - 1;
}

removeSession(session: Session): void {
session.jobs.forEach(job => job.removeAllListeners());
closeSession(session: Session): void {
session.jobs.forEach(job => {
job.removeAllListeners();
job.interrupt();
});
session.removeAllListeners();

_.pull(this.sessions, session);
Expand Down

0 comments on commit e95aa7b

Please sign in to comment.