@@ -144,7 +144,7 @@ public static function get_drive_client() {
144144 *
145145 * @return void
146146 */
147- public static function preamble () {
147+ public static function initialize_batch () {
148148 if ( ! is_null ( self ::$ current_batch ) ) {
149149 return ;
150150 }
@@ -163,7 +163,7 @@ public static function preamble() {
163163 *
164164 * @return PromiseInterface A promise that will be resolved in `$callback`.
165165 *
166- * @throws Internal_Exception The method was called without the preamble .
166+ * @throws Internal_Exception The method was called without an initialized batch .
167167 */
168168 public static function async_request ( $ request , $ transform , $ rejection_handler = null ) {
169169 if ( null === self ::$ current_batch ) {
@@ -205,7 +205,7 @@ public static function async_paginated_request(
205205 /**
206206 * Gets one page.
207207 *
208- * @throws Internal_Exception The method was called without the preamble .
208+ * @throws Internal_Exception The method was called without an initialized batch .
209209 *
210210 * phpcs:disable SlevomatCodingStandard.PHP.DisallowReference.DisallowedInheritingVariableByReference
211211 */
@@ -261,21 +261,36 @@ public static function async_paginated_request(
261261 }
262262
263263 /**
264- * Executes all requests and resolves all promises.
264+ * Executes all queued requests and resolves all promises repeatedly until there is nothing to be done .
265265 *
266266 * @param array<int|string, PromiseInterface> $promises The promises to resolve and throw exceptions if they reject.
267267 *
268268 * @return array<int|string, mixed> A list of results from the promises. Is in the same format as the parameter `$promises`, i.e. if an associative array of promises is passed, an associative array of results will be returned.
269269 */
270270 public static function execute ( $ promises = array () ) {
271- if ( is_null ( self ::$ current_batch ) ) {
272- Utils::queue ()->run ();
271+ self ::execute_current_batch ();
272+ Utils::queue ()->run ();
273+
274+ if ( count ( self ::$ pending_requests ) > 0 ) {
275+ self ::execute ();
276+ }
277+
278+ return Utils::all ( $ promises )->wait ();
279+ }
273280
274- return Utils::all ( $ promises )->wait ();
281+ /**
282+ * Executes the current batch request and calls its callbacks
283+ *
284+ * @return void
285+ */
286+ private static function execute_current_batch () {
287+ $ batch = self ::$ current_batch ;
288+
289+ if ( is_null ( $ batch ) ) {
290+ return ;
275291 }
276292
277- $ batch = self ::$ current_batch ;
278- self ::$ current_batch = self ::get_drive_client ()->createBatch ();
293+ self ::$ current_batch = null ;
279294 /**
280295 * The closure executes the batch and throws the exception if it is a rate limit exceeded exception (this is needed by the task runner).
281296 *
@@ -287,14 +302,14 @@ public static function execute( $promises = array() ) {
287302 ),
288303 'Batch Drive call ' ,
289304 static function () use ( $ batch ) {
290- // @phan-suppress-next-line PhanPossiblyNonClassMethodCall
291305 $ ret = $ batch ->execute ();
292306
293307 foreach ( $ ret as $ response ) {
294308 $ exception = self ::wrap_response_exception ( $ response );
295309
296310 if (
297- $ response instanceof Google_Service_Exception && $ exception instanceof API_Rate_Limit_Exception
311+ $ response instanceof Google_Service_Exception &&
312+ $ exception instanceof API_Rate_Limit_Exception
298313 ) {
299314 throw $ response ;
300315 }
@@ -304,22 +319,12 @@ static function () use ( $batch ) {
304319 }
305320 );
306321 $ responses = $ task ->run ();
322+ self ::get_drive_client ()->getClient ()->setUseBatch ( false );
307323
308324 foreach ( $ responses as $ key => $ response ) {
309325 call_user_func ( self ::$ pending_requests [ $ key ], $ response );
310326 unset( self ::$ pending_requests [ $ key ] );
311327 }
312-
313- Utils::queue ()->run ();
314-
315- if ( count ( self ::$ pending_requests ) > 0 ) {
316- self ::execute ();
317- }
318-
319- self ::$ current_batch = null ;
320- self ::get_drive_client ()->getClient ()->setUseBatch ( false );
321-
322- return Utils::all ( $ promises )->wait ();
323328 }
324329
325330 /**
0 commit comments