github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/experimental/close_example_test.go (about)

     1  package experimental_test
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/bananabytelabs/wazero/experimental"
     7  )
     8  
     9  var ctx context.Context
    10  
    11  // This shows how to implement a custom cleanup task on close.
    12  func Example_closeNotifier() {
    13  	closeCh := make(chan struct{})
    14  	ctx = experimental.WithCloseNotifier(
    15  		ctx,
    16  		experimental.CloseNotifyFunc(func(context.Context, uint32) { close(closeCh) }),
    17  	)
    18  
    19  	// ... create module, do some work. Sometime later in another goroutine:
    20  
    21  	select {
    22  	case <-closeCh:
    23  		// do some cleanup
    24  	default:
    25  		// do some more work with the module
    26  	}
    27  }