pub struct Chain {
pub core: Arc<GunCore>,
pub soul: Option<String>,
pub key: Option<String>,
pub parent: Option<Arc<Chain>>,
pub id: u64,
/* private fields */
}Expand description
Chain - the main API for interacting with Gun Based on Gun.js chain.js and IGunChain interface This provides the fluent API: gun.get(‘key’).put(data).on(callback)
Fields§
§core: Arc<GunCore>§soul: Option<String>§key: Option<String>§parent: Option<Arc<Chain>>§id: u64Implementations§
Source§impl Chain
impl Chain
pub fn new(core: Arc<GunCore>) -> Self
pub fn with_soul( core: Arc<GunCore>, soul: String, parent: Option<Arc<Chain>>, ) -> Self
pub fn with_key(core: Arc<GunCore>, key: String, parent: Arc<Chain>) -> Self
Sourcepub fn get(&self, key: &str) -> Arc<Chain>
pub fn get(&self, key: &str) -> Arc<Chain>
Get a property or node by key Based on Gun.js chain.get()
Sourcepub async fn put(&self, data: Value) -> GunResult<Arc<Chain>>
pub async fn put(&self, data: Value) -> GunResult<Arc<Chain>>
Put data into the current node/property Based on Gun.js chain.put() - improved implementation
Sourcepub fn on<F>(&self, callback: F) -> Arc<Chain>
pub fn on<F>(&self, callback: F) -> Arc<Chain>
Subscribe to updates on this node/property Based on Gun.js chain.on() - enhanced with change detection and network sync
Subscribes to real-time updates for this node or property. The callback will be called:
- Immediately with current data if available
- Whenever the data changes (with change detection to avoid duplicate updates)
- When data is received from the network
This method includes:
- Change detection: Only triggers callback when data actually changes
- Network synchronization: Triggers sync requests when local data changes
- Real-time propagation: Receives updates from network peers
§Arguments
callback- Closure that receives updates- First parameter: The updated data value
- Second parameter: The key if this is a property access,
Noneif node access
§Returns
Returns Arc<Chain> for method chaining. Use off() to unsubscribe.
§Example
use gun::Gun;
use serde_json::json;
let gun = Gun::new();
// Subscribe to updates
let chain = gun.get("counter");
chain.on(|data, _key| {
if let Some(value) = data.as_i64() {
println!("Counter updated: {}", value);
}
});
// Updates will trigger the callback
chain.put(json!(1)).await?;
chain.put(json!(2)).await?;
// Unsubscribe when done
chain.off();Sourcepub async fn once<F>(&self, callback: F) -> GunResult<Arc<Chain>>
pub async fn once<F>(&self, callback: F) -> GunResult<Arc<Chain>>
Get data once without subscribing Based on Gun.js chain.once() - improved with async waiting and network requests
Retrieves data once without creating a subscription. If data is not found locally, sends a network request via DAM protocol and waits for response with timeout (default 5 seconds).
§Arguments
callback- Closure that receives the data and optional key- First parameter: The data value (or
Value::Nullif not found) - Second parameter: The key if this is a property access,
Noneif node access
- First parameter: The data value (or
§Returns
Returns Ok(Arc<Chain>) for method chaining. The callback is called with:
- The data if found locally or received from network
Value::Nullif data not found and network request times out or fails
§Errors
Returns GunError if there’s an error during the operation
§Example
use gun::Gun;
use serde_json::json;
let gun = Gun::new();
// Put data first
gun.get("user").put(json!({"name": "Alice"})).await?;
// Read once
gun.get("user").once(|data, _key| {
if let Some(obj) = data.as_object() {
println!("Name: {:?}", obj.get("name"));
}
}).await?;Sourcepub fn map<F>(&self, callback: F) -> Arc<Chain>
pub fn map<F>(&self, callback: F) -> Arc<Chain>
Map over properties of a node Based on Gun.js chain.map() - complete implementation
Sourcepub async fn set(&self, item: Value) -> GunResult<Arc<Chain>>
pub async fn set(&self, item: Value) -> GunResult<Arc<Chain>>
Add item to a set Based on Gun.js chain.set() - proper set implementation