Break layout node into its own Wave Object (#21)

I am updating the layout node setup to write to its own wave object. 

The existing setup requires me to plumb the layout updates through every
time the tab gets updated, which produces a lot of annoying and
unintuitive design patterns. With this new setup, the tab object doesn't
get written to when the layout changes, only the layout object will get
written to. This prevents collisions when both the tab object and the
layout node object are getting updated, such as when a new block is
added or deleted.
This commit is contained in:
Evan Simkowitz
2024-06-05 17:21:40 -07:00
committed by GitHub
parent 28cef5f22f
commit f12e246c15
16 changed files with 149 additions and 84 deletions
+33 -19
View File
@@ -140,13 +140,19 @@ func CreateTab(ctx context.Context, workspaceId string, name string) (*Tab, erro
if ws == nil {
return nil, fmt.Errorf("workspace not found: %q", workspaceId)
}
layoutNodeId := uuid.NewString()
tab := &Tab{
OID: uuid.New().String(),
Name: name,
BlockIds: []string{},
OID: uuid.NewString(),
Name: name,
BlockIds: []string{},
LayoutNode: layoutNodeId,
}
layoutNode := &LayoutNode{
OID: layoutNodeId,
}
ws.TabIds = append(ws.TabIds, tab.OID)
DBInsert(tx.Context(), tab)
DBInsert(tx.Context(), layoutNode)
DBUpdate(tx.Context(), ws)
return tab, nil
})
@@ -154,7 +160,7 @@ func CreateTab(ctx context.Context, workspaceId string, name string) (*Tab, erro
func CreateWorkspace(ctx context.Context) (*Workspace, error) {
ws := &Workspace{
OID: uuid.New().String(),
OID: uuid.NewString(),
TabIds: []string{},
}
DBInsert(ctx, ws)
@@ -185,7 +191,7 @@ func CreateBlock(ctx context.Context, tabId string, blockDef *BlockDef, rtOpts *
if tab == nil {
return nil, fmt.Errorf("tab not found: %q", tabId)
}
blockId := uuid.New().String()
blockId := uuid.NewString()
blockData := &Block{
OID: blockId,
BlockDef: blockDef,
@@ -210,7 +216,7 @@ func findStringInSlice(slice []string, val string) int {
return -1
}
func DeleteBlock(ctx context.Context, tabId string, blockId string, newLayout any) error {
func DeleteBlock(ctx context.Context, tabId string, blockId string) error {
return WithTx(ctx, func(tx *TxWrap) error {
tab, _ := DBGet[*Tab](tx.Context(), tabId)
if tab == nil {
@@ -221,11 +227,8 @@ func DeleteBlock(ctx context.Context, tabId string, blockId string, newLayout an
return nil
}
tab.BlockIds = append(tab.BlockIds[:blockIdx], tab.BlockIds[blockIdx+1:]...)
if newLayout != nil {
tab.Layout = newLayout
}
DBUpdate(tx.Context(), tab)
DBDelete(tx.Context(), "block", blockId)
DBDelete(tx.Context(), OType_Block, blockId)
return nil
})
}
@@ -246,9 +249,10 @@ func CloseTab(ctx context.Context, workspaceId string, tabId string) error {
}
ws.TabIds = append(ws.TabIds[:tabIdx], ws.TabIds[tabIdx+1:]...)
DBUpdate(tx.Context(), ws)
DBDelete(tx.Context(), "tab", tabId)
DBDelete(tx.Context(), OType_Tab, tabId)
DBDelete(tx.Context(), OType_LayoutNode, tab.LayoutNode)
for _, blockId := range tab.BlockIds {
DBDelete(tx.Context(), "block", blockId)
DBDelete(tx.Context(), OType_Block, blockId)
}
return nil
})
@@ -300,11 +304,12 @@ func EnsureInitialData() error {
if clientCount > 0 {
return nil
}
windowId := uuid.New().String()
workspaceId := uuid.New().String()
tabId := uuid.New().String()
windowId := uuid.NewString()
workspaceId := uuid.NewString()
tabId := uuid.NewString()
layoutNodeId := uuid.NewString()
client := &Client{
OID: uuid.New().String(),
OID: uuid.NewString(),
MainWindowId: windowId,
}
err = DBInsert(ctx, client)
@@ -339,13 +344,22 @@ func EnsureInitialData() error {
return fmt.Errorf("error inserting workspace: %w", err)
}
tab := &Tab{
OID: tabId,
Name: "Tab-1",
BlockIds: []string{},
OID: tabId,
Name: "Tab-1",
BlockIds: []string{},
LayoutNode: layoutNodeId,
}
err = DBInsert(ctx, tab)
if err != nil {
return fmt.Errorf("error inserting tab: %w", err)
}
layoutNode := &LayoutNode{
OID: layoutNodeId,
}
err = DBInsert(ctx, layoutNode)
if err != nil {
return fmt.Errorf("error inserting layout node: %w", err)
}
return nil
}
+32 -11
View File
@@ -21,6 +21,15 @@ const (
UpdateType_Delete = "delete"
)
const (
OType_Client = "client"
OType_Window = "window"
OType_Workspace = "workspace"
OType_Tab = "tab"
OType_LayoutNode = "layout"
OType_Block = "block"
)
type WaveObjUpdate struct {
UpdateType string `json:"updatetype"`
OType string `json:"otype"`
@@ -51,7 +60,7 @@ type Client struct {
}
func (*Client) GetOType() string {
return "client"
return OType_Client
}
// stores the ui-context of the window
@@ -69,7 +78,7 @@ type Window struct {
}
func (*Window) GetOType() string {
return "window"
return OType_Window
}
type Workspace struct {
@@ -81,20 +90,31 @@ type Workspace struct {
}
func (*Workspace) GetOType() string {
return "workspace"
return OType_Workspace
}
type Tab struct {
OID string `json:"oid"`
Version int `json:"version"`
Name string `json:"name"`
Layout any `json:"layout,omitempty"`
BlockIds []string `json:"blockids"`
Meta map[string]any `json:"meta"`
OID string `json:"oid"`
Version int `json:"version"`
Name string `json:"name"`
LayoutNode string `json:"layoutNode"`
BlockIds []string `json:"blockids"`
Meta map[string]any `json:"meta"`
}
func (*Tab) GetOType() string {
return "tab"
return OType_Tab
}
type LayoutNode struct {
OID string `json:"oid"`
Version int `json:"version"`
Node any `json:"node,omitempty"`
Meta map[string]any `json:"meta,omitempty"`
}
func (*LayoutNode) GetOType() string {
return OType_LayoutNode
}
type FileDef struct {
@@ -138,7 +158,7 @@ type Block struct {
}
func (*Block) GetOType() string {
return "block"
return OType_Block
}
func AllWaveObjTypes() []reflect.Type {
@@ -148,5 +168,6 @@ func AllWaveObjTypes() []reflect.Type {
reflect.TypeOf(&Workspace{}),
reflect.TypeOf(&Tab{}),
reflect.TypeOf(&Block{}),
reflect.TypeOf(&LayoutNode{}),
}
}