Core
Genshi’s API is designed to be minimal and flexible. The core package provides a lot of features, but the primary focus for consumers is expected to be limited to the Store
class. This document only covers the APIs of that class.
To create a new store, you can use the Store
class directly:
The Store
constructor also provides an optional second argument for configuration the name of the store. This is particularly useful for debugging purposes.
Properties
id
Type:
string
Readonly
- Unique identifier for the store
- Generated when the store is created
name
Type:
string
Readonly
- Name of the store
- Only assignable during store creation
tag
Type:
string
Readonly
- The tag is a special identifier that is used to identify the store while debugging
- The tag is assigned automatically during store creation
- If a
name
is provided, the tag will be the name otherwise it will be theid
Methods
action
- The
action
method is used to create a new action - Action functions are pure functions that take the current state and return a new state
effect
- The
effect
method is used to create a new effect - Effect functions are impure functions that can perform side effects
dispatch
- The
dispatch
method is used to dispatch an action or effect - The method takes the action or effect function as the first argument and an optional payload as the second argument
subscribe
- The
subscribe
method is used to listen to changes in the store - It does not provide a way to subscribe to specific events, but only has one callback that is called whenever the store changes
- Returns a listener with a
remove
method to unsubscribe
getState
- Get the current state of the store
- The state is a snapshot of the store’s data at the time of the call and is changing it will not affect the store
getPreviousStates
- Get a list of previous states
- The list is ordered from latest to oldest
- The states returned are all sealed to prevent mutation
history
- Returns the history of all the actions and effects that have been performed by the store
- The list is ordered from latest to oldest
printHistory
- Prints the history to the console in a table
- Useful for debugging and understanding the sequence of actions and effects