Skip to content

State & Lifecycle

A PAM Desktop component is a long-lived PHP object. The worker process keeps it in memory between events, so its properties are ordinary PHP state: no serialization, no request cycle. Properties marked #[State] are reactive: assigning to one schedules a render, and the framework sends the host only what changed. This page covers how state flows, what each lifecycle hook is for and how to keep background work from running when nobody is looking.

src/Components/Stopwatch.pam.php
<?php
declare(strict_types=1);
namespace App\Components;
use Pam\Desktop\Attributes\State;
use Pam\Desktop\Component;
use Pam\Desktop\System\ComponentTimer;
final class Stopwatch extends Component
{
#[State]
public int $seconds = 0;
#[State]
public bool $running = false;
private ?ComponentTimer $tick = null;
public function toggle(): void
{
$this->running = !$this->running;
if ($this->running) {
$this->tick = $this->timers()->every(1000, fn () => $this->seconds++);
} else {
$this->tick?->cancel();
}
}
}
?>
<template>
<Row class="stopwatch">
<Text class="time">{{ $seconds }} s</Text>
<Button :label="$running ? 'Stop' : 'Start'" on:press="toggle" />
</Row>
</template>
  • Assigning to a #[State] property marks the component dirty. Every handler of a host batch runs first, then the component renders once.
  • Properties without #[State] are plain fields: changing them does not render. Use them for services, caches and handles.
  • State lives in the worker. Module callbacks, timers and event handlers all run in the same process and may change it.

In pam desktop dev, #[State] values survive a hot reload: they are written to a checkpoint and restored into the component of the same class at the same position, right before its mount() runs. Mark secrets and in-flight flags so they start fresh:

#[State(hotReload: false)]
public string $password = '';

Constructor arguments, services and objects are not restored, and a value the new declaration rejects (you changed the type) is dropped.

A template component’s props are its constructor parameters. The owner passes them as attributes:

src/Components/MessageComposer.pam.php
final class MessageComposer extends Component
{
#[State(hotReload: false)]
public string $draft = '';
public function __construct(public string $placeholder)
{
}
public function send(): void
{
$text = trim($this->draft);
if ($text !== '') {
$this->emit('send', $text);
$this->draft = '';
}
}
}
<MessageComposer placeholder="Message #general" on:send="post" />

The template checks props at compile time: an attribute that is neither a prop, nor an attribute of the component’s root element, nor an event is an error. #[Prop(required: true, min: 0, max: 10)] on a constructor parameter adds validation.

Data flows down through props; changes flow up through events. $this->emit('name', $payload) notifies the owner, which listens with on:name. Keep the source of truth in the screen or in a service, and let child components report intent.

Override only the hooks you need:

Hook When it runs
boot() Once, when the component is created.
mount() Once, before the first render. Load data, register listeners, start timers here.
rendering(), rendered() Around every render.
updating($property, $next, $previous), updated($property) Around a change of a #[State] property.
propsChanged(ComponentChanges $changes) The owner passed new props.
shouldUpdate(ComponentChanges $changes) Return false to skip a render.
inactive(), activated() The main window lost or regained focus while visible.
paused(), resumed() The main window was minimized or hidden, and came back.
unmount(), cleanup() The component left the tree. Release what mount() acquired.
failed(Throwable $error, ErrorContext $context) A child failed; return a fallback element to contain the error.
public function mount(): void
{
Storage::get('filters', function (?string $json): void {
$this->filters = $json === null ? [] : json_decode($json, true);
});
}
public function paused(): void
{
$this->socket?->close(); // nothing to show, nothing to receive
}
public function resumed(): void
{
$this->connect();
}

Prefer $this->timers() over the global Timers module inside a component. Its timers stop when the component unmounts, and while the application is in the background they follow a TimerBackgroundPolicy: Suspend (the default), Cancel or Continue.

use Pam\Desktop\System\TimerBackgroundPolicy;
public function mount(): void
{
// Poll every 30 s while visible; paused when minimized, resumed after.
$this->timers()->every(30_000, fn () => $this->refresh());
// A search that waits for typing to settle.
$this->search = $this->timers()->debounce(300, fn (string $query) => $this->find($query));
// Keep a sync running while hidden: opt in explicitly.
$this->timers()->every(60_000, fn () => $this->sync(), TimerBackgroundPolicy::Continue);
}

timeout(), every(), ensure(), debounce() and throttle() are available. A repeating interval is at least 16 ms. Never use a timer to animate: use an animation, which the host runs on its frame clock.

The state of the main window is the state of the application:

AppState When
Active The main window has the focus.
Inactive The main window is visible without the focus.
Background The main window is minimized or hidden.
use Pam\Desktop\App;
use Pam\Desktop\AppState;
$id = App::onStateChange(function (AppState $state): void {
$this->visible = $state !== AppState::Background;
});
App::offStateChange($id);

App::state() reads the current value and App::quit() ends the application.

App::globals() registers application-wide values, visible as $name in every template and as $this->name in every component. Reactive values re-render whoever reads them:

index.php
App::globals(['appName' => 'Invoices', 'currency' => 'USD']);

For shared mutable data, prefer a service object that screens receive and that emits changes through your own events, or keep the data in SQLite and reload it on focus.