-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
terminal.store.ts
122 lines (98 loc) · 3.03 KB
/
terminal.store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { autorun, observable } from "mobx";
import { autobind } from "../../utils";
import { Terminal } from "./terminal";
import { TerminalApi } from "../../api/terminal-api";
import { dockStore, IDockTab, TabId, TabKind } from "./dock.store";
import { WebSocketApiState } from "../../api/websocket-api";
export interface ITerminalTab extends IDockTab {
node?: string; // activate node shell mode
}
export function isTerminalTab(tab: IDockTab) {
return tab && tab.kind === TabKind.TERMINAL;
}
export function createTerminalTab(tabParams: Partial<ITerminalTab> = {}) {
return dockStore.createTab({
kind: TabKind.TERMINAL,
title: `Terminal`,
...tabParams
});
}
@autobind()
export class TerminalStore {
protected terminals = new Map<TabId, Terminal>();
protected connections = observable.map<TabId, TerminalApi>();
constructor() {
// connect active tab
autorun(() => {
const { selectedTab, isOpen } = dockStore;
if (!isTerminalTab(selectedTab)) return;
if (isOpen) {
this.connect(selectedTab.id);
}
});
// disconnect closed tabs
autorun(() => {
const currentTabs = dockStore.tabs.map(tab => tab.id);
for (const [tabId] of this.connections) {
if (!currentTabs.includes(tabId)) this.disconnect(tabId);
}
});
}
async connect(tabId: TabId) {
if (this.isConnected(tabId)) {
return;
}
const tab: ITerminalTab = dockStore.getTabById(tabId);
const api = new TerminalApi({
id: tabId,
node: tab.node,
});
const terminal = new Terminal(tabId, api);
this.connections.set(tabId, api);
this.terminals.set(tabId, terminal);
}
disconnect(tabId: TabId) {
if (!this.isConnected(tabId)) {
return;
}
const terminal = this.terminals.get(tabId);
const terminalApi = this.connections.get(tabId);
terminal.destroy();
terminalApi.destroy();
this.connections.delete(tabId);
this.terminals.delete(tabId);
}
reconnect(tabId: TabId) {
const terminalApi = this.connections.get(tabId);
if (terminalApi) terminalApi.connect();
}
isConnected(tabId: TabId) {
return !!this.connections.get(tabId);
}
isDisconnected(tabId: TabId) {
const terminalApi = this.connections.get(tabId);
if (terminalApi) {
return terminalApi.readyState === WebSocketApiState.CLOSED;
}
}
sendCommand(command: string, options: { enter?: boolean; newTab?: boolean; tabId?: TabId } = {}) {
const { enter, newTab, tabId } = options;
const { selectTab, getTabById } = dockStore;
const tab = tabId && getTabById(tabId);
if (tab) selectTab(tabId);
if (newTab) createTerminalTab();
const terminalApi = this.connections.get(dockStore.selectedTabId);
if (terminalApi) {
terminalApi.sendCommand(command + (enter ? "\r" : ""));
}
}
getTerminal(tabId: TabId) {
return this.terminals.get(tabId);
}
reset() {
[...this.connections].forEach(([tabId]) => {
this.disconnect(tabId);
});
}
}
export const terminalStore = new TerminalStore();