diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-08-17 22:49:38 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-08-17 22:49:38 -0400 |
| commit | c71fb555b23178d2abeaee2a3a5eeb6f2db604aa (patch) | |
| tree | 7e633b57652e47265fc7222011f165a03ec96f11 | |
| parent | d858988ee600b273d2120c0d09cdaf7d78395157 (diff) | |
| download | gui-c71fb555b23178d2abeaee2a3a5eeb6f2db604aa.zip | |
move shared data structures to external module
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | mux.go | 12 | ||||
| -rw-r--r-- | share.go | 53 |
3 files changed, 8 insertions, 58 deletions
@@ -3,6 +3,7 @@ module github.com/faiface/gui go 1.22.4 require ( + git.samanthony.xyz/share v0.0.0 github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 github.com/fogleman/gg v1.3.0 github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 @@ -3,6 +3,8 @@ package gui import ( "image" "image/draw" + + "git.samanthony.xyz/share" ) // Mux can be used to multiplex an Env, let's call it a root Env. Mux implements a way to @@ -10,7 +12,7 @@ import ( // events and their draw functions get redirected to the root Env. type Mux struct { addEventsIn chan<- chan<- Event - size sharedVal[image.Rectangle] + size share.Val[image.Rectangle] draw chan<- func(draw.Image) image.Rectangle } @@ -20,7 +22,7 @@ type Mux struct { // created by the Mux. func NewMux(env Env) (mux *Mux, master Env) { addEventsIn := make(chan chan<- Event) - size := newSharedVal[image.Rectangle]() + size := share.NewVal[image.Rectangle]() drawChan := make(chan func(draw.Image) image.Rectangle) mux = &Mux{ addEventsIn: addEventsIn, @@ -33,7 +35,7 @@ func NewMux(env Env) (mux *Mux, master Env) { defer close(env.Draw()) defer close(addEventsIn) - defer size.close() + defer size.Close() defer func() { for _, eventsIn := range eventsIns { close(eventsIn) @@ -52,7 +54,7 @@ func NewMux(env Env) (mux *Mux, master Env) { return } if resize, ok := e.(Resize); ok { - size.set <- resize.Rectangle + size.Set <- resize.Rectangle } for _, eventsIn := range eventsIns { eventsIn <- e @@ -89,7 +91,7 @@ func (mux *Mux) makeEnv(master bool) Env { mux.addEventsIn <- eventsIn // make sure to always send a resize event to a new Env - eventsIn <- Resize{mux.size.get()} + eventsIn <- Resize{mux.size.Get()} go func() { func() { diff --git a/share.go b/share.go deleted file mode 100644 index 36b903b..0000000 --- a/share.go +++ /dev/null @@ -1,53 +0,0 @@ -package gui - -// sharedVal is a concurrent interface to a piece of shared data. -// -// A client can read the data by sending a channel via request, and the stored value will -// be sent back via the channel. The client is responsible for closing the channel. -// -// The stored value can be changed by sending the new value via set. Requests block until -// the first value is received on set. -// -// A sharedVal should be closed after use. -type sharedVal[T any] struct { - request chan<- chan T - set chan<- T -} - -func newSharedVal[T any]() sharedVal[T] { - request := make(chan chan T) - set := make(chan T) - go func() { - val := <-set // wait for initial value - for { - select { - case v, ok := <-set: - if !ok { // closed - return - } - val = v - case req, ok := <-request: - if !ok { // closed - return - } - go func() { // don't wait for client to receive - req <- val - }() - } - } - }() - return sharedVal[T]{request, set} -} - -// get makes a synchronous request and returns the stored value. -func (sv sharedVal[T]) get() T { - c := make(chan T) - defer close(c) - sv.request <- c - return <-c -} - -func (sv sharedVal[T]) close() { - close(sv.request) - close(sv.set) -} |