# Life cycle

## Introduction

Each ConanJs state object has a a life cycle. This life cycle helps tracking several aspects of the State including:

* [Monitor async operations](/data/conan-state/life-cycle/async-handling.md)
* [Provide with introspection](/data/conan-state/life-cycle/introspection.md)&#x20;

To understand the life cycle we need to get some insight on what is inside a ConanState. It is composed of.

* The Main Thread
* The Monitor Thread (for async operations)
* The Meta Flow (for introspection / error handling)

We also need to introduce the thread and flows

## Threads and Flows

Threads and flows are the building blocks of ConanJs, while you don't need to have a deep understanding of them, it is helpful to have an overview.

### Threads

Threads are streams of data that have the same shape.

When you update the state of the Conan State through an [action](/data/conan-state/actions.md), the main thread is notified and stores the new state, so it can be accessed.

### Flows

Flows have statuses, each status then has, similar to the thread, a stream of data with the same shape.

A flow has a current status, and you can also transition to a different status.

Note that flows are not exactly the same as [Conan Flows](/data/flows.md), but they are very similar.

Both threads and flow can be [started or stopped](broken://pages/-MBmtP5NHJQKJIXkc0rZ)

{% hint style="success" %}
ConanJs auto-starts Conan State for you, so most of the times you don't have to worry about this.

Is a handy feature to have though for advanced use cases
{% endhint %}

## The Main Thread

The main thread is where all the data updates are stored.

Most of the features of the Conan State affect this Thread, it is also not accessible directly to protect it from unintentional changes.

## The Monitor Thread

The monitor thread collects all the information around async operations, and models that information into  [MonitorInfo](/api/conan-state-classes/monitorinfo.md) objects.

Check the [async handling](/data/conan-state/life-cycle/async-handling.md) section for more information on how to use this thread.

## The Meta Flow

The meta flow has the several statuses that are handy to provide with introspection.

It maps all the information into [MetaInfo](/api/conan-state-classes/metainfo.md) objects

Check the [Introspection](/data/conan-state/life-cycle/introspection.md) section for more information.

## Accessing the Monitor Thread and the Meta Flow&#x20;

Each ConanState allows easy access to the information encapsulated in the monitor flow and the meta flow through.... (can you guess it?).... more ConanState!

![Recursion!](/files/-MBsO5MnIfLQgalznpQN)

There are two properties in the ConanState for this

* **asyncState.** Returns a ConanState representation of the [Monitor Info](/api/conan-state-classes/metainfo.md) contained in the Monitor Thread. Since it returns a new ConanState, you can perform all the operations that you would with any other ConanState!
* **metaFlow.** Returns a [ConanFlow](/data/flows.md) which in turn, it can also be decomposed to obtain different ConanState

### Accessing only one state at a time

You could subscribe to the state produced by these properties as you would with any normal Conan State/ Conan Flow, this will work well if you want to consume in isolation the Meta info or the Monitor Info.

{% hint style="success" %}
This could be the case when you have a loading overlay component where you are only interested in showing if some state is currently performing an asyncAction.

You could [subscribe to](/data/conan-state/subscribing-state.md):

myState$.asyncState
{% endhint %}

Many times though you are probably going to be interested in combining the data with either the Meta info and/or the Monitor info.

In that case you have two options.

Do this yourself by composing a new state using the original ConanState and the results from the properties asyncState/metaFlow, and subscribe to this new state.

Leverage the ability to subscribe not only to data but a[ combination of data / monitor info / meta info.](/data/conan-state/subscribing-state.md#monitor-info-and-meta-info)&#x20;

{% content-ref url="/pages/-M9U47JMLJUwzYtw5l00" %}
[Async handling](/data/conan-state/life-cycle/async-handling.md)
{% endcontent-ref %}

{% content-ref url="/pages/-MBmsuicPsAxBh9nAJ\_y" %}
[Introspection](/data/conan-state/life-cycle/introspection.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.conanjs.io/data/conan-state/life-cycle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
