From 31f90d54011180df218bcac65da4bf1b8bf0aed5 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 4 Jul 2022 22:18:36 -0700 Subject: [PATCH] checkpoint --- src/main.tsx | 9 ++- src/session.ts | 171 ++++++++++++++++++++++++++++++++++++------------- src/sh2.ts | 4 +- 3 files changed, 133 insertions(+), 51 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 75c36bec..aa18d163 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -306,7 +306,7 @@ class SessionView extends React.Component<{session : SessionType}, {}> { let session = this.props.session; let window = session.getActiveWindow(); let lines = window.lines; - if (lines == null || lines.length == 0) { + if (lines == null) { return; } let lastLine = lines[lines.length-1]; @@ -321,15 +321,14 @@ class SessionView extends React.Component<{session : SessionType}, {}> { if (window == null) { return
(no active window {session.activeWindowId.get()})
; } - let lines = window.lines || []; - let idx = 0; - if (session.loading.get()) { + if (session.loading.get() || window.linesLoading.get()) { return
(loading)
; } + let idx = 0; return (
- +
diff --git a/src/session.ts b/src/session.ts index 7d30f232..f953b548 100644 --- a/src/session.ts +++ b/src/session.ts @@ -6,15 +6,6 @@ import {TermWrap} from "./term"; import {v4 as uuidv4} from "uuid"; var GlobalUser = "sawka"; -var GSessionId = "47445c53-cfcf-4943-8339-2c04447f20a1"; -var GWindowId = "1"; - -var GlobalLines = mobx.observable.box([ - {sessionid: GSessionId, windowid: GWindowId, lineid: 1, userid: "sawka", ts: 1424631125000, linetype: "text", text: "hello"}, - {sessionid: GSessionId, windowid: GWindowId, lineid: 2, userid: "sawka", ts: 1654631125000, linetype: "text", text: "again"}, - {sessionid: GSessionId, windowid: GWindowId, lineid: 3, userid: "sawka", ts: 1655403002683, linetype: "text", text: "more..."}, - {sessionid: GSessionId, windowid: GWindowId, lineid: 4, userid: "sawka", ts: 1655513202683, linetype: "cmd", cmdid: "e74a7db7-58f5-47ef-b351-364c7ba2bfbb", cmdtext: "ls"}, -]); function makeTermKey(sessionId : string, cmdId : string, windowId : string, lineid : number) : string { return sprintf("%s/%s/%s/%s", sessionId, cmdId, windowId, lineid); @@ -37,6 +28,14 @@ function getLineId(line : LineType) : string { return sprintf("%s-%s-%s", line.sessionid, line.windowid, line.lineid); } +type RemoteType = { + remotetype : string, + remoteid : string, + remotename : string, + status : string, + cwd : string, +}; + type SessionRemoteDataType = { sessionid : string, windowid : string, @@ -50,8 +49,7 @@ type WindowDataType = { windowid : string, name : string, curremote : string, - remotes : SessionRemoteDataType[], - lines : LineType[], + lines : mobx.IObservableValue, version : number, }; @@ -74,9 +72,9 @@ class Session { termMap : Record = {}; termMapById : Record = {}; history : HistoryItem[] = []; - curRemote : string; - curDir : string; loading : mobx.IObservableValue = mobx.observable.box(false); + remotes : SessionRemoteDataType[] = []; + globalRemotes : RemoteType[]; constructor() { } @@ -86,42 +84,118 @@ class Session { if (window == null) { return null; } - for (let i=0; i handleJsonFetchResponse(url, resp)).then((data) => { + mobx.action(() => { + window.lines.replace(data.data || []); + window.linesLoading.set(false); + })(); + return; + }).catch((err) => { + console.log(sprintf("error getting window=%s lines", windowid), err) + }); + } + + setActiveWindow(windowid : string) { + this.activeWindowId.set(windowid); + this.loadWindowLines(windowid); + } + submitCommand(windowid : string, commandStr : string) { let url = sprintf("http://localhost:8080/api/run-command"); let data = {type: "fecmd", sessionid: this.sessionId, windowid: windowid, cmdstr: commandStr, userid: GlobalUser}; - data.remotestate = this.getWindowCurRemoteData(); + let curWindow = this.getCurWindow(); + if (curWindow == null) { + throw new Error(sprintf("invalid current window=%s", this.activeWindowId)); + } + data.remotestate = this.getWindowCurRemoteData(this.activeWindowId); if (data.remotestate == null) { - throw new Error(sprintf("no remotestate found for windowid:%s, cannot submit command", windowid)); + throw new Error(sprintf("no remotestate found for windowid:%s (remote=%s), cannot submit command", windowid, window.curremote)); } fetch(url, {method: "post", body: JSON.stringify(data)}).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { - let lines = GlobalLines.get(); - data.data.line.isnew = true; - lines.push(data.data.line); + if (data.data != null && data.data.line != null) { + this.addLine(data.data.line); + } })(); }).catch((err) => { console.log("error calling run-command", err) }); } + addLine(line : LineType) { + if (line.sessionid != this.sessionId) { + return; + } + let window = this.getWindowById(line.windowid); + if (window == null) { + return; + } + mobx.action(() => { + let lines = window.lines; + let lineIdx = 0; + for (lineIdx=0; lineIdx line.lineid) { + break; + } + } + if (lineIdx == lines.length) { + window.lines.push(line); + return; + } + window.lines.splice(lineIdx, 0, line); + })(); + return; + } + addToHistory(hitem : HistoryItem) { this.history.push(hitem); } @@ -186,20 +260,43 @@ class Session { var DefaultSession : Session = new Session(); -function loadDefaultSession() { +function initSession() { if (DefaultSession.loading.get()) { return; } + let remotesLoaded = false; + let sessionLoaded = false; + DefaultSession.loading.set(true); + fetch("http://localhost:8080/api/get-remotes").then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { + mobx.action(() => { + DefaultSession.globalRemotes = data.data + remotesLoaded = true; + if (remotesLoaded && sessionLoaded) { + DefaultSession.loading.set(false); + } + })(); + }).catch((err) => { + console.log("error calling get-remotes", err) + }); + let usp = new URLSearchParams({name: "default"}); let url = sprintf("http://localhost:8080/api/get-session?") + usp.toString(); fetch(url).then((resp) => handleJsonFetchResponse(url, resp)).then((data) => { mobx.action(() => { let sdata = data.data; - DefaultSession.loading.set(false); + DefaultSession.sessionId = sdata.sessionid; DefaultSession.name = sdata.name; - DefaultSession.windows = sdata.windows; - DefaultSession.activeWindowId.set(sdata.windows[0].windowid); - console.log("session", sdata); + DefaultSession.windows = sdata.windows || []; + for (let i=0; i { console.log("error calling get-session", err) @@ -208,23 +305,9 @@ function loadDefaultSession() { function getDefaultSession() : Session { return DefaultSession; - - if (DefaultSession != null) { - return DefaultSession; - } - let windowLines = GlobalLines.get(); - let session = new Session(); - session.sessionId = GSessionId; - session.name = "default"; - session.activeWindowId = GWindowId; - session.windows = [ - {sessionid: GSessionId, windowid: GWindowId, name: "default", lines: windowLines}, - ]; - DefaultSession = session; - return session; } window.getDefaultSession = getDefaultSession; -export {Session, getDefaultSession, getLineId, loadDefaultSession}; +export {Session, getDefaultSession, getLineId, initSession}; export type {LineType, WindowDataType}; diff --git a/src/sh2.ts b/src/sh2.ts index 2258c368..5dba839d 100644 --- a/src/sh2.ts +++ b/src/sh2.ts @@ -5,14 +5,14 @@ import {Terminal} from 'xterm'; import {Main} from "./main"; import {GlobalWS} from "./ws"; import {v4 as uuidv4} from "uuid"; -import {loadDefaultSession} from "./session"; +import {initSession} from "./session"; let VERSION = __SHVERSION__; window.ScriptHausClientId = uuidv4(); document.addEventListener("DOMContentLoaded", () => { - loadDefaultSession(); + initSession(); GlobalWS.reconnect(); let reactElem = React.createElement(Main, null, null); let elem = document.getElementById("app");