From be1a8bddbfec44e8c17833518e2153c33fa5023d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 19 May 2020 03:28:14 +0900 Subject: [PATCH] uidriver/js: Bug fix: Create goroutine for a function passed to rAF This is necessary not to cause dead-lock. Updates #1161 --- internal/uidriver/js/ui.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/uidriver/js/ui.go b/internal/uidriver/js/ui.go index 63c96ec67..8a7db84ff 100644 --- a/internal/uidriver/js/ui.go +++ b/internal/uidriver/js/ui.go @@ -184,10 +184,10 @@ func (u *UserInterface) loop(context driver.UIContext) <-chan error { resStopAudioCh := make(chan struct{}) var cf js.Func - f := func(this js.Value, args []js.Value) interface{} { + f := func() { if u.contextLost { requestAnimationFrame.Invoke(cf) - return nil + return } if err := u.update(); err != nil { @@ -196,21 +196,23 @@ func (u *UserInterface) loop(context driver.UIContext) <-chan error { errCh <- err close(errCh) - return nil + return } if u.vsync { requestAnimationFrame.Invoke(cf) } else { setTimeout.Invoke(cf, 0) } - return nil + return } // TODO: Should cf be released after the game ends? - cf = js.FuncOf(f) + cf = js.FuncOf(func(this js.Value, args []js.Value) interface{} { + go f() + return nil + }) + // Call f asyncly to be async since ch is used in f. - go func() { - f(js.Value{}, nil) - }() + go f() // Run another loop to watch suspended() as the above update function is never called when the tab is hidden. // To check the document's visiblity, visibilitychange event should usually be used. However, this event is