Fix infinite recursion

This commit is contained in:
Tulir Asokan 2019-06-15 18:03:28 +03:00
parent 0f08c49df4
commit ef509eb308
2 changed files with 27 additions and 15 deletions

View File

@ -115,10 +115,11 @@ type Room struct {
cache *RoomCache cache *RoomCache
// Lock for state and other room stuff. // Lock for state and other room stuff.
lock sync.RWMutex lock sync.RWMutex
// Function to call when room state is unloaded. // Pre/post un/load hooks
onUnload func() bool preUnload func() bool
// Function to call when room state is loaded. preLoad func() bool
onLoad func() bool postUnload func()
postLoad func()
// Whether or not the room state has changed // Whether or not the room state has changed
changed bool changed bool
@ -143,7 +144,7 @@ func (room *Room) Load() {
if room.Loaded() { if room.Loaded() {
return return
} }
if room.onLoad != nil && !room.onLoad() { if room.preLoad != nil && !room.preLoad() {
return return
} }
room.lock.Lock() room.lock.Lock()
@ -178,6 +179,9 @@ func (room *Room) load() {
debug.Print("Failed to decode room state:", err) debug.Print("Failed to decode room state:", err)
} }
room.changed = false room.changed = false
if room.postLoad != nil {
room.postLoad()
}
} }
func (room *Room) Touch() { func (room *Room) Touch() {
@ -185,7 +189,7 @@ func (room *Room) Touch() {
} }
func (room *Room) Unload() bool { func (room *Room) Unload() bool {
if room.onUnload != nil && !room.onUnload() { if room.preUnload != nil && !room.preUnload() {
return false return false
} }
debug.Print("Unloading", room.ID) debug.Print("Unloading", room.ID)
@ -196,15 +200,26 @@ func (room *Room) Unload() bool {
room.canonicalAliasCache = "" room.canonicalAliasCache = ""
room.firstMemberCache = nil room.firstMemberCache = nil
room.secondMemberCache = nil room.secondMemberCache = nil
if room.postUnload != nil {
room.postUnload()
}
return true return true
} }
func (room *Room) SetOnUnload(fn func() bool) { func (room *Room) SetPreUnload(fn func() bool) {
room.onUnload = fn room.preUnload = fn
} }
func (room *Room) SetOnLoad(fn func() bool) { func (room *Room) SetPreLoad(fn func() bool) {
room.onLoad = fn room.preLoad = fn
}
func (room *Room) SetPostUnload(fn func()) {
room.postUnload = fn
}
func (room *Room) SetPostLoad(fn func()) {
room.postLoad = fn
} }
func (room *Room) Save() { func (room *Room) Save() {

View File

@ -93,17 +93,14 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView {
config: parent.config, config: parent.config,
} }
view.content = NewMessageView(view) view.content = NewMessageView(view)
view.Room.SetOnUnload(func() bool { view.Room.SetPreUnload(func() bool {
if view.parent.currentRoom == view { if view.parent.currentRoom == view {
return false return false
} }
view.content.Unload() view.content.Unload()
return true return true
}) })
view.Room.SetOnLoad(func() bool { view.Room.SetPostLoad(view.loadTyping)
view.loadTyping()
return true
})
view.input. view.input.
SetBackgroundColor(tcell.ColorDefault). SetBackgroundColor(tcell.ColorDefault).