# model/lifecycle

Source: https://zotonic.com/cotonic/models/lifecycle

URI: https://zotonic.com/id/2738

Language: en



<a name="model.lifecycle"></a>
Application lifecycle is a key way that modern operating systems manage resources. On Android, iOS, and recent Windows versions, apps can be started and stopped at any time by the OS. This allows these platforms to streamline and reallocate resources where they best benefit the user.

Modern browsers will also suspend and discard pages. Browsers do not provide this information in an orderly fashion. There are individual events like: <tt>load</tt>, <tt>unload</tt>, and <tt>visibilitychange</tt>. The lifecycle model publishes the different states a page can be in.

<dl>
                <dt><tt>active</tt></dt>
                <dd>
                    The page has focus, and is visible in the foreground. It is important that
                    the page is responsive to user input. It may be needed to move non-important
                    work from the main thread to a worker.
                </dd>

                <dt><tt>passive</tt></dt>
                <dd>
                    The page has lost focus, but is still visible. It is important that ui updates
                    and animations still run smoothly. It may be a good time to store unsaved
                    application state.
                </dd>

                <dt><tt>hidden</tt></dt>
                <dd>
                    The page has lost focus, and is no longer visible, but it still exists. It is
                    likely that this is the end of user interaction with the page. Unsaved state
                    should be saved. It is also a good time to stop making UI updates, since they
                    won't be seen by user.
                </dd>

                <dt><tt>frozen</tt></dt>
                <dd>
                    The page has been cached to memory by the browser. It can resume in a
                    later stage. Javascript's timers and fetch callbacks no longer work.
                    Browsers can freeze pages to prevent full page reloads. This is a good time
                    to close any open Web Socket connections, close WebRTC channels because
                    the browser want's to conserve energy.
                </dd>

                <dt><tt>terminated</tt></dt>
                <dd>
                    The page will be terminated shortly.
                </dd>
            </dl>

The lifecycle model publishes the state the page is at on the <tt>model/lifecycle/event/state</tt> topic as a retained message. This makes it possible to get the current state at any moment, and be notified of changes so the application can scale its resource usage at any moment.

State changes are always done in an orderly way. When a page goes from state <tt>active</tt> to state <tt>hidden</tt> it is made sure that the <tt>passive</tt> state is also published.

State transition diagram:  
  
<img width="250" alt="Lifecycle State Changes" src="https://user-images.githubusercontent.com/1024972/123400323-edbf7600-d5a5-11eb-94fd-0bd42aff2ac2.png" />
<a name="model.lifecycle.event.state"></a>
event/state
-----------

When the lifecycle model sees a state change in the page lifecycle state, its state is published on this topic. The message is published as a retained message, and is available when the model is active. Possible states are: <tt>active</tt>, <tt>passive</tt>, <tt>hidden</tt>, <tt>frozen</tt>, and <tt>terminated</tt>.

```
// Subscribe in a worker
self.subscribe("model/lifecycle/event/state",
    function(m) {
        switch(m.payload) {
            case "active":
            case "passive":
                // Speed up network usage again.
            case "hidden":
                // Slow down network usage.
                break;
        }
   });
```
<a name="model.lifecycle.event.online"></a>
event/online
------------

When the lifecycle model detects a change in the online state of the browser, the state will be published on this topic. When the browser is online, <tt>true</tt> will be published <tt>false</tt> otherwise.

```
self.subscribe("model/lifecycle/event/online",
    function(m) {
        if(m.payload) {
            console.log("We are online");
        } else {
            console.log("We are offline");
        }
    }
);
```
<a name="model.lifecycle.event.ping"></a>
event/ping
----------

When the lifecycle model is enabled it publishes a retained <tt>pong</tt> message on this topic. This makes it possible to check if the model is enabled.

```
// Subscribe in a worker
self.subscribe("model/lifecycle/event/ping",
    function() {
        console.log("the lifecycle model is enabled")
   });
```
<!-- autofocus model -->

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2738?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2738?language=en>)
