mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-02 18:39:05 +01:00
delete and simplify deleteblock (unify tab/subblock cases)
This commit is contained in:
parent
52cb0c6817
commit
a71195ee34
@ -65,11 +65,11 @@ func editorRun(cmd *cobra.Command, args []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
doneCh := make(chan bool)
|
doneCh := make(chan bool)
|
||||||
RpcClient.EventListener.On("blockclose", func(event *wps.WaveEvent) {
|
RpcClient.EventListener.On(wps.Event_BlockClose, func(event *wps.WaveEvent) {
|
||||||
if event.HasScope(blockRef.String()) {
|
if event.HasScope(blockRef.String()) {
|
||||||
close(doneCh)
|
close(doneCh)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
wshclient.EventSubCommand(RpcClient, wps.SubscriptionRequest{Event: "blockclose", Scopes: []string{blockRef.String()}}, nil)
|
wshclient.EventSubCommand(RpcClient, wps.SubscriptionRequest{Event: wps.Event_BlockClose, Scopes: []string{blockRef.String()}}, nil)
|
||||||
<-doneCh
|
<-doneCh
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
UPDATE db_block
|
UPDATE db_block
|
||||||
SET data = json_set(data, '$.parentoref', db_tab.oid)
|
SET data = json_set(data, '$.parentoref', 'tab:' || db_tab.oid)
|
||||||
FROM db_tab
|
FROM db_tab
|
||||||
WHERE db_block.oid IN (SELECT value FROM json_each(db_tab.data, '$.blockids'));
|
WHERE db_block.oid IN (SELECT value FROM json_each(db_tab.data, '$.blockids'));
|
||||||
|
@ -201,7 +201,7 @@ func (svc *ObjectService) DeleteBlock(uiContext waveobj.UIContext, blockId strin
|
|||||||
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
||||||
defer cancelFn()
|
defer cancelFn()
|
||||||
ctx = waveobj.ContextWithUpdates(ctx)
|
ctx = waveobj.ContextWithUpdates(ctx)
|
||||||
err := wcore.DeleteBlock(ctx, uiContext.ActiveTabId, blockId)
|
err := wcore.DeleteBlock(ctx, blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error deleting block: %w", err)
|
return nil, fmt.Errorf("error deleting block: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ func (c *Client) CreateVDomContext() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
wshclient.EventSubCommand(c.RpcClient, wps.SubscriptionRequest{Event: "blockclose", Scopes: []string{
|
wshclient.EventSubCommand(c.RpcClient, wps.SubscriptionRequest{Event: wps.Event_BlockClose, Scopes: []string{
|
||||||
waveobj.MakeORef("block", c.RpcContext.BlockId).String(),
|
waveobj.MakeORef("block", c.RpcContext.BlockId).String(),
|
||||||
}}, nil)
|
}}, nil)
|
||||||
c.RpcClient.EventListener.On("blockclose", func(event *wps.WaveEvent) {
|
c.RpcClient.EventListener.On("blockclose", func(event *wps.WaveEvent) {
|
||||||
|
@ -94,6 +94,14 @@ func ParseORef(orefStr string) (ORef, error) {
|
|||||||
return ORef{OType: otype, OID: oid}, nil
|
return ORef{OType: otype, OID: oid}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseORefNoErr(orefStr string) *ORef {
|
||||||
|
oref, err := ParseORef(orefStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &oref
|
||||||
|
}
|
||||||
|
|
||||||
type WaveObj interface {
|
type WaveObj interface {
|
||||||
GetOType() string // should not depend on object state (should work with nil value)
|
GetOType() string // should not depend on object state (should work with nil value)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
const DefaultTimeout = 2 * time.Second
|
const DefaultTimeout = 2 * time.Second
|
||||||
const DefaultActivateBlockTimeout = 60 * time.Second
|
const DefaultActivateBlockTimeout = 60 * time.Second
|
||||||
|
|
||||||
func DeleteBlock(ctx context.Context, tabId string, blockId string) error {
|
func DeleteBlock(ctx context.Context, blockId string) error {
|
||||||
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
block, err := wstore.DBMustGet[*waveobj.Block](ctx, blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting block: %w", err)
|
return fmt.Errorf("error getting block: %w", err)
|
||||||
@ -36,33 +36,28 @@ func DeleteBlock(ctx context.Context, tabId string, blockId string) error {
|
|||||||
}
|
}
|
||||||
if len(block.SubBlockIds) > 0 {
|
if len(block.SubBlockIds) > 0 {
|
||||||
for _, subBlockId := range block.SubBlockIds {
|
for _, subBlockId := range block.SubBlockIds {
|
||||||
err := DeleteSubBlock(ctx, blockId, subBlockId)
|
err := DeleteBlock(ctx, subBlockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = wstore.DeleteBlock(ctx, tabId, blockId)
|
err = wstore.DeleteBlock(ctx, blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error deleting block: %w", err)
|
return fmt.Errorf("error deleting block: %w", err)
|
||||||
}
|
}
|
||||||
go blockcontroller.StopBlockController(blockId)
|
go blockcontroller.StopBlockController(blockId)
|
||||||
sendBlockCloseEvent(tabId, blockId)
|
sendBlockCloseEvent(blockId)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// tabid is optional
|
func sendBlockCloseEvent(blockId string) {
|
||||||
func sendBlockCloseEvent(tabId string, blockId string) {
|
|
||||||
scopes := []string{
|
|
||||||
waveobj.MakeORef(waveobj.OType_Block, blockId).String(),
|
|
||||||
}
|
|
||||||
if tabId != "" {
|
|
||||||
scopes = append(scopes, waveobj.MakeORef(waveobj.OType_Tab, tabId).String())
|
|
||||||
}
|
|
||||||
waveEvent := wps.WaveEvent{
|
waveEvent := wps.WaveEvent{
|
||||||
Event: wps.Event_BlockClose,
|
Event: wps.Event_BlockClose,
|
||||||
Scopes: scopes,
|
Scopes: []string{
|
||||||
Data: blockId,
|
waveobj.MakeORef(waveobj.OType_Block, blockId).String(),
|
||||||
|
},
|
||||||
|
Data: blockId,
|
||||||
}
|
}
|
||||||
wps.Broker.Publish(waveEvent)
|
wps.Broker.Publish(waveEvent)
|
||||||
}
|
}
|
||||||
@ -77,7 +72,7 @@ func DeleteTab(ctx context.Context, workspaceId string, tabId string) error {
|
|||||||
}
|
}
|
||||||
// close blocks (sends events + stops block controllers)
|
// close blocks (sends events + stops block controllers)
|
||||||
for _, blockId := range tabData.BlockIds {
|
for _, blockId := range tabData.BlockIds {
|
||||||
err := DeleteBlock(ctx, tabId, blockId)
|
err := DeleteBlock(ctx, blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error deleting block %s: %w", blockId, err)
|
return fmt.Errorf("error deleting block %s: %w", blockId, err)
|
||||||
}
|
}
|
||||||
@ -224,32 +219,6 @@ func CreateClient(ctx context.Context) (*waveobj.Client, error) {
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSubBlock(ctx context.Context, parentBlockId string, 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 {
|
|
||||||
// recursively delete sub-blocks
|
|
||||||
for _, subBlockId := range block.SubBlockIds {
|
|
||||||
err := DeleteSubBlock(ctx, blockId, subBlockId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = wstore.DeleteSubBlock(ctx, parentBlockId, blockId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error deleting block: %w", err)
|
|
||||||
}
|
|
||||||
go blockcontroller.StopBlockController(blockId)
|
|
||||||
sendBlockCloseEvent("", blockId)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateSubBlock(ctx context.Context, blockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
func CreateSubBlock(ctx context.Context, blockId string, blockDef *waveobj.BlockDef) (*waveobj.Block, error) {
|
||||||
if blockDef == nil {
|
if blockDef == nil {
|
||||||
return nil, fmt.Errorf("blockDef is nil")
|
return nil, fmt.Errorf("blockDef is nil")
|
||||||
|
@ -392,7 +392,7 @@ func (ws *WshServer) DeleteBlockCommand(ctx context.Context, data wshrpc.Command
|
|||||||
if windowId == "" {
|
if windowId == "" {
|
||||||
return fmt.Errorf("no window found for tab")
|
return fmt.Errorf("no window found for tab")
|
||||||
}
|
}
|
||||||
err = wcore.DeleteBlock(ctx, tabId, data.BlockId)
|
err = wcore.DeleteBlock(ctx, data.BlockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error deleting block: %w", err)
|
return fmt.Errorf("error deleting block: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -146,31 +146,33 @@ func findStringInSlice(slice []string, val string) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteSubBlock(ctx context.Context, parentBlockId string, blockId string) error {
|
func DeleteBlock(ctx context.Context, blockId string) error {
|
||||||
return WithTx(ctx, func(tx *TxWrap) error {
|
return WithTx(ctx, func(tx *TxWrap) error {
|
||||||
parentBlock, _ := DBGet[*waveobj.Block](tx.Context(), parentBlockId)
|
block, err := DBGet[*waveobj.Block](tx.Context(), blockId)
|
||||||
if parentBlock != nil {
|
if err != nil {
|
||||||
parentBlock.SubBlockIds = utilfn.RemoveElemFromSlice(parentBlock.SubBlockIds, blockId)
|
return fmt.Errorf("error getting block: %w", err)
|
||||||
DBUpdate(tx.Context(), parentBlock)
|
|
||||||
}
|
}
|
||||||
DBDelete(tx.Context(), waveobj.OType_Block, blockId)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteBlock(ctx context.Context, tabId string, blockId string) error {
|
|
||||||
return WithTx(ctx, func(tx *TxWrap) error {
|
|
||||||
block, _ := DBGet[*waveobj.Block](tx.Context(), blockId)
|
|
||||||
if block == nil {
|
if block == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len(block.SubBlockIds) > 0 {
|
if len(block.SubBlockIds) > 0 {
|
||||||
return fmt.Errorf("block has subblocks, must delete subblocks first")
|
return fmt.Errorf("block has subblocks, must delete subblocks first")
|
||||||
}
|
}
|
||||||
tab, _ := DBGet[*waveobj.Tab](tx.Context(), tabId)
|
parentORef := waveobj.ParseORefNoErr(block.ParentORef)
|
||||||
if tab != nil {
|
if parentORef != nil {
|
||||||
tab.BlockIds = utilfn.RemoveElemFromSlice(tab.BlockIds, blockId)
|
if parentORef.OType == waveobj.OType_Tab {
|
||||||
DBUpdate(tx.Context(), tab)
|
tab, _ := DBGet[*waveobj.Tab](tx.Context(), parentORef.OID)
|
||||||
|
if tab != nil {
|
||||||
|
tab.BlockIds = utilfn.RemoveElemFromSlice(tab.BlockIds, blockId)
|
||||||
|
DBUpdate(tx.Context(), tab)
|
||||||
|
}
|
||||||
|
} else if parentORef.OType == waveobj.OType_Block {
|
||||||
|
parentBlock, _ := DBGet[*waveobj.Block](tx.Context(), parentORef.OID)
|
||||||
|
if parentBlock != nil {
|
||||||
|
parentBlock.SubBlockIds = utilfn.RemoveElemFromSlice(parentBlock.SubBlockIds, blockId)
|
||||||
|
DBUpdate(tx.Context(), parentBlock)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DBDelete(tx.Context(), waveobj.OType_Block, blockId)
|
DBDelete(tx.Context(), waveobj.OType_Block, blockId)
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user