From f28bdccb5db0075b755c407af707c66d64bddda7 Mon Sep 17 00:00:00 2001 From: Mike Sawka Date: Mon, 26 Aug 2024 15:17:37 -0700 Subject: [PATCH] move CreateX functions to wcore (#275) --- cmd/server/main-server.go | 3 +- frontend/app/store/global.ts | 2 +- frontend/types/gotypes.d.ts | 20 ++-- pkg/eventbus/eventbus.go | 9 -- pkg/service/clientservice/clientservice.go | 5 +- pkg/service/objectservice/objectservice.go | 14 +-- pkg/service/windowservice/windowservice.go | 6 +- pkg/tsgen/tsgen.go | 2 +- pkg/waveobj/wtype.go | 9 ++ pkg/wcore/wcore.go | 115 +++++++++++++++++++++ pkg/wshrpc/wshserver/wshserver.go | 22 +--- pkg/wstore/wstore.go | 84 --------------- 12 files changed, 151 insertions(+), 140 deletions(-) diff --git a/cmd/server/main-server.go b/cmd/server/main-server.go index 94c6a1f44..81efae9fe 100644 --- a/cmd/server/main-server.go +++ b/cmd/server/main-server.go @@ -26,6 +26,7 @@ import ( "github.com/wavetermdev/thenextwave/pkg/waveobj" "github.com/wavetermdev/thenextwave/pkg/wcloud" "github.com/wavetermdev/thenextwave/pkg/wconfig" + "github.com/wavetermdev/thenextwave/pkg/wcore" "github.com/wavetermdev/thenextwave/pkg/web" "github.com/wavetermdev/thenextwave/pkg/wps" "github.com/wavetermdev/thenextwave/pkg/wshrpc" @@ -212,7 +213,7 @@ func main() { log.Printf("error initializing wsh and shell-integration files: %v\n", err) } }() - err = wstore.EnsureInitialData() + err = wcore.EnsureInitialData() if err != nil { log.Printf("error ensuring initial data: %v\n", err) return diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index 31cd223c4..cfab6ce92 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -361,7 +361,7 @@ function handleWSEventMessage(msg: WSEventType) { return; } if (msg.eventtype == "layoutaction") { - const layoutAction: WSLayoutActionData = msg.data; + const layoutAction: LayoutActionData = msg.data; const tabId = layoutAction.tabid; const layoutModel = getLayoutModelForTabById(tabId); switch (layoutAction.actiontype) { diff --git a/frontend/types/gotypes.d.ts b/frontend/types/gotypes.d.ts index 3a8086fd0..f8c33a1dc 100644 --- a/frontend/types/gotypes.d.ts +++ b/frontend/types/gotypes.d.ts @@ -208,6 +208,16 @@ declare global { data64: string; }; + // waveobj.LayoutActionData + type LayoutActionData = { + tabid: string; + actiontype: string; + blockid: string; + nodesize?: number; + indexarr?: number[]; + magnified?: boolean; + }; + // waveobj.LayoutState type LayoutState = WaveObj & { rootnode?: any; @@ -525,16 +535,6 @@ declare global { data64: string; }; - // eventbus.WSLayoutActionData - type WSLayoutActionData = { - tabid: string; - actiontype: string; - blockid: string; - nodesize?: number; - indexarr?: number[]; - magnified?: boolean; - }; - // webcmd.WSRpcCommand type WSRpcCommand = { wscommand: "rpc"; diff --git a/pkg/eventbus/eventbus.go b/pkg/eventbus/eventbus.go index 6353aa809..2ea1ed922 100644 --- a/pkg/eventbus/eventbus.go +++ b/pkg/eventbus/eventbus.go @@ -56,15 +56,6 @@ const ( WSLayoutActionType_Remove = "delete" ) -type WSLayoutActionData struct { - TabId string `json:"tabid"` - ActionType string `json:"actiontype"` - BlockId string `json:"blockid"` - NodeSize uint `json:"nodesize,omitempty"` - IndexArr []int `json:"indexarr,omitempty"` - Magnified bool `json:"magnified,omitempty"` -} - var globalLock = &sync.Mutex{} var wsMap = make(map[string]*WindowWatchData) // websocketid => WindowWatchData diff --git a/pkg/service/clientservice/clientservice.go b/pkg/service/clientservice/clientservice.go index 86332b3ec..8dfdda62a 100644 --- a/pkg/service/clientservice/clientservice.go +++ b/pkg/service/clientservice/clientservice.go @@ -14,6 +14,7 @@ import ( "github.com/wavetermdev/thenextwave/pkg/service/objectservice" "github.com/wavetermdev/thenextwave/pkg/util/utilfn" "github.com/wavetermdev/thenextwave/pkg/waveobj" + "github.com/wavetermdev/thenextwave/pkg/wcore" "github.com/wavetermdev/thenextwave/pkg/wshrpc" "github.com/wavetermdev/thenextwave/pkg/wstore" ) @@ -63,7 +64,7 @@ func (cs *ClientService) GetWindow(windowId string) (*waveobj.Window, error) { } func (cs *ClientService) MakeWindow(ctx context.Context) (*waveobj.Window, error) { - return wstore.CreateWindow(ctx, nil) + return wcore.CreateWindow(ctx, nil) } func (cs *ClientService) GetAllConnStatus(ctx context.Context) ([]wshrpc.ConnStatus, error) { @@ -183,7 +184,7 @@ func (cs *ClientService) BootstrapStarterLayout(ctx context.Context) error { eventbus.SendEventToWindow(windowId, eventbus.WSEventType{ EventType: eventbus.WSEvent_LayoutAction, - Data: &eventbus.WSLayoutActionData{ + Data: &waveobj.LayoutActionData{ ActionType: "insertatindex", TabId: tabId, BlockId: blockData.OID, diff --git a/pkg/service/objectservice/objectservice.go b/pkg/service/objectservice/objectservice.go index 03e91acfe..9710ceb3a 100644 --- a/pkg/service/objectservice/objectservice.go +++ b/pkg/service/objectservice/objectservice.go @@ -83,21 +83,11 @@ func (svc *ObjectService) AddTabToWorkspace(uiContext waveobj.UIContext, tabName ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) defer cancelFn() ctx = waveobj.ContextWithUpdates(ctx) - windowData, err := wstore.DBMustGet[*waveobj.Window](ctx, uiContext.WindowId) - if err != nil { - return "", nil, fmt.Errorf("error getting window: %w", err) - } - tab, err := wstore.CreateTab(ctx, windowData.WorkspaceId, tabName) + tabId, err := wcore.CreateTab(ctx, uiContext.WindowId, tabName, activateTab) if err != nil { return "", nil, fmt.Errorf("error creating tab: %w", err) } - if activateTab { - err = wstore.SetActiveTab(ctx, uiContext.WindowId, tab.OID) - if err != nil { - return "", nil, fmt.Errorf("error setting active tab: %w", err) - } - } - return tab.OID, waveobj.ContextGetUpdatesRtn(ctx), nil + return tabId, waveobj.ContextGetUpdatesRtn(ctx), nil } func (svc *ObjectService) UpdateWorkspaceTabIds_Meta() tsgenmeta.MethodMeta { diff --git a/pkg/service/windowservice/windowservice.go b/pkg/service/windowservice/windowservice.go index ea330e5ba..1b422e43e 100644 --- a/pkg/service/windowservice/windowservice.go +++ b/pkg/service/windowservice/windowservice.go @@ -119,7 +119,7 @@ func (svc *WindowService) MoveBlockToNewWindow(ctx context.Context, currentTabId if !foundBlock { return nil, fmt.Errorf("block not found in current tab") } - newWindow, err := wstore.CreateWindow(ctx, nil) + newWindow, err := wcore.CreateWindow(ctx, nil) if err != nil { return nil, fmt.Errorf("error creating window: %w", err) } @@ -137,7 +137,7 @@ func (svc *WindowService) MoveBlockToNewWindow(ctx context.Context, currentTabId } eventbus.SendEventToWindow(curWindowId, eventbus.WSEventType{ EventType: eventbus.WSEvent_LayoutAction, - Data: eventbus.WSLayoutActionData{ + Data: waveobj.LayoutActionData{ ActionType: eventbus.WSLayoutActionType_Remove, TabId: currentTabId, BlockId: blockId, @@ -145,7 +145,7 @@ func (svc *WindowService) MoveBlockToNewWindow(ctx context.Context, currentTabId }) eventbus.SendEventToWindow(newWindow.OID, eventbus.WSEventType{ EventType: eventbus.WSEvent_LayoutAction, - Data: eventbus.WSLayoutActionData{ + Data: waveobj.LayoutActionData{ ActionType: eventbus.WSLayoutActionType_Insert, TabId: newWindow.ActiveTabId, BlockId: blockId, diff --git a/pkg/tsgen/tsgen.go b/pkg/tsgen/tsgen.go index 9f7fa88f3..6fc4e1d61 100644 --- a/pkg/tsgen/tsgen.go +++ b/pkg/tsgen/tsgen.go @@ -33,7 +33,7 @@ var ExtraTypes = []any{ waveobj.UIContext{}, eventbus.WSEventType{}, eventbus.WSFileEventData{}, - eventbus.WSLayoutActionData{}, + waveobj.LayoutActionData{}, filestore.WaveFile{}, wconfig.SettingsConfigType{}, wconfig.TermThemesConfigType{}, diff --git a/pkg/waveobj/wtype.go b/pkg/waveobj/wtype.go index 181b971d8..522d1e75b 100644 --- a/pkg/waveobj/wtype.go +++ b/pkg/waveobj/wtype.go @@ -173,6 +173,15 @@ func (t *Tab) GetBlockORefs() []ORef { return rtn } +type LayoutActionData struct { + TabId string `json:"tabid"` + ActionType string `json:"actiontype"` + BlockId string `json:"blockid"` + NodeSize uint `json:"nodesize,omitempty"` + IndexArr []int `json:"indexarr,omitempty"` + Magnified bool `json:"magnified,omitempty"` +} + type LayoutState struct { OID string `json:"oid"` Version int `json:"version"` diff --git a/pkg/wcore/wcore.go b/pkg/wcore/wcore.go index e3e831e21..d8e346a45 100644 --- a/pkg/wcore/wcore.go +++ b/pkg/wcore/wcore.go @@ -9,6 +9,7 @@ import ( "fmt" "time" + "github.com/google/uuid" "github.com/wavetermdev/thenextwave/pkg/blockcontroller" "github.com/wavetermdev/thenextwave/pkg/waveobj" "github.com/wavetermdev/thenextwave/pkg/wps" @@ -68,3 +69,117 @@ func DeleteTab(ctx context.Context, workspaceId string, tabId string) error { return nil } + +// returns tabid +func CreateTab(ctx context.Context, windowId string, tabName string, activateTab bool) (string, error) { + windowData, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId) + if err != nil { + return "", fmt.Errorf("error getting window: %w", err) + } + tab, err := wstore.CreateTab(ctx, windowData.WorkspaceId, tabName) + if err != nil { + return "", fmt.Errorf("error creating tab: %w", err) + } + if activateTab { + err = wstore.SetActiveTab(ctx, windowId, tab.OID) + if err != nil { + return "", fmt.Errorf("error setting active tab: %w", err) + } + } + return tab.OID, nil +} + +func CreateWindow(ctx context.Context, winSize *waveobj.WinSize) (*waveobj.Window, error) { + windowId := uuid.NewString() + workspaceId := uuid.NewString() + if winSize == nil { + winSize = &waveobj.WinSize{ + Width: 1200, + Height: 800, + } + } + window := &waveobj.Window{ + OID: windowId, + WorkspaceId: workspaceId, + Pos: waveobj.Point{ + X: 100, + Y: 100, + }, + WinSize: *winSize, + } + err := wstore.DBInsert(ctx, window) + if err != nil { + return nil, fmt.Errorf("error inserting window: %w", err) + } + ws := &waveobj.Workspace{ + OID: workspaceId, + Name: "w" + workspaceId[0:8], + } + err = wstore.DBInsert(ctx, ws) + if err != nil { + return nil, fmt.Errorf("error inserting workspace: %w", err) + } + _, err = CreateTab(ctx, windowId, "T1", true) + if err != nil { + return nil, fmt.Errorf("error inserting tab: %w", err) + } + client, err := wstore.DBGetSingleton[*waveobj.Client](ctx) + if err != nil { + return nil, fmt.Errorf("error getting client: %w", err) + } + client.WindowIds = append(client.WindowIds, windowId) + err = wstore.DBUpdate(ctx, client) + if err != nil { + return nil, fmt.Errorf("error updating client: %w", err) + } + return wstore.DBMustGet[*waveobj.Window](ctx, windowId) +} + +func EnsureInitialData() error { + // does not need to run in a transaction since it is called on startup + ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second) + defer cancelFn() + client, err := wstore.DBGetSingleton[*waveobj.Client](ctx) + if err == wstore.ErrNotFound { + client, err = CreateClient(ctx) + if err != nil { + return fmt.Errorf("error creating client: %w", err) + } + } + if len(client.WindowIds) > 0 { + return nil + } + _, err = CreateWindow(ctx, &waveobj.WinSize{Height: 0, Width: 0}) + if err != nil { + return fmt.Errorf("error creating window: %w", err) + } + return nil +} + +func CreateClient(ctx context.Context) (*waveobj.Client, error) { + client := &waveobj.Client{ + OID: uuid.NewString(), + WindowIds: []string{}, + } + err := wstore.DBInsert(ctx, client) + if err != nil { + return nil, fmt.Errorf("error inserting client: %w", err) + } + return client, nil +} + +func CreateBlock(ctx context.Context, createBlockCmd wshrpc.CommandCreateBlockData) (*waveobj.ORef, error) { + tabId := createBlockCmd.TabId + blockData, err := wstore.CreateBlock(ctx, tabId, createBlockCmd.BlockDef, createBlockCmd.RtOpts) + if err != nil { + return nil, fmt.Errorf("error creating block: %w", err) + } + controllerName := blockData.Meta.GetString(waveobj.MetaKey_Controller, "") + if controllerName != "" { + err = blockcontroller.StartBlockController(ctx, createBlockCmd.TabId, blockData.OID) + if err != nil { + return nil, fmt.Errorf("error starting block controller: %w", err) + } + } + return &waveobj.ORef{OType: waveobj.OType_Block, OID: blockData.OID}, nil +} diff --git a/pkg/wshrpc/wshserver/wshserver.go b/pkg/wshrpc/wshserver/wshserver.go index 6f2deef1b..951ad2fdf 100644 --- a/pkg/wshrpc/wshserver/wshserver.go +++ b/pkg/wshrpc/wshserver/wshserver.go @@ -177,7 +177,6 @@ func SavePlotData(ctx context.Context, blockId string, history string) error { } func (ws *WshServer) GetMetaCommand(ctx context.Context, data wshrpc.CommandGetMetaData) (waveobj.MetaMapType, error) { - log.Printf("calling meta: %s\n", data.ORef) obj, err := wstore.DBGetORef(ctx, data.ORef) if err != nil { return nil, fmt.Errorf("error getting object: %w", err) @@ -263,21 +262,10 @@ func sendWStoreUpdatesToEventBus(updates waveobj.UpdatesRtnType) { func (ws *WshServer) CreateBlockCommand(ctx context.Context, data wshrpc.CommandCreateBlockData) (*waveobj.ORef, error) { ctx = waveobj.ContextWithUpdates(ctx) tabId := data.TabId - if data.TabId != "" { - tabId = data.TabId - } - blockData, err := wstore.CreateBlock(ctx, tabId, data.BlockDef, data.RtOpts) + blockRef, err := wcore.CreateBlock(ctx, data) if err != nil { return nil, fmt.Errorf("error creating block: %w", err) } - controllerName := blockData.Meta.GetString(waveobj.MetaKey_Controller, "") - if controllerName != "" { - // TODO - err = blockcontroller.StartBlockController(ctx, data.TabId, blockData.OID) - if err != nil { - return nil, fmt.Errorf("error starting block controller: %w", err) - } - } updates := waveobj.ContextGetUpdatesRtn(ctx) sendWStoreUpdatesToEventBus(updates) windowId, err := wstore.DBFindWindowForTabId(ctx, tabId) @@ -289,14 +277,14 @@ func (ws *WshServer) CreateBlockCommand(ctx context.Context, data wshrpc.Command } eventbus.SendEventToWindow(windowId, eventbus.WSEventType{ EventType: eventbus.WSEvent_LayoutAction, - Data: &eventbus.WSLayoutActionData{ + Data: &waveobj.LayoutActionData{ ActionType: "insert", TabId: tabId, - BlockId: blockData.OID, + BlockId: blockRef.OID, Magnified: data.Magnified, }, }) - return &waveobj.ORef{OType: waveobj.OType_Block, OID: blockData.OID}, nil + return &waveobj.ORef{OType: waveobj.OType_Block, OID: blockRef.OID}, nil } func (ws *WshServer) SetViewCommand(ctx context.Context, data wshrpc.CommandBlockSetViewData) error { @@ -442,7 +430,7 @@ func (ws *WshServer) DeleteBlockCommand(ctx context.Context, data wshrpc.Command } eventbus.SendEventToWindow(windowId, eventbus.WSEventType{ EventType: eventbus.WSEvent_LayoutAction, - Data: &eventbus.WSLayoutActionData{ + Data: &waveobj.LayoutActionData{ ActionType: "delete", TabId: tabId, BlockId: data.BlockId, diff --git a/pkg/wstore/wstore.go b/pkg/wstore/wstore.go index 514cec0a5..e46c24edf 100644 --- a/pkg/wstore/wstore.go +++ b/pkg/wstore/wstore.go @@ -6,7 +6,6 @@ package wstore import ( "context" "fmt" - "time" "github.com/google/uuid" "github.com/wavetermdev/thenextwave/pkg/util/utilfn" @@ -189,56 +188,6 @@ func UpdateObjectMeta(ctx context.Context, oref waveobj.ORef, meta waveobj.MetaM }) } -func CreateWindow(ctx context.Context, winSize *waveobj.WinSize) (*waveobj.Window, error) { - windowId := uuid.NewString() - workspaceId := uuid.NewString() - if winSize == nil { - winSize = &waveobj.WinSize{ - Width: 1200, - Height: 800, - } - } - window := &waveobj.Window{ - OID: windowId, - WorkspaceId: workspaceId, - Pos: waveobj.Point{ - X: 100, - Y: 100, - }, - WinSize: *winSize, - } - err := DBInsert(ctx, window) - if err != nil { - return nil, fmt.Errorf("error inserting window: %w", err) - } - ws := &waveobj.Workspace{ - OID: workspaceId, - Name: "w" + workspaceId[0:8], - } - err = DBInsert(ctx, ws) - if err != nil { - return nil, fmt.Errorf("error inserting workspace: %w", err) - } - tab, err := CreateTab(ctx, ws.OID, "T1") - if err != nil { - return nil, fmt.Errorf("error inserting tab: %w", err) - } - err = SetActiveTab(ctx, window.OID, tab.OID) - if err != nil { - return nil, fmt.Errorf("error setting active tab: %w", err) - } - client, err := DBGetSingleton[*waveobj.Client](ctx) - if err != nil { - return nil, fmt.Errorf("error getting client: %w", err) - } - client.WindowIds = append(client.WindowIds, windowId) - err = DBUpdate(ctx, client) - if err != nil { - return nil, fmt.Errorf("error updating client: %w", err) - } - return DBMustGet[*waveobj.Window](ctx, windowId) -} - func MoveBlockToTab(ctx context.Context, currentTabId string, newTabId string, blockId string) error { return WithTx(ctx, func(tx *TxWrap) error { currentTab, _ := DBGet[*waveobj.Tab](tx.Context(), currentTabId) @@ -260,36 +209,3 @@ func MoveBlockToTab(ctx context.Context, currentTabId string, newTabId string, b return nil }) } - -func CreateClient(ctx context.Context) (*waveobj.Client, error) { - client := &waveobj.Client{ - OID: uuid.NewString(), - WindowIds: []string{}, - } - err := DBInsert(ctx, client) - if err != nil { - return nil, fmt.Errorf("error inserting client: %w", err) - } - return client, nil -} - -func EnsureInitialData() error { - // does not need to run in a transaction since it is called on startup - ctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second) - defer cancelFn() - client, err := DBGetSingleton[*waveobj.Client](ctx) - if err == ErrNotFound { - client, err = CreateClient(ctx) - if err != nil { - return fmt.Errorf("error creating client: %w", err) - } - } - if len(client.WindowIds) > 0 { - return nil - } - _, err = CreateWindow(ctx, &waveobj.WinSize{Height: 0, Width: 0}) - if err != nil { - return fmt.Errorf("error creating window: %w", err) - } - return nil -}