🗡️
ConanJs
  • What is ConanJs?
  • Why ConanJs...
    • ... if coming from Redux
    • ... if using vanilla React
    • ... if learning React
    • ... if not using React
  • How to install/use ConanJs
  • About us / Github / Contact us
  • Demos
    • Demo Gallery
    • Conan State Demos
      • Hello World
      • Todos
        • Todos - Basic
        • Todos - Async
        • Todos - Optimistic
      • Github issues viewer
    • Conan Flow Demos
      • Authentication
  • CONAN DATA
    • General Concepts
    • Conan State
      • Actions & Reducers
        • Reducers
        • Actions
      • Creating State
      • Observing State
        • Live rendering
        • Connecting
      • Composing State
      • Scaling State
      • Orchestrating State
      • Life cycle
        • Async handling
        • Introspection
      • Testing state
    • Conan Flow
      • Creating Flows
      • Serialising Flows
      • Observing Flows
  • CONAN Runtime
  • Dependency Injection
    • General Concepts
    • Creating the Context
    • Using the Context
  • ASAPs
  • Logging
  • API
    • Main Classes
      • Conan
        • StateDef
      • ConanState
      • ConanFlow
        • UserFlowDef
        • UserStatusDef
        • Status
    • Conan State Classes
      • Thread
      • ConnectedState
      • MonitorInfo
      • MetaInfo
    • Dependency Injection
      • DiContextFactory
    • ASAPS
      • Asaps
      • Asap
Powered by GitBook
On this page
  • Introduction
  • One state to one state
  • Map
  • Filter
  • Two states to one state
  • Merge
  • Tuple
  • Many states to one state
  • Combine
  • Examples
  • Composing State + Dependency Injection
  • Demo

Was this helpful?

  1. CONAN DATA
  2. Conan State

Composing State

PreviousConnectingNextScaling State

Last updated 4 years ago

Was this helpful?

Introduction

We advocate for creating atomic state and to compose new state when you need to.

There are several composition operations that you can perform and that we will see below.

One key aspect of composing state is that it returns another ConanState, which you can compose again.

One state to one state

Map

We can derive one state into a new one using ConanState.map:

productQty$.map((productQty)=>productQty.qty)

productPrice$.map((price)=>price.priceUsd)

Filter

We can filter 1 state into a new one using ConanState.filter:

productQty$.filter((productQty) => productQty.qty % 2 === 0)
    
productQty$.filter((productQty)=>productQty.qty % 2 !== 0)

Two states to one state

Merge

We can merge 2 states into 1 using ConanState.merge:

productQty$.merge(productPrice$, (productQty, productPrice)
 => productQty && productPrice ? productQty.qty * productPrice.priceUsd : 0)

Tuple

productQty$.tuple(productPrice$)

Many states to one state

Combine

We can combine N states into a new one using ConanState.combine:

ConanState.combine(`combine`, {
    price: productPrice$,
    qty: productQty$
})
ConanState.combine<CombinedObject>(
    `combine`,
    {
        price: productPrice$,
        qty: productQty$
    }
)

Examples

You can navigate these examples in this sandbox:

Composing State + Dependency Injection

Our dependency injection lets you describe atomic and composed state such that from the point of view of their consumers, they would not know (and won't care) wether if it is atomic state or not.

Demo

Check out our for more details

dependency Injection
https://codesandbox.io/s/0po76?file=/src/app.tsxcodesandbox.io
Composing Conan states
Inception!