Made event handlers more robust.
This commit is contained in:
parent
d874148760
commit
c40d950654
|
@ -39,6 +39,7 @@ func uitask() {
|
||||||
var stall = make(chan struct{})
|
var stall = make(chan struct{})
|
||||||
|
|
||||||
// This is the common code for running an event.
|
// This is the common code for running an event.
|
||||||
|
// It runs on the main thread without a message pump; it provides its own.
|
||||||
// TODO
|
// TODO
|
||||||
// - define event
|
// - define event
|
||||||
// - figure out how to return values from event handlers
|
// - figure out how to return values from event handlers
|
||||||
|
@ -50,7 +51,26 @@ func doevent(e event) {
|
||||||
close(c)
|
close(c)
|
||||||
}()
|
}()
|
||||||
for req := range c {
|
for req := range c {
|
||||||
issue(req)
|
// note: this is perform, not issue!
|
||||||
|
// doevent runs on the main thread without a message pump!
|
||||||
|
perform(req)
|
||||||
}
|
}
|
||||||
stall <- struct{}{} // leave event handler
|
// leave the event handler; leave it only after returning from an event handler so we must issue it like a normal Request
|
||||||
|
issue(&Request{
|
||||||
|
op: func() {
|
||||||
|
stall <- struct{}{}
|
||||||
|
},
|
||||||
|
// unfortunately, closing a nil channel causes a panic
|
||||||
|
// therefore, we have to make a dummy channel
|
||||||
|
// TODO add conditional checks to the request handler instead?
|
||||||
|
resp: make(chan interface{}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common code for performing a Request.
|
||||||
|
// This should run on the main thread.
|
||||||
|
// Implementations of issue() should call this.
|
||||||
|
func perform(req *Request) {
|
||||||
|
req.op()
|
||||||
|
close(req.resp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ func issue(req *Request) {
|
||||||
//export doissue
|
//export doissue
|
||||||
func doissue(data C.gpointer) C.gboolean {
|
func doissue(data C.gpointer) C.gboolean {
|
||||||
req := (*Request)(unsafe.Pointer(data))
|
req := (*Request)(unsafe.Pointer(data))
|
||||||
req.op()
|
perform(req)
|
||||||
close(req.resp)
|
|
||||||
return C.FALSE // don't repeat
|
return C.FALSE // don't repeat
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue