]> git.giacomolli.uk - onechat/blobdiff - memory.go
Keep a built leaf when the view lags the tree
[onechat] / memory.go
index cb55cab9820de7f5cda2436921312c1d1cd31f56..c3e9f454177837e6352f35b098a1dc53a0d73ad9 100644 (file)
--- a/memory.go
+++ b/memory.go
@@ -272,15 +272,24 @@ func (m *Memory) Append(kind, text string) (Message, error) {
 }
 
 // addMessage builds the leaf of message when it needs no model call, or else
 }
 
 // addMessage builds the leaf of message when it needs no model call, or else
-// adds the leaf to the queue. Then it appends the leaf to the views.
+// adds the leaf to the queue. Then it appends the leaf to the views. A crash can
+// stop an append after the leaf build and before the view save, so a leaf that
+// is built already stays as it is, and restoreQueue settles its parent.
 func (m *Memory) addMessage(message Message) error {
 func (m *Memory) addMessage(message Message) error {
+       built, err := m.tree.Built(0, message.ID)
+       if err != nil {
+               return err
+       }
+
        leaf, free := freeLeaf(message)
        leaf, free := freeLeaf(message)
-       if free {
-               err := m.build(leaf)
+       switch {
+       case built:
+       case free:
+               err = m.build(leaf)
                if err != nil {
                        return err
                }
                if err != nil {
                        return err
                }
-       } else {
+       default:
                m.queue.AddLeaf(message.ID)
        }
 
                m.queue.AddLeaf(message.ID)
        }