| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2019-2024 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Future::AsyncAwait::Awaitable 0.71; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | C<Future::AsyncAwait::Awaitable> - the interface required by C<Future::AsyncAwait> | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | =for highlighter language=perl | ||||||||||
| 18 | |||||||||||
| 19 | This module documents the method interface required by C<Future::AsyncAwait> | ||||||||||
| 20 | to operate on future instances returned by expressions invoked by the C<await> | ||||||||||
| 21 | keyword, and returned by functions declared by C<async sub>. This information | ||||||||||
| 22 | is largely of relevance to implementors of other module integrations, event | ||||||||||
| 23 | systems, or similar. It is not necessary to make regular use of the syntax | ||||||||||
| 24 | provided by the module when working with existing event systems. | ||||||||||
| 25 | |||||||||||
| 26 | The methods required by this interface are all capitalised and prefixed with | ||||||||||
| 27 | C<AWAIT_...>, ensuring they are unlikely to clash with existing methods on a | ||||||||||
| 28 | class which may have differing semantics. | ||||||||||
| 29 | |||||||||||
| 30 | =head2 Role::Tiny | ||||||||||
| 31 | |||||||||||
| 32 | If L<Role::Tiny> is available, this module declares itself to be a role that | ||||||||||
| 33 | requires the following named methods. The role supplies no code to the applied | ||||||||||
| 34 | class, but can be useful for checking that you have in fact implemented all of | ||||||||||
| 35 | the required methods. | ||||||||||
| 36 | |||||||||||
| 37 | =head2 Conformance Test | ||||||||||
| 38 | |||||||||||
| 39 | To assist implementors of alternative future-like classes, an API conformance | ||||||||||
| 40 | test suite is provided by L<Test::Future::AsyncAwait::Awaitable>. You may find | ||||||||||
| 41 | this useful to check that your implementation is suitable. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | if( defined eval { require Role::Tiny } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | Role::Tiny->import; | |||||||||
| 47 | |||||||||||
| 48 | 1 | requires( qw( | |||||||||
| 49 | AWAIT_CLONE AWAIT_NEW_DONE AWAIT_NEW_FAIL | ||||||||||
| 50 | |||||||||||
| 51 | AWAIT_DONE AWAIT_FAIL AWAIT_GET | ||||||||||
| 52 | AWAIT_IS_READY AWAIT_ON_READY | ||||||||||
| 53 | AWAIT_IS_CANCELLED AWAIT_ON_CANCEL | ||||||||||
| 54 | |||||||||||
| 55 | AWAIT_WAIT | ||||||||||
| 56 | ) ); | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | =head1 CONSTRUCTORS | ||||||||||
| 60 | |||||||||||
| 61 | The following methods are expected to create new future instances. They make | ||||||||||
| 62 | use of the class set by the prevailing C<future_class> import argument, if | ||||||||||
| 63 | set, or default to C<Future> if not. | ||||||||||
| 64 | |||||||||||
| 65 | =head2 AWAIT_NEW_DONE | ||||||||||
| 66 | |||||||||||
| 67 | Generate a new immediate future that is successful. The future will already be | ||||||||||
| 68 | ready and have the list of values set as its result. | ||||||||||
| 69 | |||||||||||
| 70 | $f = $CLASS->AWAIT_NEW_DONE( @results ); | ||||||||||
| 71 | |||||||||||
| 72 | # $f->AWAIT_IS_READY will be true | ||||||||||
| 73 | # $f->AWAIT_GET will return @results | ||||||||||
| 74 | |||||||||||
| 75 | =head2 AWAIT_NEW_FAIL | ||||||||||
| 76 | |||||||||||
| 77 | Generate a new immediate future that is failed. The future will already be | ||||||||||
| 78 | ready and invoking the L</AWAIT_GET> method will throw the given exception. | ||||||||||
| 79 | |||||||||||
| 80 | $f = $CLASS->AWAIT_NEW_FAIL( $message ); | ||||||||||
| 81 | |||||||||||
| 82 | # $f->AWAIT_IS_READY will be true | ||||||||||
| 83 | # $f->AWAIT_GET will throw $message | ||||||||||
| 84 | |||||||||||
| 85 | =head1 INSTANCE METHODS | ||||||||||
| 86 | |||||||||||
| 87 | =head2 AWAIT_CLONE | ||||||||||
| 88 | |||||||||||
| 89 | Generate a new pending future of the same type as an existing one, which is | ||||||||||
| 90 | not modified by doing so. It will only be invoked on instances that are | ||||||||||
| 91 | currently pending. | ||||||||||
| 92 | |||||||||||
| 93 | $new_f = $f->AWAIT_CLONE; | ||||||||||
| 94 | |||||||||||
| 95 | If the instance has any fields that are required for successful operation | ||||||||||
| 96 | (such as application-wide context or event system components) these ought to | ||||||||||
| 97 | be copied. The method should not otherwise copy any per-instance state such | ||||||||||
| 98 | as pending callbacks or partial results. | ||||||||||
| 99 | |||||||||||
| 100 | =head2 AWAIT_DONE | ||||||||||
| 101 | |||||||||||
| 102 | Sets the success result of an existing still-pending future. It will only be | ||||||||||
| 103 | invoked on future instances that are currently pending. | ||||||||||
| 104 | |||||||||||
| 105 | $f->AWAIT_DONE( @results ); | ||||||||||
| 106 | |||||||||||
| 107 | # $f->AWAIT_IS_READY will now be true | ||||||||||
| 108 | # $f->AWAIT_GET will now return @results | ||||||||||
| 109 | |||||||||||
| 110 | =head2 AWAIT_FAIL | ||||||||||
| 111 | |||||||||||
| 112 | Sets the failure result of an existing still-pending future. It will only be | ||||||||||
| 113 | invoked on future instances that are currently pending. | ||||||||||
| 114 | |||||||||||
| 115 | $f->AWAIT_FAIL( $message ); | ||||||||||
| 116 | |||||||||||
| 117 | # $f->AWAIT_IS_READY will now be true | ||||||||||
| 118 | # $f->AWAIT_GET will now throw $message | ||||||||||
| 119 | |||||||||||
| 120 | =head2 AWAIT_IS_READY | ||||||||||
| 121 | |||||||||||
| 122 | Returns true if a future is ready (successful, failed or cancelled); false if | ||||||||||
| 123 | still pending. | ||||||||||
| 124 | |||||||||||
| 125 | $bool = $f->AWAIT_IS_READY; | ||||||||||
| 126 | |||||||||||
| 127 | =head2 AWAIT_IS_CANCELLED | ||||||||||
| 128 | |||||||||||
| 129 | Returns true is a future has already been cancelled; false if still pending, | ||||||||||
| 130 | successful or failed. | ||||||||||
| 131 | |||||||||||
| 132 | $bool = $f->AWAIT_IS_CANCELLED; | ||||||||||
| 133 | |||||||||||
| 134 | An implementation that does not support cancellation can simply return a | ||||||||||
| 135 | constant false here: | ||||||||||
| 136 | |||||||||||
| 137 | sub AWAIT_IS_CANCELLED { 0 } | ||||||||||
| 138 | |||||||||||
| 139 | =head2 AWAIT_GET | ||||||||||
| 140 | |||||||||||
| 141 | Yields the result of a successful future (or just the first value if called in | ||||||||||
| 142 | scalar context). Throws the failure message as an exception if called on a a | ||||||||||
| 143 | failed one. Will not be invoked on a pending or cancelled future. | ||||||||||
| 144 | |||||||||||
| 145 | @result = $f->AWAIT_GET; | ||||||||||
| 146 | $result = $f->AWAIT_GET; | ||||||||||
| 147 | $f->AWAIT_GET; | ||||||||||
| 148 | |||||||||||
| 149 | =head2 AWAIT_ON_READY | ||||||||||
| 150 | |||||||||||
| 151 | Attach a new CODE reference to be invoked when the future becomes ready (by | ||||||||||
| 152 | success or failure). The arguments and context that C<$code> is invoked with | ||||||||||
| 153 | are unspecified. | ||||||||||
| 154 | |||||||||||
| 155 | $f->AWAIT_ON_READY( $code ); | ||||||||||
| 156 | |||||||||||
| 157 | =head2 AWAIT_CHAIN_CANCEL | ||||||||||
| 158 | |||||||||||
| 159 | Attach a future instance to be cancelled when another one is cancelled. | ||||||||||
| 160 | |||||||||||
| 161 | $f1->AWAIT_CHAIN_CANCEL( $f2 ); | ||||||||||
| 162 | |||||||||||
| 163 | When C<$f1> is cancelled, then C<$f2> is cancelled. There is no link from | ||||||||||
| 164 | C<$f2> back to C<$f1> - whenever C<$f2> changes state here, nothing special | ||||||||||
| 165 | happens to C<$f1>. | ||||||||||
| 166 | |||||||||||
| 167 | An implementation that does not support cancellation can simply ignore this | ||||||||||
| 168 | method. | ||||||||||
| 169 | |||||||||||
| 170 | sub AWAIT_CHAIN_CANCEL { } | ||||||||||
| 171 | |||||||||||
| 172 | An older version of this API specification named this C<AWAIT_ON_CANCEL>, but | ||||||||||
| 173 | that name will be repurposed for attaching code blocks in a later version. | ||||||||||
| 174 | |||||||||||
| 175 | =head2 AWAIT_ON_CANCEL | ||||||||||
| 176 | |||||||||||
| 177 | Attach a new CODE reference to be invoked when the future is cancelled. | ||||||||||
| 178 | |||||||||||
| 179 | $f->AWAIT_ON_CANCEL( $code ); | ||||||||||
| 180 | |||||||||||
| 181 | An implementation that does not support cancellation can simply ignore this | ||||||||||
| 182 | method. | ||||||||||
| 183 | |||||||||||
| 184 | sub AWAIT_ON_CANCEL { } | ||||||||||
| 185 | |||||||||||
| 186 | =head2 AWAIT_WAIT | ||||||||||
| 187 | |||||||||||
| 188 | Called by the toplevel C<await> expression in order to run the event system | ||||||||||
| 189 | and wait for the instance to be ready. It should return results or throw an | ||||||||||
| 190 | exception in the same manner as L</AWAIT_GET>. | ||||||||||
| 191 | |||||||||||
| 192 | @result = $f->AWAIT_WAIT; | ||||||||||
| 193 | $result = $f->AWAIT_WAIT; | ||||||||||
| 194 | $f->AWAIT_WAIT; | ||||||||||
| 195 | |||||||||||
| 196 | =cut | ||||||||||
| 197 | |||||||||||
| 198 | =head1 AUTHOR | ||||||||||
| 199 | |||||||||||
| 200 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 201 | |||||||||||
| 202 | =cut | ||||||||||
| 203 | |||||||||||
| 204 | 1 | 0x55AA; | |||||||||