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 dc45c394..eb49f5b7 100644 --- a/resources/views/docs/mobile/4/digging-deeper/async-tasks.md +++ b/resources/views/docs/mobile/4/digging-deeper/async-tasks.md @@ -440,6 +440,36 @@ 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 as you know them from +Laravel: + +```php +use Illuminate\Support\Facades\Concurrency; + +[$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. + +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. + +`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, `Concurrency::driver('sync')->run()`, +which runs the closures one after another. You may call `defer()` in a job as well. + ## Task classes For anything you'd rather not write inline, extend `AsyncTask` and put the work in `handle()`: @@ -509,6 +539,14 @@ A [group](#grouping-tasks) runs its tasks inline too, in the order of your array `wait()` returns the results at once. Every task 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()`, 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 + +``` + ## How It Works When you dispatch a task, the work closure is serialized and handed to a background PHP context — a full PHP @@ -529,7 +567,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). diff --git a/resources/views/docs/mobile/4/digging-deeper/queues.md b/resources/views/docs/mobile/4/digging-deeper/queues.md index aa86be7f..2ca7faef 100644 --- a/resources/views/docs/mobile/4/digging-deeper/queues.md +++ b/resources/views/docs/mobile/4/digging-deeper/queues.md @@ -111,6 +111,10 @@ 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 + 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.