Development Documentation (main branch) - For stable release docs, see docs.rs/eidetica

Awareness

Struct Awareness 

pub struct Awareness { /* private fields */ }
Expand description

The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information (cursor, username, status, ..). Each client can update its own local state and listen to state changes of remote clients.

Each client is identified by a unique client id (something we borrow from doc.clientID). A client can override its own state by propagating a message with an increasing timestamp (clock). If such a message is received, it is applied if the known state of that client is older than the new state (clock < new_clock). If a client thinks that a remote client is offline, it may propagate a message with { clock, state: null, client }. If such a message is received, and the known clock of that client equals the received clock, it will clean the state.

Before a client disconnects, it should propagate a null state with an updated clock.

Implementations§

§

impl Awareness

pub fn new(doc: Doc) -> Awareness

Creates a new instance of Awareness struct, which operates over a given document. Awareness instance has full ownership of that document. If necessary it can be accessed using either Awareness::doc or Awareness::doc_mut methods.

pub fn with_clock<C>(doc: Doc, clock: C) -> Awareness
where C: Clock + 'static,

Creates a new instance of Awareness struct, which operates over a given document. Awareness instance has full ownership of that document. If necessary it can be accessed using either Awareness::doc or Awareness::doc_mut methods.

pub fn on_update<F>(&self, f: F) -> Arc<dyn Drop>
where F: Fn(&Awareness, &Event, Option<&Origin>) + 'static,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn on_update_with<K, F>(&self, key: K, f: F)
where K: Into<Origin>, F: Fn(&Awareness, &Event, Option<&Origin>) + 'static,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn unobserve_update<K>(&self, key: K) -> bool
where K: Into<Origin>,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn on_change<F>(&self, f: F) -> Arc<dyn Drop>
where F: Fn(&Awareness, &Event, Option<&Origin>) + 'static,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn on_change_with<K, F>(&self, key: K, f: F)
where K: Into<Origin>, F: Fn(&Awareness, &Event, Option<&Origin>) + 'static,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn unobserve_change<K>(&self, key: K) -> bool
where K: Into<Origin>,

Returns a channel receiver for an incoming awareness events. This channel can be cloned.

pub fn doc(&self) -> &Doc

Returns a read-only reference to an underlying Doc.

pub fn doc_mut(&mut self) -> &mut Doc

Returns a read-write reference to an underlying Doc.

pub fn client_id(&self) -> u64

Returns a globally unique client ID of an underlying Doc.

pub fn iter(&self) -> AwarenessIter<'_>

Returns a state map of all the clients tracked by current Awareness instance. Those states are identified by their corresponding ClientIDs. The associated state is represented and replicated to other clients as a JSON string.

pub fn local_state<S>(&self) -> Option<S>

Returns a JSON string state representation of a current Awareness instance.

pub fn local_state_raw(&self) -> Option<Arc<str>>

Returns a JSON string state representation of a current Awareness instance.

pub fn remove_state(&self, client_id: u64)

Clears out a state of a given client, effectively marking it as disconnected.

pub fn state<D>(&self, client_id: u64) -> Option<D>

Gets the state of a particular client.

pub fn meta(&self, client_id: u64) -> Option<(u32, u64)>

Get the (sequence number, last updated timestamp) metadata associated with particular client.

pub fn clean_local_state(&self)

Clears out a state of a current client (see: Awareness::client_id), effectively marking it as disconnected.

pub fn set_local_state<S>(&self, state: S) -> Result<(), Error>
where S: Serialize,

Sets a current Awareness instance state to a corresponding JSON string. This state will be replicated to other clients as part of the AwarenessUpdate and it will trigger an event to be emitted if current instance was created using [Awareness::with_observer] method.

pub fn set_local_state_raw<S>(&self, json: S)
where S: Into<Arc<str>>,

Sets a current Awareness instance state to a corresponding JSON string. This state will be replicated to other clients as part of the AwarenessUpdate and it will trigger an event to be emitted if current instance was created using [Awareness::with_observer] method.

pub fn update(&self) -> Result<AwarenessUpdate, Error>

Returns a serializable update object which is representation of a current Awareness state. This doesn’t include states that have been removed in the past.

pub fn update_with_clients<I>( &self, clients: I, ) -> Result<AwarenessUpdate, Error>
where I: IntoIterator<Item = u64>,

Returns a serializable update object which is representation of a current Awareness state. Unlike Awareness::update, this method variant allows to prepare update only for a subset of known clients. These clients must all be known to a current Awareness instance, otherwise a Error::ClientNotFound error will be returned.

pub fn apply_update(&self, update: AwarenessUpdate) -> Result<(), Error>

Applies an update (incoming from remote channel or generated using Awareness::update / Awareness::update_with_clients methods) and modifies a state of a current instance.

If current instance has an observer channel (see: [Awareness::with_observer]), applied changes will also be emitted as events.

pub fn apply_update_with<O>( &self, update: AwarenessUpdate, origin: O, ) -> Result<(), Error>
where O: Into<Origin>,

Applies an update (incoming from remote channel or generated using Awareness::update / Awareness::update_with_clients methods) and modifies a state of a current instance.

If current instance has an observer channel (see: [Awareness::with_observer]), applied changes will also be emitted as events.

pub fn apply_update_summary( &self, update: AwarenessUpdate, ) -> Result<Option<AwarenessUpdateSummary>, Error>

Applies an update (incoming from remote channel or generated using Awareness::update / Awareness::update_with_clients methods) and modifies a state of a current instance. Returns an AwarenessUpdateSummary object informing about the changes that were applied.

If current instance has an observer channel (see: [Awareness::with_observer]), applied changes will also be emitted as events.

pub fn apply_update_summary_with<O>( &self, update: AwarenessUpdate, origin: O, ) -> Result<Option<AwarenessUpdateSummary>, Error>
where O: Into<Origin>,

Applies an update (incoming from remote channel or generated using Awareness::update / Awareness::update_with_clients methods) and modifies a state of a current instance. Returns an AwarenessUpdateSummary object informing about the changes that were applied.

If current instance has an observer channel (see: [Awareness::with_observer]), applied changes will also be emitted as events.

Trait Implementations§

§

impl Debug for Awareness

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Awareness

Available on not (target_family=wasm and target_os=unknown).
§

fn default() -> Awareness

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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<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
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<M> Meta for M
where M: Default,