pub struct State { /* private fields */ }Expand description
State timestamp generator for conflict resolution
Generates monotonically increasing timestamps that combine system time with a sub-millisecond counter. This ensures uniqueness and proper ordering even with high write rates.
State values are used throughout Gun to:
- Resolve conflicts (higher state wins)
- Order updates chronologically
- Track when data was last modified
§Thread Safety
State is thread-safe and can be shared across threads using Arc<State>.
Implementations§
Source§impl State
impl State
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new State generator
Initializes the state system with a counter starting at 0.
Sourcepub fn next(&self) -> f64
pub fn next(&self) -> f64
Generate a new state timestamp Returns a timestamp that increases with each call
§Panics
This function will panic if the system time is before the Unix epoch, which should never happen in practice on modern systems.
Sourcepub fn is(node: &Option<Node>, key: &str) -> Option<f64>
pub fn is(node: &Option<Node>, key: &str) -> Option<f64>
Get the state timestamp for a specific key on a node
Retrieves the state value stored in the node’s metadata for the given key.
Returns None if the key doesn’t exist or has no state recorded.
§Arguments
node- Optional reference to the nodekey- The property key to check
§Returns
The state timestamp for the key, or None if not found.
§Example
use gun::state::{State, Node};
let node = Node::new();
let state_value = State::is(&Some(node), "my_key");Sourcepub fn ify(
node: &mut Node,
key: Option<&str>,
state: Option<f64>,
value: Option<Value>,
soul: Option<&str>,
) -> Node
pub fn ify( node: &mut Node, key: Option<&str>, state: Option<f64>, value: Option<Value>, soul: Option<&str>, ) -> Node
Update a node with state information for a key
This method sets the state timestamp and optionally the soul and data for a node. It’s called whenever data is updated to record when the change occurred.
§Arguments
node- Mutable reference to the node to updatekey- Optional property key (ifNone, only soul is set)state- Optional state timestamp (generated bynext)value- Optional value to store in the node’s datasoul- Optional soul identifier for the node
§Returns
A clone of the updated node (for convenience in chaining).
§Example
use gun::state::{State, Node};
use serde_json::json;
let mut node = Node::with_soul("my_soul".to_string());
let state = State::new();
let timestamp = state.next();
State::ify(
&mut node,
Some("my_key"),
Some(timestamp),
Some(json!("my_value")),
Some("my_soul"),
);