move CreateX functions to wcore (#275)

This commit is contained in:
Mike Sawka 2024-08-26 15:17:37 -07:00 committed by GitHub
parent 4e68211f22
commit f28bdccb5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 151 additions and 140 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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";

View File

@ -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

View File

@ -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,

View File

@ -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 {

View File

@ -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,

View File

@ -33,7 +33,7 @@ var ExtraTypes = []any{
waveobj.UIContext{},
eventbus.WSEventType{},
eventbus.WSFileEventData{},
eventbus.WSLayoutActionData{},
waveobj.LayoutActionData{},
filestore.WaveFile{},
wconfig.SettingsConfigType{},
wconfig.TermThemesConfigType{},

View File

@ -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"`

View File

@ -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
}

View File

@ -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,

View File

@ -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
}