Last active
August 29, 2015 14:08
-
-
Save emidoots/0b7c40e8fb6b566e491f to your computer and use it in GitHub Desktop.
(gopherjs) Channel Based Main Thread Ownership
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/gopherjs/gopherjs/js" | |
) | |
var MainLoopChan = make(chan func(), 1) | |
type Window struct{} | |
func (w *Window) renderFrame(args ...interface{}) { | |
go func() { | |
fmt.Println("renderFrame()") | |
MainLoopChan <- func() { | |
fmt.Println("glFooBar()...") | |
js.Global.Call("requestAnimationFrame", w.renderFrame) | |
} | |
}() | |
} | |
func NewWindow() *Window { | |
w := &Window{} | |
// Window creation. | |
js.Global.Call("requestAnimationFrame", w.renderFrame) | |
return w | |
} | |
func main() { | |
NewWindow() | |
// Main loop. | |
for { | |
fmt.Println("main loop: waiting for func") | |
select { | |
case f := <-MainLoopChan: | |
fmt.Println("main loop: exec func") | |
//gopherjs:blocking | |
f() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment