1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix race condition on tab start

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-11-08 15:49:35 -05:00
parent 7d430b57ad
commit 09acd0206c
2 changed files with 15 additions and 18 deletions

View File

@ -58,15 +58,8 @@ export class TerminalApi extends WebSocketApi {
public onReady = new EventEmitter<[]>(); public onReady = new EventEmitter<[]>();
@observable public isReady = false; @observable public isReady = false;
public readonly url: string;
public static async new(options: TerminalApiQuery): Promise<TerminalApi> { constructor(protected query: TerminalApiQuery) {
const shellToken = await ipcRenderer.invoke("cluster:shell-api", getHostedClusterId(), options.id);
return new TerminalApi({ ...options, shellToken });
}
private constructor(query: TerminalApiQuery) {
super({ super({
logging: isDevelopment, logging: isDevelopment,
flushOnOpen: false, flushOnOpen: false,
@ -74,26 +67,30 @@ export class TerminalApi extends WebSocketApi {
}); });
makeObservable(this); makeObservable(this);
const { hostname, protocol, port } = location;
if (query.node) { if (query.node) {
query.type ||= "node"; query.type ||= "node";
} }
}
this.url = url.format({ async connect() {
this.emitStatus("Connecting ...");
const shellToken = await ipcRenderer.invoke("cluster:shell-api", getHostedClusterId(), this.query.id);
const { hostname, protocol, port } = location;
const socketUrl = url.format({
protocol: protocol.includes("https") ? "wss" : "ws", protocol: protocol.includes("https") ? "wss" : "ws",
hostname, hostname,
port, port,
pathname: "/api", pathname: "/api",
query, query: {
...this.query,
shellToken,
},
slashes: true, slashes: true,
}); });
}
connect() {
this.emitStatus("Connecting ...");
this.onData.addListener(this._onReady, { prepend: true }); this.onData.addListener(this._onReady, { prepend: true });
super.connect(this.url); super.connect(socketUrl);
} }
destroy() { destroy() {

View File

@ -65,12 +65,12 @@ export class TerminalStore extends Singleton {
}); });
} }
async connect(tabId: TabId) { connect(tabId: TabId) {
if (this.isConnected(tabId)) { if (this.isConnected(tabId)) {
return; return;
} }
const tab: ITerminalTab = dockStore.getTabById(tabId); const tab: ITerminalTab = dockStore.getTabById(tabId);
const api = await TerminalApi.new({ const api = new TerminalApi({
id: tabId, id: tabId,
node: tab.node, node: tab.node,
}); });