zero-client 0.2.0

A native Rust client for Zero.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! The connection status state machine
//! (`packages/zero-client/src/client/connection-manager.ts`).

use super::event_loop::EventLoop;
use super::{ClientErrorKind, ConnectionError, Scheduler, Timer};
use crate::json::{Object, Value, number};
use crate::protocol::{ErrorBody, FailureCause};
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};

/// The engine's connection state.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum State {
    Initializing,
    Disconnected(ConnectionError),
    Connecting {
        attempt: u32,
        disconnect_at: f64,
        reason: Option<ConnectionError>,
    },
    Connected,
    NeedsAuth(ConnectionError),
    Error(ConnectionError),
    Closed(ConnectionError),
}

impl State {
    pub(crate) fn status(&self) -> ConnectionStatus {
        match self {
            State::Initializing => ConnectionStatus::Initializing,
            State::Disconnected(_) => ConnectionStatus::Disconnected,
            State::Connecting { .. } => ConnectionStatus::Connecting,
            State::Connected => ConnectionStatus::Connected,
            State::NeedsAuth(_) => ConnectionStatus::NeedsAuth,
            State::Error(_) => ConnectionStatus::Error,
            State::Closed(_) => ConnectionStatus::Closed,
        }
    }

    /// needs-auth and error: the run loop pauses until `connect()`.
    pub(crate) fn is_terminal(&self) -> bool {
        matches!(self, State::NeedsAuth(_) | State::Error(_))
    }
}

type Subscriber = Rc<dyn Fn(&State)>;
type Waiter = (Box<dyn Fn() -> bool>, Box<dyn FnOnce(State)>);

pub(crate) struct ConnectionManager {
    me: Weak<ConnectionManager>,
    state: RefCell<State>,
    disconnect_timeout: f64,
    timeout_check_interval: f64,
    scheduler: Rc<dyn Scheduler>,
    event_loop: Weak<EventLoop>,
    subscribers: RefCell<Vec<(u64, Subscriber)>>,
    next_subscriber_id: Cell<u64>,
    state_waiters: RefCell<Vec<Waiter>>,
    connect_requested: Cell<bool>,
    connect_request_waiters: RefCell<Vec<Box<dyn FnOnce()>>>,
    connecting_started_at: Cell<Option<f64>>,
    timeout_timer: RefCell<Option<Box<dyn Timer>>>,
    last_timeout_check_at: Cell<f64>,
}

impl ConnectionManager {
    pub(crate) fn new(
        disconnect_timeout: f64,
        scheduler: Rc<dyn Scheduler>,
        event_loop: Weak<EventLoop>,
    ) -> Rc<Self> {
        let now = scheduler.now();
        Rc::new_cyclic(|me| ConnectionManager {
            me: me.clone(),
            state: RefCell::new(State::Initializing),
            disconnect_timeout,
            timeout_check_interval: 1000.0,
            scheduler,
            event_loop,
            subscribers: RefCell::default(),
            next_subscriber_id: Cell::new(0),
            state_waiters: RefCell::default(),
            connect_requested: Cell::new(false),
            connect_request_waiters: RefCell::default(),
            connecting_started_at: Cell::new(None),
            timeout_timer: RefCell::new(None),
            last_timeout_check_at: Cell::new(now),
        })
    }

    pub(crate) fn state(&self) -> State {
        self.state.borrow().clone()
    }

    pub(crate) fn status(&self) -> ConnectionStatus {
        self.state.borrow().status()
    }

    pub(crate) fn is(&self, status: ConnectionStatus) -> bool {
        self.status() == status
    }

    pub(crate) fn is_in_terminal_state(&self) -> bool {
        self.state.borrow().is_terminal()
    }

    pub(crate) fn should_continue_run_loop(&self) -> bool {
        !self.is(ConnectionStatus::Closed)
    }

    fn microtask(&self, task: impl FnOnce() + 'static) {
        if let Some(l) = self.event_loop.upgrade() {
            l.microtask(task);
        }
    }

    /// Hears every transition synchronously. Returns the unsubscribe.
    pub(crate) fn subscribe(&self, listener: impl Fn(&State) + 'static) -> Box<dyn Fn()> {
        let id = self.next_subscriber_id.get();
        self.next_subscriber_id.set(id + 1);
        self.subscribers.borrow_mut().push((id, Rc::new(listener)));
        let me = self.me.clone();
        Box::new(move || {
            if let Some(me) = me.upgrade() {
                me.subscribers.borrow_mut().retain(|(i, _)| *i != id);
            }
        })
    }

