Overview

A declaration to add a computed-property into the state. It helps to add a property that does not need to be stored-property. It’s like Swift’s computed property like following:

struct State {
 var items: [Item] = [] {

 var itemsCount: Int {
   items.count
 }
}

However, this Swift’s computed-property will compute the value every state changed. It might become a serious issue on performance.

Compared with Swift’s computed property and this, this does not compute the value every state changes, It does compute depend on specified rules. That rules mainly come from the concept of Memoization.

Example code:

struct State: ExtendedStateType {

 var name: String = ...
 var items: [Int] = []

 struct Extended: ExtendedType {

   static let instance = Extended()

   let filteredArray = Field.Computed<[Int]> {
     $0.items.filter { $0 > 300 }
   }
   .dropsInput {
     $0.noChanges(\\.items)
   }
 }
}
let store: MyStore<State, Never> = ...

let state = store.state

let result: [Int] = state.computed.filteredArray

Instructions

Computed Property on State

States may have a property that actually does not need to be stored property. In that case, we can use computed property.

Although, we should take care of the cost of the computing to return value in that. What is that case? Followings explains that.

Computed concept is inspired from Vuex Getters. https://vuex.vuejs.org/guide/getters.html

For example, there is itemsCount.