2024-12-02 20:44:08 +01:00
|
|
|
package wcore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-12-03 18:38:46 +01:00
|
|
|
"log"
|
2024-12-02 20:44:08 +01:00
|
|
|
"time"
|
|
|
|
|
2024-12-03 18:38:46 +01:00
|
|
|
"github.com/google/uuid"
|
2024-12-02 20:44:08 +01:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/panichandler"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/telemetry"
|
2024-12-03 18:38:46 +01:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
|
2024-12-02 20:44:08 +01:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wps"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wshrpc"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateSubBlock(ctx context.Context, blockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
|
|
|
if blockDef == nil {
|
|
|
|
return nil, fmt.Errorf("blockDef is nil")
|
|
|
|
}
|
|
|
|
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
|
|
|
return nil, fmt.Errorf("no view provided for new block")
|
|
|
|
}
|
2024-12-03 18:38:46 +01:00
|
|
|
blockData, err := createSubBlockObj(ctx, blockId, blockDef)
|
2024-12-02 20:44:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating sub block: %w", err)
|
|
|
|
}
|
|
|
|
return blockData, nil
|
|
|
|
}
|
|
|
|
|
2024-12-03 18:38:46 +01:00
|
|
|
func createSubBlockObj(ctx context.Context, parentBlockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
|
|
|
return wstore.WithTxRtn(ctx, func(tx *wstore.TxWrap) (*waveobj.Block, error) {
|
|
|
|
parentBlock, _ := wstore.DBGet[*waveobj.Block](tx.Context(), parentBlockId)
|
|
|
|
if parentBlock == nil {
|
|
|
|
return nil, fmt.Errorf("parent block not found: %q", parentBlockId)
|
|
|
|
}
|
|
|
|
blockId := uuid.NewString()
|
|
|
|
blockData := &waveobj.Block{
|
|
|
|
OID: blockId,
|
|
|
|
ParentORef: waveobj.MakeORef(waveobj.OType_Block, parentBlockId).String(),
|
|
|
|
BlockDef: blockDef,
|
|
|
|
RuntimeOpts: nil,
|
|
|
|
Meta: blockDef.Meta,
|
|
|
|
}
|
|
|
|
wstore.DBInsert(tx.Context(), blockData)
|
|
|
|
parentBlock.SubBlockIds = append(parentBlock.SubBlockIds, blockId)
|
|
|
|
wstore.DBUpdate(tx.Context(), parentBlock)
|
|
|
|
return blockData, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-02 20:44:08 +01:00
|
|
|
func CreateBlock(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts) (*waveobj.Block, error) {
|
|
|
|
if blockDef == nil {
|
|
|
|
return nil, fmt.Errorf("blockDef is nil")
|
|
|
|
}
|
|
|
|
if blockDef.Meta == nil || blockDef.Meta.GetString(waveobj.MetaKey_View, "") == "" {
|
|
|
|
return nil, fmt.Errorf("no view provided for new block")
|
|
|
|
}
|
2024-12-03 18:38:46 +01:00
|
|
|
blockData, err := createBlockObj(ctx, tabId, blockDef, rtOpts)
|
2024-12-02 20:44:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating block: %w", err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer panichandler.PanicHandler("CreateBlock:telemetry")
|
|
|
|
blockView := blockDef.Meta.GetString(waveobj.MetaKey_View, "")
|
|
|
|
if blockView == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tctx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
telemetry.UpdateActivity(tctx, wshrpc.ActivityUpdate{
|
|
|
|
Renderers: map[string]int{blockView: 1},
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
return blockData, nil
|
|
|
|
}
|
|
|
|
|
2024-12-03 18:38:46 +01:00
|
|
|
func createBlockObj(ctx context.Context, tabId string, blockDef *waveobj.BlockDef, rtOpts *waveobj.RuntimeOpts) (*waveobj.Block, error) {
|
|
|
|
return wstore.WithTxRtn(ctx, func(tx *wstore.TxWrap) (*waveobj.Block, error) {
|
|
|
|
tab, _ := wstore.DBGet[*waveobj.Tab](tx.Context(), tabId)
|
|
|
|
if tab == nil {
|
|
|
|
return nil, fmt.Errorf("tab not found: %q", tabId)
|
|
|
|
}
|
|
|
|
blockId := uuid.NewString()
|
|
|
|
blockData := &waveobj.Block{
|
|
|
|
OID: blockId,
|
|
|
|
ParentORef: waveobj.MakeORef(waveobj.OType_Tab, tabId).String(),
|
|
|
|
BlockDef: blockDef,
|
|
|
|
RuntimeOpts: rtOpts,
|
|
|
|
Meta: blockDef.Meta,
|
|
|
|
}
|
|
|
|
wstore.DBInsert(tx.Context(), blockData)
|
|
|
|
tab.BlockIds = append(tab.BlockIds, blockId)
|
|
|
|
wstore.DBUpdate(tx.Context(), tab)
|
|
|
|
return blockData, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-02 20:44:08 +01:00
|
|
|
func DeleteBlock(ctx context.Context, blockId string) error {
|
|
|
|
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting block: %w", err)
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(block.SubBlockIds) > 0 {
|
|
|
|
for _, subBlockId := range block.SubBlockIds {
|
|
|
|
err := DeleteBlock(ctx, subBlockId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-03 18:38:46 +01:00
|
|
|
parentBlockCount, err := deleteBlockObj(ctx, blockId)
|
2024-12-02 20:44:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting block: %w", err)
|
|
|
|
}
|
2024-12-03 18:38:46 +01:00
|
|
|
log.Printf("DeleteBlock: parentBlockCount: %d", parentBlockCount)
|
|
|
|
parentORef := waveobj.ParseORefNoErr(block.ParentORef)
|
|
|
|
|
|
|
|
if parentORef.OType == waveobj.OType_Tab {
|
|
|
|
if parentBlockCount == 0 {
|
|
|
|
// if parent tab has no blocks, delete the tab
|
|
|
|
log.Printf("DeleteBlock: parent tab has no blocks, deleting tab %s", parentORef.OID)
|
|
|
|
parentWorkspaceId, err := wstore.DBFindWorkspaceForTabId(ctx, parentORef.OID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error finding workspace for tab to delete %s: %w", parentORef.OID, err)
|
|
|
|
}
|
|
|
|
newActiveTabId, err := DeleteTab(ctx, parentWorkspaceId, parentORef.OID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting tab %s: %w", parentORef.OID, err)
|
|
|
|
}
|
|
|
|
SendActiveTabUpdate(ctx, parentWorkspaceId, newActiveTabId)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-12-02 20:44:08 +01:00
|
|
|
go blockcontroller.StopBlockController(blockId)
|
|
|
|
sendBlockCloseEvent(blockId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-03 18:38:46 +01:00
|
|
|
// returns the updated block count for the parent object
|
|
|
|
func deleteBlockObj(ctx context.Context, blockId string) (int, error) {
|
|
|
|
return wstore.WithTxRtn(ctx, func(tx *wstore.TxWrap) (int, error) {
|
|
|
|
block, err := wstore.DBGet[*waveobj.Block](tx.Context(), blockId)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("error getting block: %w", err)
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
return -1, fmt.Errorf("block not found: %q", blockId)
|
|
|
|
}
|
|
|
|
if len(block.SubBlockIds) > 0 {
|
|
|
|
return -1, fmt.Errorf("block has subblocks, must delete subblocks first")
|
|
|
|
}
|
|
|
|
parentORef := waveobj.ParseORefNoErr(block.ParentORef)
|
|
|
|
parentBlockCount := -1
|
|
|
|
if parentORef != nil {
|
|
|
|
if parentORef.OType == waveobj.OType_Tab {
|
|
|
|
tab, _ := wstore.DBGet[*waveobj.Tab](tx.Context(), parentORef.OID)
|
|
|
|
if tab != nil {
|
|
|
|
tab.BlockIds = utilfn.RemoveElemFromSlice(tab.BlockIds, blockId)
|
|
|
|
wstore.DBUpdate(tx.Context(), tab)
|
|
|
|
parentBlockCount = len(tab.BlockIds)
|
|
|
|
}
|
|
|
|
} else if parentORef.OType == waveobj.OType_Block {
|
|
|
|
parentBlock, _ := wstore.DBGet[*waveobj.Block](tx.Context(), parentORef.OID)
|
|
|
|
if parentBlock != nil {
|
|
|
|
parentBlock.SubBlockIds = utilfn.RemoveElemFromSlice(parentBlock.SubBlockIds, blockId)
|
|
|
|
wstore.DBUpdate(tx.Context(), parentBlock)
|
|
|
|
parentBlockCount = len(parentBlock.SubBlockIds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wstore.DBDelete(tx.Context(), waveobj.OType_Block, blockId)
|
|
|
|
return parentBlockCount, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-02 20:44:08 +01:00
|
|
|
func sendBlockCloseEvent(blockId string) {
|
|
|
|
waveEvent := wps.WaveEvent{
|
|
|
|
Event: wps.Event_BlockClose,
|
|
|
|
Scopes: []string{
|
|
|
|
waveobj.MakeORef(waveobj.OType_Block, blockId).String(),
|
|
|
|
},
|
|
|
|
Data: blockId,
|
|
|
|
}
|
|
|
|
wps.Broker.Publish(waveEvent)
|
|
|
|
}
|