    /// Calls `waiter` in a microtask after the next transition. A waiter
    /// whose `is_current` turns false would do nothing then, and is dropped
    /// when the next one is added: while connected, the run loop waits again
    /// on every ping without a transition.
    pub(crate) fn on_next_state_change(
        &self,
        is_current: impl Fn() -> bool + 'static,
        waiter: impl FnOnce(State) + 'static,
    ) {
        let mut w = self.state_waiters.borrow_mut();
        w.retain(|(current, _)| current());
        w.push((Box::new(is_current), Box::new(waiter)));
    }

    pub(crate) fn request_connect(&self) {
        if self.connect_requested.replace(true) {
            return;
        }
        let waiters = std::mem::take(&mut *self.connect_request_waiters.borrow_mut());
        for w in waiters {
            self.microtask(w);
        }
    }

    /// Calls `waiter` in a microtask once `connect()` is requested (right
    /// away if it already was).
    pub(crate) fn on_connect_request(&self, waiter: impl FnOnce() + 'static) {
        if self.connect_requested.get() {
            self.microtask(waiter);
        } else {
            self.connect_request_waiters
                .borrow_mut()
                .push(Box::new(waiter));
        }
    }

    /// Consumes a pending connect request and resumes connecting.
    pub(crate) fn resume_from_connect_request(&self) -> bool {
        if !self.is_in_terminal_state() || !self.connect_requested.replace(false) {
            return false;
        }
        self.connecting(None);
        true
    }

    /// From initializing to connecting, once the local store is loaded.
    pub(crate) fn initialized(&self) {
        if !matches!(*self.state.borrow(), State::Initializing) {
            return;
        }
        let now = self.scheduler.now();
        self.connecting_started_at.set(Some(now));
        self.set(State::Connecting {
            attempt: 0,
            disconnect_at: now + self.disconnect_timeout,
            reason: None,
        });
        self.maybe_start_timeout_interval();
        self.publish();
    }

    pub(crate) fn connecting(&self, reason: Option<ConnectionError>) {
        let state = self.state();
        let mut hidden_disconnect = false;
        match &state {
            State::Closed(_) => return,
            State::Disconnected(r) => {
                if r.client_kind() != Some(ClientErrorKind::Hidden) {
                    return;
                }
                hidden_disconnect = true;
            }
            _ => {}
        }
        let now = self.scheduler.now();
        if hidden_disconnect {
            self.connecting_started_at.set(None);
        }
        if let State::Connecting {
            attempt,
            disconnect_at,
            ..
        } = state
        {
            self.set(State::Connecting {
                attempt: attempt + 1,
                disconnect_at,
                reason,
            });
            self.maybe_start_timeout_interval();
            self.publish();
            return;
        }
        let started = self.connecting_started_at.get().unwrap_or(now);
        self.connecting_started_at.set(Some(started));
        self.set(State::Connecting {
            attempt: 1,
            disconnect_at: started + self.disconnect_timeout,
            reason,
        });
        self.maybe_start_timeout_interval();
        self.publish();
    }

    pub(crate) fn connected(&self) {
        if matches!(*self.state.borrow(), State::Closed(_) | State::Connected) {
            return;
        }
        self.connecting_started_at.set(None);
        self.maybe_stop_timeout_interval();
        self.set(State::Connected);
        self.publish();
    }

    pub(crate) fn disconnected(&self, reason: ConnectionError) {
        match *self.state.borrow() {
            State::Closed(_) | State::Disconnected(_) => return,
            State::Connected => self.connecting_started_at.set(None),
            _ => {}
        }
        self.maybe_stop_timeout_interval();
        self.set(State::Disconnected(reason));
        self.publish();
    }

    pub(crate) fn needs_auth(&self, reason: ConnectionError) {
        if matches!(*self.state.borrow(), State::Closed(_) | State::NeedsAuth(_)) {
            return;
        }
        self.connecting_started_at.set(None);
        self.maybe_stop_timeout_interval();
        self.set(State::NeedsAuth(reason));
        self.publish();
    }

    pub(crate) fn error(&self, reason: ConnectionError) {
        if matches!(*self.state.borrow(), State::Closed(_) | State::Error(_)) {
            return;
        }
        self.connecting_started_at.set(None);
        self.maybe_stop_timeout_interval();
        self.set(State::Error(reason));
        self.publish();
    }

