From e8dd80a62a3547e3e657089bee15e7b609243a30 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 00:38:55 +0100 Subject: [PATCH 1/8] Document Laravel's Concurrency facade on async tasks --- .../mobile/4/digging-deeper/async-tasks.md | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index e70d84d3..4d4d090a 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -575,6 +575,137 @@ handler without an error screen and without a report. +## Laravel's Concurrency facade + +Laravel's [`Concurrency` facade](https://laravel.com/docs/concurrency) runs several closures at the same time and gives +you their results. On a server, Laravel starts a PHP process for each closure. A phone can't do that, so inside your app +each closure runs as an async task instead, and the code you know from Laravel works in a screen as it is: + +```php +use App\Models\Order; +use App\Models\User; +use Illuminate\Support\Facades\Concurrency; + +public function refresh(): void +{ + [$this->users, $this->unpaid] = Concurrency::run([ + fn () => User::count(), + fn () => Order::where('paid', false)->count(), + ]); +} +``` + +There is nothing to configure. On a device and in a [Jump](../the-basics/jump) session, this is the default driver of +the facade, whatever your `concurrency` configuration says, and the `process` and `fork` drivers give you the same one. +When you deploy your Laravel app as a website, it has Laravel's own drivers there. You need Laravel 11.24 or newer. + +As in a [group](#grouping-tasks), you should write each closure on a line of its own. + +### Concurrency results + +The results come back under the keys of your array, with their PHP types. So a closure may return a model, a collection, +an enum, a date or anything else that can be serialized. You may type-hint the services that a closure needs: + +```php +use App\Services\Ledger; + +$totals = Concurrency::run([ + 'month' => fn (Ledger $ledger) => $ledger->totalOfMonth(), + 'year' => fn (Ledger $ledger) => $ledger->totalOfYear(), +]); +``` + +When a closure throws an exception, it is reported where the closure runs, and `run()` throws it again once every +closure is done. You may catch it by its own class. If several closures throw, you get the first one in the order of +your array: + +```php +use App\Exceptions\OutOfStock; + +$sku = $this->sku; + +try { + [$price, $delivery] = Concurrency::run([ + fn () => Shop::priceOf($sku), + fn () => Shop::deliveryOf($sku), + ]); +} catch (OutOfStock $e) { + $this->error = "{$e->sku} is out of stock."; +} +``` + +An exception is built again from its message, or from the public properties that are named like the arguments of its +constructor. If that can't be done, `run()` throws an `AsyncTaskException`, and its `originalClass()` gives you the name +of the class. + +### The screen waits + +The `run()` method holds your handler until every closure is done, as [`wait()`](#waiting-for-the-results) does for a +group. Your screen doesn't render in the meantime, and taps and other events are handled once your handler returns. + +You may only call `run()` in a native screen. In a job, in the work of an async task and in a web view, it throws a +`RuntimeException` and starts nothing. There you may use Laravel's `sync` driver, which runs the closures one after +another: + +```php +$totals = Concurrency::driver('sync')->run($closures); +``` + +### Timeouts for closures + +Each closure may take 60 seconds. You may pass another number of seconds, or a `CarbonInterval`, as the second argument. +A `0` means no deadline: + +```php +$totals = Concurrency::run($closures, 10); +``` + +When a closure passes its deadline, `run()` throws an `AsyncTaskException`. The closure itself can't be stopped, so it +runs to its end. + +### Using `$this` in concurrent closures + +Your closures follow the same rules as [the work of a task](#the-work-closure). A closure that uses `$this` runs on a +copy of your screen, so you should return the value and assign it once `run()` has returned: + +```php +// The closure runs on a copy of the screen, so your screen never sees this report. +Concurrency::run(fn () => $this->report = $this->buildReport()); + +// Return the report instead. +[$this->report] = Concurrency::run(fn () => $this->buildReport()); +``` + +When a closure takes along what [can't be sent](#what-cant-be-sent), `run()` throws an `InvalidArgumentException` before +any closure starts. + +### Deferring work + +If you don't need the results, you may use the `defer()` method. It starts each closure as a task of its own and returns +right away: + +```php +Concurrency::defer([ + fn () => Metrics::report('users'), + fn () => Metrics::report('orders'), +]); +``` + +The closures have no deadline, and an exception that one of them throws is reported. You may call `defer()` in a job as +well. If a task can't be started, `defer()` throws a `RuntimeException`. + +### Choosing between `all()` and `run()` + +| | `AsyncTask::all()` | `Concurrency::run()` | +| --- | --- | --- | +| Your handler | Returns at once, unless you call `wait()` | Waits for the results | +| Your screen | Stays responsive, unless you call `wait()` | Waits for the results | +| Results | Scalars and arrays, as JSON | PHP types, such as models and collections | +| A closure that throws | `failed()` gets the first failure, and the tasks not started yet never run | Thrown in its own class, once every closure is done | + +Use `Concurrency::run()` for short work of which your handler needs the results, and for code that your app shares with +a website. For work that takes longer than a moment, use `AsyncTask::all()` with `finished()`. + ## Task classes For anything you'd rather not write inline, extend `AsyncTask` and put the work in `handle()`: From cd552a84a8291b5177c13cc5e4fc0065f4e9630e Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 00:38:55 +0100 Subject: [PATCH 2/8] Document how to test a screen that calls Concurrency::run() --- .../mobile/4/digging-deeper/async-tasks.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index 4d4d090a..93b925e0 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -798,6 +798,30 @@ it('counts the users and the unpaid orders', function () { Under the fake, every task of a group runs, also the ones after a task that failed. Without the fake no task runs in a test, so `wait()` fails there with a message that tells you to call `AsyncTask::fake()`. +In a test, the [`Concurrency` facade](#laravels-concurrency-facade) uses the driver of your configuration. To test a +screen that calls `Concurrency::run()`, you may choose the `async-task` driver in your `phpunit.xml` file: + +```xml + +``` + +Then call `AsyncTask::fake()` in your test. The closures run one after another, and each is recorded as a dispatch: + +```php +it('counts the users and the unpaid orders', function () { + $fake = AsyncTask::fake(); + + User::factory()->count(3)->create(); + + Native::test(Dashboard::class) + ->call('refresh') + ->assertSet('users', 3) + ->assertSet('unpaid', 0); + + $fake->assertDispatchedTimes(2); +}); +``` + ## How It Works When you dispatch a task, the work closure is serialized and handed to a background PHP context — a full PHP From 90ccf69721d56c3c408dfe58228fcb9d7b51e6fe Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 00:38:55 +0100 Subject: [PATCH 3/8] Name Concurrency::run() in the async task reminders --- resources/views/docs/mobile/4/digging-deeper/async-tasks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index 93b925e0..dc4c824e 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -842,7 +842,7 @@ the callbacks run on your UI thread, in your component. - An assignment to `$this` in the work changes the copy. Return the value, and assign it in `finished()`. - Results round-trip as JSON — return scalars and arrays; pass large data by file path or cache key. - Tasks run once, with no automatic retry. -- `wait()` holds your screen until the whole group is done, so keep it for short work. +- `wait()` and `Concurrency::run()` hold your screen until everything is done, so keep them for short work. - Scoped callbacks are dropped if the user navigates away from the screen that started the work; use `shared()` when the result matters regardless of what screen the user moves to. - Async tasks don't survive the app being killed. For durable background work, use [Queues](queues). From 0b3cf2b199537370802be93413b0787cb3dfd262 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 00:38:55 +0100 Subject: [PATCH 4/8] Say what the background connection and Concurrency do in a job --- resources/views/docs/mobile/4/digging-deeper/queues.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/resources/views/docs/mobile/4/digging-deeper/queues.md b/resources/views/docs/mobile/4/digging-deeper/queues.md index aa86be7f..29a3815c 100644 --- a/resources/views/docs/mobile/4/digging-deeper/queues.md +++ b/resources/views/docs/mobile/4/digging-deeper/queues.md @@ -112,5 +112,9 @@ NativePHP manages the worker lifecycle natively on both platforms. - The queue worker requires ZTS (Thread-Safe) PHP, which NativePHP includes by default. - Only the `database` queue connection is supported. This uses the same SQLite database as your app. +- A job on Laravel's `background` connection runs as an [async task](async-tasks#laravels-concurrency-facade) instead of + in the worker. It is tried once, and it is lost when the app closes before it ran. +- `Concurrency::run()` can't be called in a job. Use `Concurrency::driver('sync')->run()` there, which runs the closures + one after another. - Jobs are persisted to the database, so they survive app restarts. - If a job fails, Laravel's standard retry and failure handling applies. From 8e164165ca67d5d38187abeca534c9105c089ce1 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 01:45:09 +0100 Subject: [PATCH 5/8] Point to Laravel's Concurrency docs and say which connection the worker runs --- resources/views/docs/mobile/4/digging-deeper/async-tasks.md | 3 +++ resources/views/docs/mobile/4/digging-deeper/queues.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index 8dbee7eb..897a29f0 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -601,6 +601,9 @@ There is nothing to configure. On a device and in a [Jump](../the-basics/jump) s the facade, whatever your `concurrency` configuration says, and the `process` and `fork` drivers give you the same one. When you deploy your Laravel app as a website, it has Laravel's own drivers there. You need Laravel 11.24 or newer. +This section covers what is different inside your app. For the facade itself, read Laravel's +[Concurrency documentation](https://laravel.com/docs/concurrency). + As in a [group](#grouping-tasks), you should write each closure on a line of its own. ### Concurrency results diff --git a/resources/views/docs/mobile/4/digging-deeper/queues.md b/resources/views/docs/mobile/4/digging-deeper/queues.md index 29a3815c..2ca7faef 100644 --- a/resources/views/docs/mobile/4/digging-deeper/queues.md +++ b/resources/views/docs/mobile/4/digging-deeper/queues.md @@ -111,7 +111,7 @@ NativePHP manages the worker lifecycle natively on both platforms. ## Things to Note - The queue worker requires ZTS (Thread-Safe) PHP, which NativePHP includes by default. -- Only the `database` queue connection is supported. This uses the same SQLite database as your app. +- The worker runs jobs on the `database` connection, which uses the same SQLite database as your app. - A job on Laravel's `background` connection runs as an [async task](async-tasks#laravels-concurrency-facade) instead of in the worker. It is tried once, and it is lost when the app closes before it ran. - `Concurrency::run()` can't be called in a job. Use `Concurrency::driver('sync')->run()` there, which runs the closures From e71ec3ca300b521cbba09db9c6989d683b3e0416 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 10:54:06 +0100 Subject: [PATCH 6/8] Cut the Concurrency section to what differs inside an app --- .../mobile/4/digging-deeper/async-tasks.md | 130 ++---------------- 1 file changed, 13 insertions(+), 117 deletions(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index 8078b4ce..ee301acb 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -452,137 +452,33 @@ rethrow it there to let your handler end quietly. ## Laravel's Concurrency facade -Laravel's [`Concurrency` facade](https://laravel.com/docs/concurrency) runs several closures at the same time and gives -you their results. On a server, Laravel starts a PHP process for each closure. A phone can't do that, so inside your app -each closure runs as an async task instead, and the code you know from Laravel works in a screen as it is: +Inside your app, each closure that you pass to Laravel's [`Concurrency` facade](https://laravel.com/docs/concurrency) +runs as an async task. So `Concurrency::run()` and `Concurrency::defer()` work in a screen, and the code you know from +Laravel works as it is: ```php -use App\Models\Order; -use App\Models\User; use Illuminate\Support\Facades\Concurrency; -public function refresh(): void -{ - [$this->users, $this->unpaid] = Concurrency::run([ - fn () => User::count(), - fn () => Order::where('paid', false)->count(), - ]); -} +[$this->users, $this->unpaid] = Concurrency::run([ + fn () => User::count(), + fn () => Order::where('paid', false)->count(), +]); ``` There is nothing to configure. On a device and in a [Jump](../the-basics/jump) session, this is the default driver of the facade, whatever your `concurrency` configuration says, and the `process` and `fork` drivers give you the same one. When you deploy your Laravel app as a website, it has Laravel's own drivers there. You need Laravel 11.24 or newer. -This section covers what is different inside your app. For the facade itself, read Laravel's -[Concurrency documentation](https://laravel.com/docs/concurrency). - -As in a [group](#grouping-tasks), you should write each closure on a line of its own. - -### Concurrency results - -The results come back under the keys of your array, with their PHP types. So a closure may return a model, a collection, -an enum, a date or anything else that can be serialized. You may type-hint the services that a closure needs: - -```php -use App\Services\Ledger; - -$totals = Concurrency::run([ - 'month' => fn (Ledger $ledger) => $ledger->totalOfMonth(), - 'year' => fn (Ledger $ledger) => $ledger->totalOfYear(), -]); -``` - -When a closure throws an exception, it is reported where the closure runs, and `run()` throws it again once every -closure is done. You may catch it by its own class. If several closures throw, you get the first one in the order of -your array: - -```php -use App\Exceptions\OutOfStock; - -$sku = $this->sku; - -try { - [$price, $delivery] = Concurrency::run([ - fn () => Shop::priceOf($sku), - fn () => Shop::deliveryOf($sku), - ]); -} catch (OutOfStock $e) { - $this->error = "{$e->sku} is out of stock."; -} -``` - -An exception is built again from its message, or from the public properties that are named like the arguments of its -constructor. If that can't be done, `run()` throws an `AsyncTaskException`, and its `originalClass()` gives you the name -of the class. +The closures follow the rules of [the work closure](#the-work-closure), and as in a group, each should be written on a +line of its own. -### The screen waits - -The `run()` method holds your handler until every closure is done, as [`wait()`](#waiting-for-the-results) does for a -group. Your screen doesn't render in the meantime, and taps and other events are handled once your handler returns. +`run()` holds your handler and your screen until every closure is done, as `wait()` does for a group, and its results +keep their PHP types, such as models and collections. It suits short work, and code that your app shares with a +website. For anything longer, use `AsyncTask::all()` with `finished()`. You may only call `run()` in a native screen. In a job, in the work of an async task and in a web view, it throws a `RuntimeException` and starts nothing. There you may use Laravel's `sync` driver, which runs the closures one after -another: - -```php -$totals = Concurrency::driver('sync')->run($closures); -``` - -### Timeouts for closures - -Each closure may take 60 seconds. You may pass another number of seconds, or a `CarbonInterval`, as the second argument. -A `0` means no deadline: - -```php -$totals = Concurrency::run($closures, 10); -``` - -When a closure passes its deadline, `run()` throws an `AsyncTaskException`. The closure itself can't be stopped, so it -runs to its end. - -### Using `$this` in concurrent closures - -Your closures follow the same rules as [the work of a task](#the-work-closure). A closure that uses `$this` runs on a -copy of your screen, so you should return the value and assign it once `run()` has returned: - -```php -// The closure runs on a copy of the screen, so your screen never sees this report. -Concurrency::run(fn () => $this->report = $this->buildReport()); - -// Return the report instead. -[$this->report] = Concurrency::run(fn () => $this->buildReport()); -``` - -When a closure takes along what [can't be sent](#what-cant-be-sent), `run()` throws an `InvalidArgumentException` before -any closure starts. - -### Deferring work - -If you don't need the results, you may use the `defer()` method. It starts each closure as a task of its own and returns -right away: - -```php -Concurrency::defer([ - fn () => Metrics::report('users'), - fn () => Metrics::report('orders'), -]); -``` - -The closures have no deadline, and an exception that one of them throws is reported. You may call `defer()` in a job as -well. If a task can't be started, `defer()` throws a `RuntimeException`. - -### Choosing between `all()` and `run()` - -| | `AsyncTask::all()` | `Concurrency::run()` | -| --- | --- | --- | -| Your handler | Returns at once, unless you call `wait()` | Waits for the results | -| Your screen | Stays responsive, unless you call `wait()` | Waits for the results | -| Results | Scalars and arrays, as JSON | PHP types, such as models and collections | -| A closure that throws | `failed()` gets the first failure, and the tasks not started yet never run | Thrown in its own class, once every closure is done | - -Use `Concurrency::run()` for short work of which your handler needs the results, and for code that your app shares with -a website. For work that takes longer than a moment, use `AsyncTask::all()` with `finished()`. +another, with `Concurrency::driver('sync')->run()`. You may call `defer()` in a job as well. ## Task classes From 6d56093cbd034658390cafdf893b4b2549ba7e5d Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 10:54:13 +0100 Subject: [PATCH 7/8] Say how to test Concurrency::run() without a second test example --- .../mobile/4/digging-deeper/async-tasks.md | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index ee301acb..3a56892c 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -550,29 +550,13 @@ A [group](#grouping-tasks) runs its tasks inline too, in the order of your array runs in a test, so `wait()` fails there with a message that tells you to call `AsyncTask::fake()`. In a test, the [`Concurrency` facade](#laravels-concurrency-facade) uses the driver of your configuration. To test a -screen that calls `Concurrency::run()`, you may choose the `async-task` driver in your `phpunit.xml` file: +screen that calls `Concurrency::run()`, choose the `async-task` driver in your `phpunit.xml` file and call +`AsyncTask::fake()` in your test. The closures then run one after another, and each is recorded as a dispatch: ```xml ``` -Then call `AsyncTask::fake()` in your test. The closures run one after another, and each is recorded as a dispatch: - -```php -it('counts the users and the unpaid orders', function () { - $fake = AsyncTask::fake(); - - User::factory()->count(3)->create(); - - Native::test(Dashboard::class) - ->call('refresh') - ->assertSet('users', 3) - ->assertSet('unpaid', 0); - - $fake->assertDispatchedTimes(2); -}); -``` - ## How It Works When you dispatch a task, the work closure is serialized and handed to a background PHP context — a full PHP From c06711949c6321788ffaef2732392a7bb5f6eb09 Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Wed, 2 Dec 2026 10:54:42 +0100 Subject: [PATCH 8/8] Smooth two sentences on the Concurrency facade --- .../views/docs/mobile/4/digging-deeper/async-tasks.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md index 3a56892c..5e814798 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -453,8 +453,8 @@ rethrow it there to let your handler end quietly. ## Laravel's Concurrency facade Inside your app, each closure that you pass to Laravel's [`Concurrency` facade](https://laravel.com/docs/concurrency) -runs as an async task. So `Concurrency::run()` and `Concurrency::defer()` work in a screen, and the code you know from -Laravel works as it is: +runs as an async task, so `Concurrency::run()` and `Concurrency::defer()` work in a screen as you know them from +Laravel: ```php use Illuminate\Support\Facades\Concurrency; @@ -477,8 +477,8 @@ keep their PHP types, such as models and collections. It suits short work, and c website. For anything longer, use `AsyncTask::all()` with `finished()`. You may only call `run()` in a native screen. In a job, in the work of an async task and in a web view, it throws a -`RuntimeException` and starts nothing. There you may use Laravel's `sync` driver, which runs the closures one after -another, with `Concurrency::driver('sync')->run()`. You may call `defer()` in a job as well. +`RuntimeException` and starts nothing. There you may use Laravel's `sync` driver, `Concurrency::driver('sync')->run()`, +which runs the closures one after another. You may call `defer()` in a job as well. ## Task classes