# Class: StateStore<T>
A StateStore
is a container of reactive application state properties.
Application state properties define key application state and are the core of Flo.w RDF reactivity. Changes to application state properties trigger reactive updates of UI elements and other components that depend on them.
Application state properties are identified by property paths. The root of a property path is the application
StateStore
object. Paths can have arbitrary depth allowing the developer to organize and group application
state properties.
In JavaScript, property paths are specified as strings using dotted notation. For example:
'prop1'
'subGroup.prop2'
To take advantage of type-safety in Typescript, property paths should be specified as arrays of strings. For example:
['prop1']
['subGroup', 'prop2']
Using this syntax allows IDEs to provide code-completion and ensures type errors are detected during development.
# State Initialization Methods
# State accessor Methods
- get - Get a state property value.
- set - Set a state property value.
- merge - Set multiple state property values.
- increment - Increment a state property value.
- decrement - Decrement a state property value.
- toggle - Toggle a state property value.
# Computed State Methods
- compute - Register a computed state property.
- asyncCompute - Create an asynchronous computed state property.
# Flo.w Dataset Query Methods
- registerQuery - Register a Flo.w reactive dataset query.
# Serialization Methods
- serialize - Configure state serialization with the browser URL.
# Other Reactive State Methods
- autorun - Run a function in a reactive context when application state changes.
- toStream - Create an observable stream of application state changes.
# Type parameters
Name | Default | Description |
---|---|---|
T | any | The type of the application state object. This enables code-completion and type-safety when using Flo.w RDF with Typescript. |
# Hierarchy
- StateStore
# Constructors
# constructor
+ new StateStore(context
: FlowCoreContext<T>): StateStore
Constructs a new StateStore
instance.
# Parameters:
Name | Type | Description |
---|---|---|
context | FlowCoreContext<T> | The parent Flo.w context. |
Returns: StateStore
# Properties
# context
• Readonly
context: FlowCoreContext<T>
The parent Flo.w context.
# Methods
# asyncCompute
▸ asyncCompute<TResult>(computeFn
: AsyncComputeFn<T, TResult>, initialValue?
: TResult | null, delay
: number, name?
: string): PromisedComputedValue<TResult>
Create an asynchronous compute function.
# Type parameters:
Name |
---|
TResult |
# Parameters:
Name | Type | Default value | Description |
---|---|---|---|
computeFn | AsyncComputeFn<T, TResult> | - | Asyncronous compute function |
initialValue | TResult | null | null | Initial result value. Specifies the computed value before the asynchronous operation has completed. |
delay | number | - | Debounce delay (milliseconds). |
name? | string | - | Optional name displayed in debugging messages. |
Returns: PromisedComputedValue<TResult>
# autorun
▸ autorun(autorunFn
: (state: DeepReadonly<T>) => void, name?
: string): IDisposer
Defines a function that runs in a reactive context when state properties that are referenced in the function change.
# Parameters:
Name | Type | Description |
---|---|---|
autorunFn | (state: DeepReadonly<T>) => void | Function to execute when referenced state properties change. |
name? | string | Optional name displayed in debugging messages. |
Returns: IDisposer
# compute
▸ compute<P>(path
: P, computeFn
: ComputeFn<T, PathValue<T, P>>, options?
: ReactiveOptions): void
Register a computed application state property.
The computed property will be recalculated when state properties referenced in computeFn
change.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state path. |
computeFn | ComputeFn<T, PathValue<T, P>> | Function to compute value from other state properties. |
options? | ReactiveOptions | Reactive options. |
Returns: void
# decrement
▸ decrement<P>(path
: P): number
Decrement the specified numeric application state property.
The state property will be coerced to a number if it is not already.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state property path. |
Returns: number
# get
▸ get<P>(path
: P): PathValue<T, P>
Get the current value of an application state property.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state property path. |
Returns: PathValue<T, P>
# increment
▸ increment<P>(path
: P): number
Increment the specified numeric state property.
The state property will be coerced to a number if it is not already.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state property path. |
Returns: number
# initialize
▸ initialize(state
: T): void
Initializes the StateStore
with the supplied initial state.
# Parameters:
Name | Type | Description |
---|---|---|
state | T | State object. |
Returns: void
# merge
▸ merge(patch
: DeepPartial<T>): void
Merge the supplied partial state into the StateStore
.
Use this method to update multiple state properties in one transaction.
# Parameters:
Name | Type | Description |
---|---|---|
patch | DeepPartial<T> | Partial state object. |
Returns: void
# registerQuery
▸ registerQuery<P>(path
: P, queryFn
: FlowQueryDefinitionFn<T>, resultTransformFn?
: FlowQueryResultTransformFn<ExtractQueryResultType<PathValue<T, P>>>): FlowQuery<ExtractQueryResultType<PathValue<T, P>>, T>
Registers a Flo.w reactive query that executes when application state properties referenced in the supplied query definition function are changed.
See FlowQuery for more details.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state path. |
queryFn | FlowQueryDefinitionFn<T> | Query definition function. |
resultTransformFn? | FlowQueryResultTransformFn<ExtractQueryResultType<PathValue<T, P>>> | Results transorm function. |
Returns: FlowQuery<ExtractQueryResultType<PathValue<T, P>>, T>
# serialize
▸ serialize(serializeFn
: SerializeFn<T>, deserializeFn
: DeserializeFn<T>): IReactionDisposer
Configure serialization/deserialization of application state properties to/from browser URL query parameters.
This allows deep-linking and sharing of web application URLs that specify application state.
# Parameters:
Name | Type | Description |
---|---|---|
serializeFn | SerializeFn<T> | Function to serialize application state properties to browser URL query parameters. |
deserializeFn | DeserializeFn<T> | Function to deserialize application state properties from browser URL query parameters. |
Returns: IReactionDisposer
# set
▸ set<P>(path
: P, val
: PathValue<T, P>): void
Set the value of an application state property.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | Application state property path. |
val | PathValue<T, P> | New value to set. |
Returns: void
# toStream
▸ toStream<P>(path
: P, fireImmediately?
: boolean): IObservableStream<PathValue<T, P>>
Expose an application state property as an observable stream of values.
The observable stream will emit a value whenever the application state property changes.
Subscribe to the returned stream to receive values. Call the unsubscribe
method of the subscription
when the subscription is no longer required.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Default value | Description |
---|---|---|---|
path | P | - | Application state property path |
fireImmediately | boolean | true | Set to true to emit a value immediately. Otherwise wait for first change. |
Returns: IObservableStream<PathValue<T, P>>
# toggle
▸ toggle<P>(path
: P): boolean
Toggle the specified boolean state property.
The state property will be coerced to a boolean if it is not already.
# Type parameters:
Name | Type |
---|---|
P | Path<T> |
# Parameters:
Name | Type | Description |
---|---|---|
path | P | The state property path. |
Returns: boolean