process static command / block commands. setmeta, setview

This commit is contained in:
sawka
2024-05-16 18:01:52 -07:00
parent 70dd4a1e30
commit d34ccfd7ab
5 changed files with 62 additions and 20 deletions
-3
View File
@@ -19,9 +19,6 @@ const MarkdownPreview = ({ contentAtom }: { contentAtom: jotai.Atom<Promise<stri
</div>
);
};
let counter = 0;
const PreviewView = ({ blockId }: { blockId: string }) => {
const blockDataAtom: jotai.Atom<BlockData> = blockDataMap.get(blockId);
const fileNameAtom = useBlockAtom(blockId, "preview:filename", () =>
+1
View File
@@ -39,6 +39,7 @@
&.view-preview-markdown {
align-items: start;
justify-content: start;
overflow: auto;
}
&.view-preview-text {
+22
View File
@@ -15,12 +15,16 @@ const CommandKey = "command"
const (
BlockCommand_Message = "message"
BlockCommand_SetView = "setview"
BlockCommand_SetMeta = "setmeta"
BlockCommand_Input = "controller:input"
)
var CommandToTypeMap = map[string]reflect.Type{
BlockCommand_Message: reflect.TypeOf(MessageCommand{}),
BlockCommand_Input: reflect.TypeOf(InputCommand{}),
BlockCommand_SetView: reflect.TypeOf(SetViewCommand{}),
BlockCommand_SetMeta: reflect.TypeOf(SetMetaCommand{}),
}
type BlockCommand interface {
@@ -67,3 +71,21 @@ type InputCommand struct {
func (ic *InputCommand) GetCommand() string {
return BlockCommand_Input
}
type SetViewCommand struct {
Command string `json:"command"`
View string `json:"view"`
}
func (svc *SetViewCommand) GetCommand() string {
return BlockCommand_SetView
}
type SetMetaCommand struct {
Command string `json:"command"`
Meta map[string]any `json:"meta"`
}
func (smc *SetMetaCommand) GetCommand() string {
return BlockCommand_SetMeta
}
+29 -12
View File
@@ -259,20 +259,8 @@ func (bc *BlockController) Run(bdata *BlockData) {
}
}()
messageCount := 0
for genCmd := range bc.InputCh {
switch cmd := genCmd.(type) {
case *MessageCommand:
fmt.Printf("MESSAGE: %s | %q\n", bc.BlockId, cmd.Message)
messageCount++
eventbus.SendEvent(application.WailsEvent{
Name: "block:ptydata",
Data: map[string]any{
"blockid": bc.BlockId,
"blockfile": "main",
"ptydata": base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("message %d\r\n", messageCount))),
},
})
case *InputCommand:
fmt.Printf("INPUT: %s | %q\n", bc.BlockId, cmd.InputData64)
if bc.ShellInputCh != nil {
@@ -317,3 +305,32 @@ func GetBlockController(blockId string) *BlockController {
defer globalLock.Unlock()
return blockControllerMap[blockId]
}
func ProcessStaticCommand(blockId string, cmdGen BlockCommand) {
switch cmd := cmdGen.(type) {
case *MessageCommand:
log.Printf("MESSAGE: %s | %q\n", blockId, cmd.Message)
case *SetViewCommand:
log.Printf("SETVIEW: %s | %q\n", blockId, cmd.View)
block := GetBlockData(blockId)
if block != nil {
block.WithLock(func() {
block.View = cmd.View
})
}
case *SetMetaCommand:
log.Printf("SETMETA: %s | %v\n", blockId, cmd.Meta)
block := GetBlockData(blockId)
if block != nil {
block.WithLock(func() {
for k, v := range cmd.Meta {
if v == nil {
delete(block.Meta, k)
continue
}
block.Meta[k] = v
}
})
}
}
}
+10 -5
View File
@@ -5,6 +5,7 @@ package blockservice
import (
"fmt"
"strings"
"github.com/wavetermdev/thenextwave/pkg/blockcontroller"
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
@@ -52,14 +53,18 @@ func (bs *BlockService) GetBlockData(blockId string) (map[string]any, error) {
}
func (bs *BlockService) SendCommand(blockId string, cmdMap map[string]any) error {
bc := blockcontroller.GetBlockController(blockId)
if bc == nil {
return fmt.Errorf("block controller not found for block %q", blockId)
}
cmd, err := blockcontroller.ParseCmdMap(cmdMap)
if err != nil {
return fmt.Errorf("error parsing command map: %w", err)
}
bc.InputCh <- cmd
if strings.HasPrefix(cmd.GetCommand(), "controller:") {
bc := blockcontroller.GetBlockController(blockId)
if bc == nil {
return fmt.Errorf("block controller not found for block %q", blockId)
}
bc.InputCh <- cmd
} else {
blockcontroller.ProcessStaticCommand(blockId, cmd)
}
return nil
}