|
| 1 | +// This is auto generated code |
| 2 | +package async |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// Promise represents an eventual completion of an ansynchronous operation |
| 11 | +// and its resulting value. Promise can be either fulfilled or failed and |
| 12 | +// it can happen only one time. All Promise operations are thread-safe. |
| 13 | +// |
| 14 | +// To create a promise use: `&BigIntPromise{}` |
| 15 | +type BigIntPromise struct { |
| 16 | + mutex sync.Mutex |
| 17 | + successFn func(*big.Int) |
| 18 | + failureFn func(error) |
| 19 | + completeFn func(*big.Int, error) |
| 20 | + |
| 21 | + isComplete bool |
| 22 | + value *big.Int |
| 23 | + err error |
| 24 | +} |
| 25 | + |
| 26 | +// OnSuccess registers a function to be called when the Promise |
| 27 | +// has been fulfilled. In case of a failed Promise, function is not |
| 28 | +// called at all. OnSuccess is a non-blocking operation. Only one on success |
| 29 | +// function can be registered for a Promise. If the Promise has been already |
| 30 | +// fulfilled, the function is called immediatelly. |
| 31 | +func (p *BigIntPromise) OnSuccess(onSuccess func(*big.Int)) *BigIntPromise { |
| 32 | + p.mutex.Lock() |
| 33 | + defer p.mutex.Unlock() |
| 34 | + |
| 35 | + p.successFn = onSuccess |
| 36 | + |
| 37 | + if p.isComplete && p.err == nil { |
| 38 | + p.callSuccessFn() |
| 39 | + } |
| 40 | + |
| 41 | + return p |
| 42 | +} |
| 43 | + |
| 44 | +// OnFailure registers a function to be called when the Promise |
| 45 | +// execution failed. In case of a fulfilled Promise, function is not |
| 46 | +// called at all. OnFailure is a non-blocking operation. Only one on failure |
| 47 | +// function can be registered for a Promise. If the Promise has already failed, |
| 48 | +// the function is called immediatelly. |
| 49 | +func (p *BigIntPromise) OnFailure(onFailure func(error)) *BigIntPromise { |
| 50 | + p.mutex.Lock() |
| 51 | + defer p.mutex.Unlock() |
| 52 | + |
| 53 | + p.failureFn = onFailure |
| 54 | + |
| 55 | + if p.isComplete && p.err != nil { |
| 56 | + p.callFailureFn() |
| 57 | + } |
| 58 | + |
| 59 | + return p |
| 60 | +} |
| 61 | + |
| 62 | +// OnComplete registers a function to be called when the Promise |
| 63 | +// execution completed no matter if it succeded or failed. |
| 64 | +// In case of a successful execution, error passed to the callback |
| 65 | +// function is nil. In case of a failed execution, there is no |
| 66 | +// value evaluated so the value parameter is nil. OnComplete is |
| 67 | +// a non-blocking operation. Only one on complete function can be |
| 68 | +// registered for a Promise. If the Promise has already completed, |
| 69 | +// the function is called immediatelly. |
| 70 | +func (p *BigIntPromise) OnComplete(onComplete func(*big.Int, error)) *BigIntPromise { |
| 71 | + p.mutex.Lock() |
| 72 | + defer p.mutex.Unlock() |
| 73 | + |
| 74 | + p.completeFn = onComplete |
| 75 | + |
| 76 | + if p.isComplete { |
| 77 | + p.callCompleteFn() |
| 78 | + } |
| 79 | + |
| 80 | + return p |
| 81 | +} |
| 82 | + |
| 83 | +// Fulfill can happen only once for a Promise and it results in calling |
| 84 | +// the OnSuccess callback, if registered. If Promise has been already |
| 85 | +// completed by either fulfilling or failing, this function reports |
| 86 | +// an error. |
| 87 | +func (p *BigIntPromise) Fulfill(value *big.Int) error { |
| 88 | + p.mutex.Lock() |
| 89 | + defer p.mutex.Unlock() |
| 90 | + |
| 91 | + if p.isComplete { |
| 92 | + return fmt.Errorf("promise already completed") |
| 93 | + } |
| 94 | + |
| 95 | + p.isComplete = true |
| 96 | + p.value = value |
| 97 | + |
| 98 | + p.callSuccessFn() |
| 99 | + p.callCompleteFn() |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// Fail can happen only once for a Promise and it results in calling |
| 105 | +// the OnFailure callback, if registered. If Promise has been already |
| 106 | +// completed by either fulfilling or failing, this function reports |
| 107 | +// an error. Also, this function reports an error if `err` parameter |
| 108 | +// is `nil`. |
| 109 | +func (p *BigIntPromise) Fail(err error) error { |
| 110 | + p.mutex.Lock() |
| 111 | + defer p.mutex.Unlock() |
| 112 | + |
| 113 | + if err == nil { |
| 114 | + return fmt.Errorf("error cannot be nil") |
| 115 | + } |
| 116 | + |
| 117 | + if p.isComplete { |
| 118 | + return fmt.Errorf("promise already completed") |
| 119 | + } |
| 120 | + |
| 121 | + p.isComplete = true |
| 122 | + p.err = err |
| 123 | + |
| 124 | + p.callFailureFn() |
| 125 | + p.callCompleteFn() |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func (p *BigIntPromise) callCompleteFn() { |
| 131 | + if p.completeFn != nil { |
| 132 | + go func() { |
| 133 | + p.completeFn(p.value, p.err) |
| 134 | + }() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (p *BigIntPromise) callSuccessFn() { |
| 139 | + if p.successFn != nil { |
| 140 | + go func() { |
| 141 | + p.successFn(p.value) |
| 142 | + }() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func (p *BigIntPromise) callFailureFn() { |
| 147 | + if p.failureFn != nil { |
| 148 | + go func() { |
| 149 | + p.failureFn(p.err) |
| 150 | + }() |
| 151 | + } |
| 152 | +} |
0 commit comments