Learn how SmartGraph manages state within ReactiveComponents using BehaviorSubjects
BehaviorSubject
. This approach allows for reactive and efficient state handling within each ReactiveComponent
. Let’s explore how this works in detail.
ReactiveComponent
can manage its own internal state using BehaviorSubject
s. Here’s how it works:
create_state
: This method creates a new BehaviorSubject
with an initial value if it doesn’t already exist.get_state
: Retrieves the BehaviorSubject
for a given state key.update_state
: Updates the value of an existing state by emitting a new value to its BehaviorSubject
.CounterComponent
maintains an internal count
state. Each time process
is called, it increments the count and returns the new value.
BehaviorSubject
for state management allows for reactive programming patterns. You can subscribe to state changes and react accordingly:
count
state, so any changes to the count will be printed automatically.
Counter
component maintains its state across multiple executions of the pipeline.
VocabularyBank
, DifficultyManager
, and UserProgressTracker
) maintains its own internal state using create_state
, get_state
, and update_state
.
DifficultyManager
reacts to correct/incorrect answers by updating the difficulty level and streak count.
UserProgressTracker
maintains a running total of words and correct answers across multiple executions.
LanguageLearningApp
coordinates the flow of state information between components in the pipeline.
BehaviorSubject
, provides a powerful and flexible way to handle state within components. This approach allows for reactive, state-driven computations while keeping state encapsulated within individual components.
By leveraging this system, you can build complex, stateful pipelines where each component can maintain and react to its own state changes, leading to more modular and maintainable AI applications.