serialize (w/ actionid) the resize events

This commit is contained in:
sawka 2024-12-19 09:44:29 -08:00
parent aee35f0e01
commit e6315658e1
5 changed files with 88 additions and 47 deletions

View File

@ -46,6 +46,7 @@ type TermWrapOptions = {
export class TermWrap {
blockId: string;
ptyOffset: number;
pendingPtyOffset: number;
dataBytesProcessed: number;
terminal: Terminal;
connectElem: HTMLDivElement;
@ -56,6 +57,7 @@ export class TermWrap {
heldData: Uint8Array[];
handleResize_debounced: () => void;
hasResized: boolean;
isLoadingCache: boolean;
constructor(
blockId: string,
@ -64,6 +66,7 @@ export class TermWrap {
waveOptions: TermWrapOptions
) {
this.loaded = false;
this.isLoadingCache = false;
this.blockId = blockId;
this.ptyOffset = 0;
this.dataBytesProcessed = 0;
@ -165,11 +168,20 @@ export class TermWrap {
}
handleTermData(data: string) {
if (this.isLoadingCache) {
return;
}
if (!this.loaded) {
return;
}
const b64data = util.stringToBase64(data);
RpcApi.ControllerInputCommand(TabRpcClient, { blockid: this.blockId, inputdata64: b64data });
const actionId = util.getNextActionId();
RpcApi.ControllerInputCommand(TabRpcClient, {
blockid: this.blockId,
inputdata64: b64data,
feactionid: actionId,
pendingptyoffset: this.pendingPtyOffset,
});
}
addFocusListener(focusFn: () => void) {
@ -198,11 +210,14 @@ export class TermWrap {
let prtn = new Promise<void>((presolve, _) => {
resolve = presolve;
});
this.terminal.write(data, () => {
if (setPtyOffset != null) {
this.ptyOffset = setPtyOffset;
this.pendingPtyOffset = setPtyOffset;
} else {
this.ptyOffset += data.length;
this.pendingPtyOffset = this.ptyOffset + data.length;
}
this.terminal.write(data, () => {
this.ptyOffset = this.pendingPtyOffset;
if (setPtyOffset == null) {
this.dataBytesProcessed += data.length;
}
resolve();
@ -217,6 +232,8 @@ export class TermWrap {
if (cacheFile != null) {
ptyOffset = cacheFile.meta["ptyoffset"] ?? 0;
if (cacheData.byteLength > 0) {
try {
this.isLoadingCache = true;
const curTermSize: TermSize = { rows: this.terminal.rows, cols: this.terminal.cols };
const fileTermSize: TermSize = cacheFile.meta["termsize"];
let didResize = false;
@ -228,10 +245,13 @@ export class TermWrap {
this.terminal.resize(fileTermSize.cols, fileTermSize.rows);
didResize = true;
}
this.doTerminalWrite(cacheData, ptyOffset);
await this.doTerminalWrite(cacheData, ptyOffset);
if (didResize) {
this.terminal.resize(curTermSize.cols, curTermSize.rows);
}
} finally {
this.isLoadingCache = false;
}
}
}
const { data: mainData, fileInfo: mainFile } = await fetchWaveFile(this.blockId, TermFileName, ptyOffset);

View File

@ -116,6 +116,8 @@ declare global {
blockid: string;
inputdata64?: string;
signame?: string;
pendingptyoffset?: number;
feactionid?: string;
termsize?: TermSize;
};

View File

@ -65,6 +65,7 @@ type BlockInputUnion struct {
InputData []byte `json:"inputdata,omitempty"`
SigName string `json:"signame,omitempty"`
TermSize *waveobj.TermSize `json:"termsize,omitempty"`
FeActionId string `json:"feactionid,omitempty"`
}
type BlockController struct {
@ -80,6 +81,8 @@ type BlockController struct {
ShellProcExitCode int
RunLock *atomic.Bool
StatusVersion int
ProcessedToOffset int64
LastResizeActionId string
}
type BlockControllerRuntimeStatus struct {
@ -117,6 +120,16 @@ func (bc *BlockController) getShellProc() *shellexec.ShellProc {
return bc.ShellProc
}
func (bc *BlockController) TestAndSetResizeActionId(actionId string) bool {
bc.Lock.Lock()
defer bc.Lock.Unlock()
if actionId <= bc.LastResizeActionId {
return false
}
bc.LastResizeActionId = actionId
return true
}
type RunShellOpts struct {
TermSize waveobj.TermSize `json:"termsize,omitempty"`
}
@ -469,6 +482,8 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
shellProc.Cmd.Write(ic.InputData)
}
if ic.TermSize != nil {
ok := bc.TestAndSetResizeActionId(ic.FeActionId)
if ok {
err = setTermSize(ctx, bc.BlockId, *ic.TermSize)
if err != nil {
log.Printf("error setting pty size: %v\n", err)
@ -479,6 +494,7 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
}
}
}
}
}()
go func() {
defer panichandler.PanicHandler("blockcontroller:shellproc-output-loop")

View File

@ -315,6 +315,8 @@ type CommandBlockInputData struct {
BlockId string `json:"blockid" wshcontext:"BlockId"`
InputData64 string `json:"inputdata64,omitempty"`
SigName string `json:"signame,omitempty"`
PendingPtyOffset int64 `json:"pendingptyoffset,omitempty"`
FeActionId string `json:"feactionid,omitempty"`
TermSize *waveobj.TermSize `json:"termsize,omitempty"`
}

View File

@ -244,6 +244,7 @@ func (ws *WshServer) ControllerInputCommand(ctx context.Context, data wshrpc.Com
inputUnion := &blockcontroller.BlockInputUnion{
SigName: data.SigName,
TermSize: data.TermSize,
FeActionId: data.FeActionId,
}
if len(data.InputData64) > 0 {
inputBuf := make([]byte, base64.StdEncoding.DecodedLen(len(data.InputData64)))