Skip to main content

Zero

Struct Zero 

Source
pub struct Zero<C = ()> { /* private fields */ }
Expand description

A Zero client: syncs the rows of named queries from zero-cache into a local replica, keeps materialized views of them current, and runs mutators optimistically before the server confirms them.

C is what mutators are given besides their arguments, such as the logged-in user’s auth data. Everything runs on the thread that made the client, driven by the Scheduler and the socket events.

Implementations§

Source§

impl<C: 'static> Zero<C>

Source

pub fn new( schema: Arc<Schema>, context: C, mutators: Vec<Mutator<C>>, options: ZeroOptions, scheduler: Rc<dyn Scheduler>, socket_factory: Option<Rc<dyn SocketFactory>>, ) -> Result<Self, Error>

  • schema: the app’s schema.
  • mutators: every mutator the client may run. Pending mutations are replayed by name, so each must be registered.
  • scheduler: the clock and timers, on the app’s event loop; tests pass a ManualScheduler.
  • socket_factory: opens the sync socket. Without one the client runs locally.

When the server no longer has the client’s state, such as after it closed the connection for a message over its size limit, and the app set no on_client_state_not_found, the client replaces itself by a new one on the same store, as JS reloads the page: a new client ID and client group, the views and preloads kept, and pending mutations of the old client group dropped.

Source

pub fn client_id(&self) -> String

The client’s ID, which changes when it replaces itself.

Source

pub fn client_group_id(&self) -> String

The client group’s ID, which changes when the client replaces itself.

Source

pub fn user_id(&self) -> Option<&str>

The user the client syncs for.

Source

pub fn schema(&self) -> &Arc<Schema> ⓘ

The app’s schema.

Source

pub fn context(&self) -> &C

What the mutators are given besides their arguments.

Source

pub fn closed(&self) -> bool

Whether the client is closed.

Source

pub fn connection(&self) -> &Connection

The client’s connection to zero-cache.

Source

pub fn materialize( &self, query: &AnyQuery, ttl: &Ttl, ) -> Result<MaterializedView, Error>

A view of the query’s rows that stays current. Named queries are synced from the server; an unnamed one only sees local data.

Source

pub fn run( &self, query: &AnyQuery, run_type: RunType, ttl: Option<&Ttl>, ) -> Deferred<ViewValue> ⓘ

The query’s rows: what the replica holds now (Unknown), or once the server has confirmed them on this connection (Complete) or any (Cached). Await the result, or read it once settled.

An unnamed query only sees local data: the server never confirms it, so waiting for Complete or Cached never settles.

Source

pub fn preload(&self, query: &AnyQuery, ttl: &Ttl) -> Preload

Keeps the query’s rows synced without a view, until cleanup. Preloading an unnamed query does nothing.

Source

pub fn mutate(&self, name: &str, args: Option<Value>) -> MutatorResult

Runs a mutator on the client now and sends it to the server. A mutator not registered with this client doesn’t run: both outcomes are a ZeroError saying so. JS throws that error instead.

Source

pub fn set_visible(&self, visible: bool)

The app was shown, or hidden. Hidden for ZeroOptions::hidden_disconnect_delay_ms, the client disconnects until it is shown again.

Source

pub fn reconnect_now(&self)

The network may be back: reconnect without waiting for the backoff.

Source

pub fn when_pending_settled(&self, done: impl FnOnce() + 'static)

Calls done once the server has confirmed every pending mutation, or when it can’t.

Source

pub fn close(&self)

Closes the connection. Pending mutations and queries fail, and views stop updating.

Source

pub fn flush_storage(&self)

Waits for the local store’s queued writes, such as before the app exits. A failure to write them is reported to ZeroOptions::on_storage_failure.

Source

pub fn delete(&self) -> DroppedDatabases

Closes the client and deletes its local data (zero.delete()): the replica and pending mutations of this user and storage key, under every schema version. Pending mutations the server hasn’t confirmed are lost. Use it to log a user out.

Trait Implementations§

Source§

impl<C> Drop for Zero<C>

Source§

fn drop(&mut self)

Dropping the client closes it: otherwise its socket would stay open, and its timers run, with no client left to hear them.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<C = ()> !RefUnwindSafe for Zero<C>

§

impl<C = ()> !Send for Zero<C>

§

impl<C = ()> !Sync for Zero<C>

§

impl<C = ()> !UnwindSafe for Zero<C>

§

impl<C> Forget for Zero<C>

§

impl<C> Freeze for Zero<C>
where Rc<C>: Freeze,

§

impl<C> Move for Zero<C>

§

impl<C> Unpin for Zero<C>
where Rc<C>: Unpin,

§

impl<C> UnsafeUnpin for Zero<C>
where Rc<C>: UnsafeUnpin,

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.