    pub(crate) fn closed(&self) {
        if matches!(*self.state.borrow(), State::Closed(_)) {
            return;
        }
        self.connecting_started_at.set(None);
        self.maybe_stop_timeout_interval();
        self.set(State::Closed(ConnectionError::client(
            ClientErrorKind::ClientClosed,
            "Zero was explicitly closed by calling zero.close()",
        )));
        self.publish();
        self.subscribers.borrow_mut().clear();
        self.resolve_state_waiters();
    }

    fn set(&self, state: State) {
        *self.state.borrow_mut() = state;
    }

    fn publish(&self) {
        let state = self.state();
        let subscribers: Vec<Subscriber> = self
            .subscribers
            .borrow()
            .iter()
            .map(|(_, s)| s.clone())
            .collect();
        for s in subscribers {
            s(&state);
        }
        self.resolve_state_waiters();
    }

    fn resolve_state_waiters(&self) {
        let state = self.state();
        let waiters = std::mem::take(&mut *self.state_waiters.borrow_mut());
        for (_, w) in waiters {
            let state = state.clone();
            self.microtask(move || w(state));
        }
    }

    /// Pushes the disconnect deadline forward by however long timers were
    /// not running (a suspended app, a sleeping laptop).
    fn credit_frozen_time(&self) {
        let now = self.scheduler.now();
        let elapsed = now - self.last_timeout_check_at.get();
        self.last_timeout_check_at.set(now);
        if elapsed <= 2.0 * self.timeout_check_interval {
            return;
        }
        if let Some(started) = self.connecting_started_at.get() {
            self.connecting_started_at.set(Some(started + elapsed));
        }
        let mut state = self.state.borrow_mut();
        if let State::Connecting { disconnect_at, .. } = &mut *state {
            *disconnect_at += elapsed;
        }
    }

    fn check_timeout(&self) {
        let State::Connecting { disconnect_at, .. } = self.state() else {
            return;
        };
        if self.scheduler.now() >= disconnect_at {
            self.disconnected(ConnectionError::client(
                ClientErrorKind::Offline,
                format!(
                    "Zero was unable to connect for {} seconds and was disconnected",
                    number::format((self.disconnect_timeout / 1000.0).floor())
                ),
            ));
        }
    }

    fn maybe_start_timeout_interval(&self) {
        if self.timeout_timer.borrow().is_some() {
            return;
        }
        self.last_timeout_check_at.set(self.scheduler.now());
        let me = self.me.clone();
        let timer = self.scheduler.schedule_repeating(
            self.timeout_check_interval,
            Box::new(move || {
                if let Some(me) = me.upgrade() {
                    me.credit_frozen_time();
                    me.check_timeout();
                }
            }),
        );
        *self.timeout_timer.borrow_mut() = Some(timer);
    }

    fn maybe_stop_timeout_interval(&self) {
        if let Some(t) = self.timeout_timer.borrow_mut().take() {
            t.cancel();
        }
    }
}

crate::ast::string_enum!(
    /// The status names (`packages/zero-client/src/client/connection-status-enum.ts`).
    ConnectionStatus {
        Initializing = "initializing",
        Disconnected = "disconnected",
        Connecting = "connecting",
        Connected = "connected",
        NeedsAuth = "needs-auth",
        Error = "error",
        Closed = "closed",
    }
);

/// The connection state an app sees (`ConnectionState` in
/// `packages/zero-client/src/client/connection.ts`).
#[derive(Clone, Debug, PartialEq)]
pub enum ConnectionState {
    /// The client has not tried to connect yet.
    Initializing,
    /// Offline: the client keeps trying to connect, less often.
    Disconnected {
        /// Why the connection ended.
        reason: String,
    },
    /// Trying to connect.
    Connecting {
        /// Why the last attempt failed, if one did.
        reason: Option<String>,
    },
    /// Syncing.
    Connected,
    /// The server or the API server rejected the credentials: call
    /// `Connection::connect` with new ones.
    NeedsAuth(NeedsAuthReason),
    /// The client stopped: the server rejected it or a change failed.
    /// `Connection::connect` tries again.
    Error {
        /// What failed.
        reason: String,
    },
    /// `Zero::close` was called.
    Closed {
        /// Why the client closed.
        reason: String,
    },
}

