lib.mx raw

   1  package lib
   2  
   3  // Worker is a spawn target. Its sole result is named `closed`, and that name
   4  // together with the type is the contract: the runtime binds the domain's
   5  // breaker into the result, and the target selects on it to learn that its
   6  // parent ended the domain.
   7  func Worker(in chan []byte, out chan []byte) (closed chan struct{}) {
   8  	for {
   9  		select {
  10  		case <-closed:
  11  			return closed
  12  		case msg := <-in:
  13  			out <- msg
  14  		}
  15  	}
  16  }
  17  
  18  // Plain has the shape of a spawn target but its result is not named `closed`,
  19  // so it is an ordinary function and may be called directly. Before the name
  20  // became part of the contract, the shape alone classified it as a target and
  21  // the direct call died in its prologue with "spawn target called directly".
  22  func Plain(in chan []byte) (out chan []byte) {
  23  	out = in
  24  	in <- []byte("plain")
  25  	return
  26  }
  27