github.com/storacha/go-ucanto@v0.7.2/core/receipt/fx/fx.go (about)

     1  package fx
     2  
     3  import (
     4  	"github.com/storacha/go-ucanto/core/invocation"
     5  	"github.com/storacha/go-ucanto/ucan"
     6  )
     7  
     8  type Effects interface {
     9  	Fork() []Effect
    10  	Join() Effect
    11  }
    12  
    13  type effects struct {
    14  	fork []Effect
    15  	join Effect
    16  }
    17  
    18  func (fx effects) Fork() []Effect {
    19  	return fx.fork
    20  }
    21  
    22  func (fx effects) Join() Effect {
    23  	return fx.join
    24  }
    25  
    26  // Option is an option configuring effects.
    27  type Option func(fx *effects) error
    28  
    29  // WithFork configures the forks for the receipt.
    30  func WithFork(forks ...Effect) Option {
    31  	return func(fx *effects) error {
    32  		fx.fork = forks
    33  		return nil
    34  	}
    35  }
    36  
    37  // WithJoin configures the join for the receipt.
    38  func WithJoin(join Effect) Option {
    39  	return func(fx *effects) error {
    40  		fx.join = join
    41  		return nil
    42  	}
    43  }
    44  
    45  func NewEffects(opts ...Option) Effects {
    46  	var fx effects
    47  	for _, opt := range opts {
    48  		opt(&fx)
    49  	}
    50  	return fx
    51  }
    52  
    53  // Effect is either an invocation or a link to one.
    54  type Effect struct {
    55  	invocation invocation.Invocation
    56  	link       ucan.Link
    57  }
    58  
    59  // Invocation returns the invocation if it is available.
    60  func (e Effect) Invocation() (invocation.Invocation, bool) {
    61  	return e.invocation, e.invocation != nil
    62  }
    63  
    64  // Link returns the invocation root link.
    65  func (e Effect) Link() ucan.Link {
    66  	if e.invocation != nil {
    67  		return e.invocation.Link()
    68  	}
    69  	return e.link
    70  }
    71  
    72  func FromLink(link ucan.Link) Effect {
    73  	return Effect{nil, link}
    74  }
    75  
    76  func FromInvocation(invocation invocation.Invocation) Effect {
    77  	return Effect{invocation, nil}
    78  }