impl ConnectionState {
    /// The state's kind, without its details.
    pub fn status(&self) -> ConnectionStatus {
        match self {
            ConnectionState::Initializing => ConnectionStatus::Initializing,
            ConnectionState::Disconnected { .. } => ConnectionStatus::Disconnected,
            ConnectionState::Connecting { .. } => ConnectionStatus::Connecting,
            ConnectionState::Connected => ConnectionStatus::Connected,
            ConnectionState::NeedsAuth(_) => ConnectionStatus::NeedsAuth,
            ConnectionState::Error { .. } => ConnectionStatus::Error,
            ConnectionState::Closed { .. } => ConnectionStatus::Closed,
        }
    }

    pub(crate) fn of(state: &State) -> ConnectionState {
        match state {
            State::Initializing => ConnectionState::Initializing,
            State::Closed(r) => ConnectionState::Closed {
                reason: r.message().to_owned(),
            },
            State::Connected => ConnectionState::Connected,
            State::Connecting { reason, .. } => ConnectionState::Connecting {
                reason: reason
                    .as_ref()
                    .map(|r| r.message().to_owned())
                    .filter(|m| !m.is_empty()),
            },
            State::Disconnected(r) => ConnectionState::Disconnected {
                reason: r.message().to_owned(),
            },
            State::Error(r) => ConnectionState::Error {
                reason: r.message().to_owned(),
            },
            State::NeedsAuth(r) => ConnectionState::NeedsAuth(NeedsAuthReason::of(r)),
        }
    }

    /// The JSON form of the JS `ConnectionState`.
    pub fn to_json(&self) -> Value {
        let mut o = Object::new();
        o.insert("name", Value::from(self.status().as_str()));
        match self {
            ConnectionState::Initializing | ConnectionState::Connected => {}
            ConnectionState::Connecting { reason } => {
                if let Some(r) = reason {
                    o.insert("reason", Value::from(r.as_str()));
                }
            }
            ConnectionState::Disconnected { reason }
            | ConnectionState::Error { reason }
            | ConnectionState::Closed { reason } => {
                o.insert("reason", Value::from(reason.as_str()));
            }
            ConnectionState::NeedsAuth(r) => {
                o.insert("reason", r.to_json());
            }
        }
        Value::Object(o)
    }
}

/// Why the client needs new credentials.
#[derive(Clone, Debug, PartialEq)]
pub enum NeedsAuthReason {
    /// The API server answered a push with 401 or 403.
    Mutate {
        /// The HTTP status.
        status: f64,
        /// The response body, if any.
        body: Option<String>,
    },
    /// The API server answered a query transform with 401 or 403.
    Query {
        /// The HTTP status.
        status: f64,
        /// The response body, if any.
        body: Option<String>,
    },
    /// zero-cache rejected the credentials.
    ZeroCache {
        /// zero-cache's message.
        reason: String,
    },
}

impl NeedsAuthReason {
    fn of(error: &ConnectionError) -> NeedsAuthReason {
        if let ConnectionError::Server(body) = error {
            let http = |cause: &FailureCause| match cause {
                FailureCause::Http {
                    status,
                    body_preview,
                } => Some((*status, body_preview.clone().filter(|b| !b.is_empty()))),
                _ => None,
            };
            match &**body {
                ErrorBody::PushFailed { cause, .. } => {
                    if let Some((status, body)) = http(cause) {
                        return NeedsAuthReason::Mutate { status, body };
                    }
                }
                ErrorBody::TransformFailed { cause, .. } => {
                    if let Some((status, body)) = http(cause) {
                        return NeedsAuthReason::Query { status, body };
                    }
                }
                _ => {}
            }
        }
        NeedsAuthReason::ZeroCache {
            reason: error.message().to_owned(),
        }
    }

    /// The JS reason object.
    pub fn to_json(&self) -> Value {
        let mut o = Object::new();
        match self {
            NeedsAuthReason::Mutate { status, body } | NeedsAuthReason::Query { status, body } => {
                let ty = if matches!(self, NeedsAuthReason::Mutate { .. }) {
                    "mutate"
                } else {
                    "query"
                };
                o.insert("type", Value::from(ty));
                o.insert("status", Value::from(*status));
                if let Some(b) = body {
                    o.insert("body", Value::from(b.as_str()));
                }
            }
            NeedsAuthReason::ZeroCache { reason } => {
                o.insert("type", Value::from("zero-cache"));
                o.insert("reason", Value::from(reason.as_str()));
            }
        }
        Value::Object(o)
    }
}