The lifetime of state

The state lives as long as its bean. So a bean's scope is its state's lifetime: the application's, a screen's, a widget's.

Why

State that outlives its screen leaks, and state that is disposed by hand is disposed in the wrong place sooner or later. The container already knows when a bean ends: the state ends there.

Example

// The state of one screen: made when the screen opens, ended when it closes.
@Component()
@Scope(NavigationScopes.route)
class Draft {
  @Signal()
  String title = '';
}

NavigationScopes.route is the navigation starter's scope: see Beans per screen or branch.

When the bean is destroyed

The fields themselves keep their values: a field is a plain field, with or without readers. After the application closes, basket.items and basket.total still read; an effect no longer runs for a change.

A prototype

A prototype (@Scope('prototype')) has its own state per instance. The container never destroys a prototype, so its effects run while something holds the instance, and its state goes with the last reference to it.

See also