github.com/wfusion/gofusion@v1.1.14/routine/promise.go (about)

     1  package routine
     2  
     3  import (
     4  	"math/rand"
     5  	"unsafe"
     6  )
     7  
     8  var (
     9  	ErrCancelled error = &CancelledError{}
    10  )
    11  
    12  // CancelledError present the Future object is cancelled.
    13  type CancelledError struct {
    14  }
    15  
    16  func (e *CancelledError) Error() string {
    17  	return "Task be cancelled"
    18  }
    19  
    20  // resultType present the type of Future final status.
    21  type resultType int
    22  
    23  const (
    24  	ResultSuccess resultType = iota
    25  	ResultFailure
    26  	ResultCancelled
    27  )
    28  
    29  // Result presents the result of a promise.
    30  // If Typ is ResultSuccess, Result field will present the returned value of Future task.
    31  // If Typ is ResultFailure, Result field will present a related error .
    32  // If Typ is ResultCancelled, Result field will be null.
    33  type Result struct {
    34  	Result any        //result of the promise
    35  	Typ    resultType //success, failure, or cancelled?
    36  }
    37  
    38  // promise presents an object that acts as a proxy for a result.
    39  // that is initially unknown, usually because the computation of its
    40  // value is yet incomplete (refer to wikipedia).
    41  // You can use Resolve/Reject/Cancel to set the final result of promise.
    42  // Future can return a read-only placeholder view of result.
    43  type promise struct {
    44  	*Future
    45  }
    46  
    47  // Cancel sets the status of promise to ResultCancelled.
    48  // If promise is cancelled, Get() will return nil and CANCELLED error.
    49  // All callback functions will be not called if promise is cancelled.
    50  func (p *promise) Cancel() (e error) {
    51  	return p.Future.Cancel()
    52  }
    53  
    54  // Resolve sets the value for promise, and the status will be changed to ResultSuccess.
    55  // if promise is resolved, Get() will return the value and nil error.
    56  func (p *promise) Resolve(v any) (e error) {
    57  	return p.setResult(&Result{v, ResultSuccess})
    58  }
    59  
    60  // Reject sets the error for promise, and the status will be changed to ResultFailure.
    61  // if promise is rejected, Get() will return nil and the related error value.
    62  func (p *promise) Reject(err error) (e error) {
    63  	return p.setResult(&Result{err, ResultFailure})
    64  }
    65  
    66  // OnSuccess registers a callback function that will be called when promise is resolved.
    67  // If promise is already resolved, the callback will immediately be called.
    68  // The value of promise will be parameter of Done callback function.
    69  func (p *promise) OnSuccess(callback func(v any)) *promise {
    70  	p.Future.OnSuccess(callback)
    71  	return p
    72  }
    73  
    74  // OnFailure registers a callback function that will be called when promise is rejected.
    75  // If promise is already rejected, the callback will immediately be called.
    76  // The error of promise will be parameter of Fail callback function.
    77  func (p *promise) OnFailure(callback func(v any)) *promise {
    78  	p.Future.OnFailure(callback)
    79  	return p
    80  }
    81  
    82  // OnComplete register a callback function that will be called when promise is rejected or resolved.
    83  // If promise is already rejected or resolved, the callback will immediately be called.
    84  // According to the status of promise, value or error will be parameter of Always callback function.
    85  // Value is the parameter if promise is resolved, or error is the parameter if promise is rejected.
    86  // Always callback will be not called if promise be called.
    87  func (p *promise) OnComplete(callback func(v any)) *promise {
    88  	p.Future.OnComplete(callback)
    89  	return p
    90  }
    91  
    92  // OnCancel registers a callback function that will be called when promise is cancelled.
    93  // If promise is already cancelled, the callback will immediately be called.
    94  func (p *promise) OnCancel(callback func()) *promise {
    95  	p.Future.OnCancel(callback)
    96  	return p
    97  }
    98  
    99  // NewPromise is factory function for promise
   100  func NewPromise() *promise {
   101  	val := &futureVal{
   102  		dones:   make([]func(v any), 0, 8),
   103  		fails:   make([]func(v any), 0, 8),
   104  		always:  make([]func(v any), 0, 4),
   105  		cancels: make([]func(), 0, 2),
   106  		pipes:   make([]*pipe, 0, 4),
   107  	}
   108  	f := &promise{
   109  		Future: &Future{
   110  			Id:    rand.Int(),
   111  			final: make(chan struct{}),
   112  			val:   unsafe.Pointer(val),
   113  		},
   114  	}
   115  	return f
   116  }