Skip to content

Commit

Permalink
correctly release JS callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
emidoots committed Jul 1, 2019
1 parent 1455159 commit da9db7b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (h *HTML) removeProperties(prev *HTML) {
// Event listeners
for _, l := range prev.eventListeners {
h.node.Call("removeEventListener", l.Name, l.wrapper)
l.wrapper.Release()
}
}

Expand Down Expand Up @@ -1143,10 +1144,14 @@ func unmount(e ComponentOrHTML) {

// requestAnimationFrame calls the native JS function of the same name.
func requestAnimationFrame(callback func(float64)) int {
return global.Call("requestAnimationFrame", funcOf(func(this jsObject, args []jsObject) interface{} {
var cb jsFunc
cb = funcOf(func(this jsObject, args []jsObject) interface{} {
cb.Release()

callback(args[0].Float())
return undefined
})).Int()
})
return global.Call("requestAnimationFrame", cb).Int()
}

// RenderBody renders the given component as the document body. The given
Expand All @@ -1164,15 +1169,19 @@ func RenderBody(body Component) {
doc := global.Get("document")
if doc.Get("readyState").String() == "loading" {
// avoid duplicate body
doc.Call("addEventListener", "DOMContentLoaded", funcOf(func(this jsObject, args []jsObject) interface{} {
var cb jsFunc
cb = funcOf(func(this jsObject, args []jsObject) interface{} {
cb.Release()

doc.Set("body", nextRender.node)
mount(pendingMounts...)
if m, ok := body.(Mounter); ok {
mount(m)
}
requestAnimationFrame(batch.render)
return undefined
}))
})
doc.Call("addEventListener", "DOMContentLoaded", cb)
return
}
doc.Set("body", nextRender.node)
Expand Down

0 comments on commit da9db7b

Please sign in to comment.