Chain

Struct Chain 

Source
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: u64

Implementations§

Source§

impl Chain

Source

pub fn new(core: Arc<GunCore>) -> Self

Source

pub fn with_soul( core: Arc<GunCore>, soul: String, parent: Option<Arc<Chain>>, ) -> Self

Source

pub fn with_key(core: Arc<GunCore>, key: String, parent: Arc<Chain>) -> Self

Source

pub fn get(&self, key: &str) -> Arc<Chain>

Get a property or node by key Based on Gun.js chain.get()

Source

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

Source

pub fn on<F>(&self, callback: F) -> Arc<Chain>
where F: Fn(Value, Option<String>) + Send + Sync + Clone + 'static,

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, None if 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();
Source

pub async fn once<F>(&self, callback: F) -> GunResult<Arc<Chain>>
where F: FnOnce(Value, Option<String>),

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::Null if not found)
    • Second parameter: The key if this is a property access, None if node access
§Returns

Returns Ok(Arc<Chain>) for method chaining. The callback is called with:

  • The data if found locally or received from network
  • Value::Null if 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?;
Source

pub fn map<F>(&self, callback: F) -> Arc<Chain>
where F: Fn(Value, String) + Send + Sync + Clone + 'static,

Map over properties of a node Based on Gun.js chain.map() - complete implementation

Source

pub async fn set(&self, item: Value) -> GunResult<Arc<Chain>>

Add item to a set Based on Gun.js chain.set() - proper set implementation

Source

pub fn back(&self, amount: Option<usize>) -> Option<Arc<Chain>>

Go back up the chain Based on Gun.js chain.back()

Source

pub fn off(&self) -> Arc<Chain>

Remove all listeners for this chain Based on Gun.js chain.off() - properly removes listeners

Trait Implementations§

Source§

impl Clone for Chain

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Chain

§

impl !RefUnwindSafe for Chain

§

impl Send for Chain

§

impl Sync for Chain

§

impl Unpin for Chain

§

impl !UnwindSafe for Chain

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more