11<?php
22namespace ScriptFUSION \Porter \Connector ;
33
4+ use Amp \Coroutine ;
45use Amp \Promise ;
56use ScriptFUSION \Porter \Connector \FetchExceptionHandler \FetchExceptionHandler ;
67use ScriptFUSION \Porter \Connector \FetchExceptionHandler \StatelessFetchExceptionHandler ;
@@ -46,8 +47,8 @@ public function mustCache(): bool
4647 }
4748
4849 /**
49- * Retries the specified callback a predefined number of times with the specified resource fetch exception handler,
50- * if set, and then the predefined fetch exception handler.
50+ * Retries the specified callback up to a predefined number of times. If it throws a recoverable exception, the
51+ * resource fetch exception handler is invoked, if set, and then the predefined fetch exception handler.
5152 *
5253 * @param callable $callback Callback.
5354 *
@@ -63,16 +64,21 @@ public function retry(callable $callback)
6364 }
6465
6566 /**
66- * Retries the specified callback asynchronously a predefined number of times with the specified resource fetch
67- * exception handler, if set, and then the predefined fetch exception handler.
67+ * Closes over the specified async generator function with a static factory method that invokes it as a coroutine
68+ * and retries it up to the predefined number of fetch attempts. If it throws a recoverable exception, the resource
69+ * fetch exception handler is invoked, if set, and then the predefined fetch exception handler.
6870 *
69- * @param callable $callback
71+ * @param \Closure $asyncGenerator Async generator function.
72+ *
73+ * @return Promise The result returned by the async function.
7074 */
71- public function retryAsync (callable $ callback ): Promise
75+ public function retryAsync (\ Closure $ asyncGenerator ): Promise
7276 {
7377 return \ScriptFUSION \Retry \retryAsync (
7478 $ this ->maxFetchAttempts ,
75- $ callback ,
79+ static function () use ($ asyncGenerator ): Coroutine {
80+ return new Coroutine ($ asyncGenerator ());
81+ },
7682 $ this ->createExceptionHandler ()
7783 );
7884 }
@@ -81,7 +87,7 @@ private function createExceptionHandler(): \Closure
8187 {
8288 $ userHandlerCloned = $ providerHandlerCloned = false ;
8389
84- return function (\Exception $ exception ) use (&$ userHandlerCloned , &$ providerHandlerCloned ) {
90+ return function (\Exception $ exception ) use (&$ userHandlerCloned , &$ providerHandlerCloned ): void {
8591 // Throw exception instead of retrying, if unrecoverable.
8692 if (!$ exception instanceof RecoverableConnectorException) {
8793 throw $ exception ;
0 commit comments