Skip to content

Async Tasks demo screen - #8

Draft
simonhamp wants to merge 1 commit into
mainfrom
async-tasks-demo
Draft

Async Tasks demo screen#8
simonhamp wants to merge 1 commit into
mainfrom
async-tasks-demo

Conversation

@simonhamp

Copy link
Copy Markdown
Member

Adds a demo screen for the AsyncTask API, reachable from the launcher at /async-tasks.

What it covers

Five cards, one per part of the API:

  1. finished() — a slow task on a background PHP thread; result lands back on the component, with elapsed ms.
  2. failed() — a task that throws, surfaced as an AsyncTaskException carrying the original message and class.
  3. Concurrency — three tasks dispatched together with staggered sleeps (3s / 1s / 2s). Results land in duration order, not dispatch order, which is the visible proof they ran concurrently rather than queueing.
  4. shared() — result delivered as a named event via #[On] instead of a screen-scoped callback. Navigate away mid-flight and back: a plain finished() would drop the result, this one still arrives.
  5. Static-closure guard — a $this-bound work closure is rejected at dispatch time, in the handler where you can see it, rather than failing silently in a background log.

The tap counter at the top of the screen is the load-bearing bit: hammer it while tasks are running and it keeps counting, so the UI thread is visibly never blocked.

Tests

AsyncTask::fake() runs dispatched work inline and synchronously, so finished()/failed() fire during the test with no background threads. The on-device delays exist to make the async behaviour watchable; an instant() helper zeroes them so the suite doesn't sleep.

8 tests, 62 assertions, all passing.

Depends on

The AsyncTask API, AsyncTask::fake() and Native\Mobile\Testing\Native all land in the component-async-api branch of nativephp/mobile. This will not build against dev-main until that ships — pointing nativephp/mobile at a local checkout of that branch is how it was developed and verified. No composer changes are included here, deliberately.

🤖 Generated with Claude Code

Exercises the AsyncTask API end to end: finished(), failed(), several
tasks in flight at once, shared() delivery via a named event, and the
static-closure guard.

A tap counter sits at the top of the screen as the load-bearing proof —
it keeps counting while tasks run, so the UI thread is visibly never
blocked.

Tests use AsyncTask::fake(), which runs work inline and synchronously.
The on-device delays are zeroed via a helper so the suite doesn't sleep.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant