rs-store
A thread-safe Redux-style state management library implemented in Rust.
Overview
rs-store provides a predictable state container inspired by Redux, featuring thread-safe state management with support for reducers, subscribers, and async actions through Thunk. Unlike traditional Redux, rs-store's reducers can produce side effects, providing more flexibility in state management.
Features
- ๐ Thread-safe state management
- ๐ข Publisher/Subscriber pattern for state changes
- ๐ Support for asynchronous operations via Thunk actions
- ๐ Side effect handling in reducers
- ๐ Middleware handles actions and effects
- ๐ Backpressure handling with configurable policies
- ๐ฏ Bounded channel size with sync channels
- ๐ Decoupling state updates from notification delivery
- ๐ Channeled subscription support
- ๐ Metrics support
- ๐งช Comprehensive test coverage
Installation
Add this to your Cargo.toml:
[]
= "3.0"
Quick Start
use ;
use Arc;
Feature Details
Backpressure feature
Backpressure is a feature that allows you to control the rate of state updates. and it also can be used to prevent slow subscribers from blocking state updates.
Backpressure Policies
rs-store supports multiple backpressure policies:
- BlockOnFull: Blocks the sender when the queue is full (default)
- DropOldestIf(None): Drops the oldest item when the queue is full (replaces deprecated
DropOldest) - DropLatestIf(None): Drops the newest item when the queue is full (replaces deprecated
DropLatest) - DropOldestIf(Some(predicate)): Drops items from the oldest based on a custom predicate when the queue is full
- DropLatestIf(Some(predicate)): Drops items from the latest based on a custom predicate when the queue is full
Note:
DropOldestandDropLatestare deprecated. UseDropOldestIf(None)andDropLatestIf(None)instead for unconditional dropping, or provide a predicate for conditional dropping.
Predicate-based Backpressure
The DropLatestIf and DropOldestIf policies allow you to implement intelligent message dropping based on custom criteria.
Example: Unconditional dropping
use ;
// Drop the oldest item unconditionally when queue is full
let policy = DropOldestIf;
let store = new
.with_capacity
.with_policy
.build
.unwrap;
Example: Conditional dropping with predicate
use ;
// Drop low-priority messages based on a predicate
let predicate = Boxnew;
let policy = DropLatestIf;
let store = new
.with_capacity // Small capacity to trigger backpressure
.with_policy
.build
.unwrap;
This allows you to prioritize important messages and drop less critical ones when the system is under load.
Side Effects in Reducers
Unlike traditional Redux implementations, rs-store allows reducers to produce side effects directly. This means reducers can produce asynchronous operations.
Middleware
Middleware is a powerful feature that allows you to intercept and modify actions before they reach the reducer, or to handle side effects, logging, metrics, etc.
Channeled Subscription
The channeled subscription feature provides a way to subscribe to a store in new context with a channel.
Latest State Notification for New Subscribers
When a new subscriber is added to the store, it automatically receives the current state through the on_subscribe
method.
This ensures that new subscribers don't miss the current state and can start with the latest information.
use ;
use ;
;
// Usage
let store = new_with_reducer
.build
.unwrap;
// Dispatch some actions to change the state
store.dispatch.unwrap;
store.dispatch.unwrap;
// Add a new subscriber - it will receive the current state (counter: 2)
let subscriber = new;
store.add_subscriber;
This feature ensures that new subscribers are immediately synchronized with the current state of the store.
Metrics
The metrics feature provides a way to collect metrics.
Documentation
For detailed documentation, visit:
Implementation Status
In Progress ๐ง
- Latest state notification for new subscribers
- Notification scheduler (CurrentThread, ThreadPool)
- [-]
Stop store after all effects are scheduled(removed) - drop store after all references are dropped
- dispatcher has weak reference to the store
- effects system
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.