spacetimedb-sdk 2.2.0

A Rust SDK for clients to interface with SpacetimeDB
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
560
561
562
563
564
565
566
//! Internal mechanisms for managing subscribed queries.
//!
//! This module is internal, and may incompatibly change without warning.

use crate::spacetime_module::AbstractEventContext;
use crate::{
    db_connection::{next_query_set_id, next_request_id, DbContextImpl, PendingMutation},
    spacetime_module::{SpacetimeModule, SubscriptionHandle},
};
use futures_channel::mpsc;
use spacetimedb_client_api_messages::websocket::{self as ws, common::QuerySetId};
use spacetimedb_data_structures::map::HashMap;
use spacetimedb_query_builder::Query;
use std::sync::{Arc, Mutex};

// TODO: Rewrite for subscription manipulation, once we get that.
// Currently race conditions abound, as you may resubscribe before the prev sub was applied,
// clobbering your previous callback.

pub struct SubscriptionManager<M: SpacetimeModule> {
    subscriptions: HashMap<QuerySetId, SubscriptionHandleImpl<M>>,
}

impl<M: SpacetimeModule> Default for SubscriptionManager<M> {
    fn default() -> Self {
        Self {
            subscriptions: HashMap::default(),
        }
    }
}

pub(crate) type OnAppliedCallback<M> =
    Box<dyn FnOnce(&<M as SpacetimeModule>::SubscriptionEventContext) + Send + 'static>;
pub(crate) type OnErrorCallback<M> =
    Box<dyn FnOnce(&<M as SpacetimeModule>::ErrorContext, crate::Error) + Send + 'static>;
pub type OnEndedCallback<M> = Box<dyn FnOnce(&<M as SpacetimeModule>::SubscriptionEventContext) + Send + 'static>;

/// When handling a pending unsubscribe, there are three cases the caller must handle.
pub(crate) enum PendingUnsubscribeResult<M: SpacetimeModule> {
    // The unsubscribe message should be sent to the server.
    SendUnsubscribe(ws::v2::Unsubscribe),
    // The subscription is immediately being cancelled, so the callback should be run.
    RunCallback(OnEndedCallback<M>),
    // No action is required.
    DoNothing,
}

impl<M: SpacetimeModule> SubscriptionManager<M> {
    pub(crate) fn on_disconnect(&mut self, _ctx: &M::ErrorContext) {
        // We need to clear all the subscriptions.
        // TODO: is this correct? We don't remove them from the client cache,
        // we may want to resume them in the future if we impl reconnecting,
        // and users can already register on-disconnect callbacks which will run in this case.

        // NOTE(cloutiertyler)
        // This function previously invoke `on_error` for all subscriptions.
        // However, this is inconsistent behavior given that `on_disconnect` for
        // connections no longer always has an error argument and that the user
        // can add an `on_ended` callback when unsubscribing.
        //
        // We propose instead that `on_ended` be added to the subscription
        // builder so that it can be invoked when the subscription is ended
        // because of a normal disconnect, but without the user calling
        // `unsubscribe_then`. This can be done in a non-breaking way.
        //
        // For now, we will just do nothing when a subscription ends normally.
    }

    /// Register a new subscription. This does not send the subscription to the server.
    /// Rather, it makes the subscription available for the next `apply_subscriptions` call.
    pub(crate) fn register_subscription(&mut self, query_set_id: QuerySetId, handle: SubscriptionHandleImpl<M>) {
        self.subscriptions
            .try_insert(query_set_id, handle.clone())
            .unwrap_or_else(|_| unreachable!("Duplicate subscription id {query_set_id:?}"));
    }

    /// This should be called when we get a subscription applied message from the server.
    pub(crate) fn subscription_applied(&mut self, ctx: &M::SubscriptionEventContext, query_set_id: QuerySetId) {
        let Some(sub) = self.subscriptions.get_mut(&query_set_id) else {
            // TODO: log or double check error handling.
            return;
        };
        if let Some(callback) = sub.on_applied() {
            callback(ctx)
        }
    }

    /// This should be called when we get a subscription applied message from the server.
    pub(crate) fn handle_pending_unsubscribe(&mut self, query_set_id: QuerySetId) -> PendingUnsubscribeResult<M> {
        let Some(sub) = self.subscriptions.get(&query_set_id) else {
            // TODO: log or double check error handling.
            return PendingUnsubscribeResult::DoNothing;
        };
        let mut sub = sub.clone();
        if sub.is_cancelled() {
            // This means that the subscription was cancelled before it was started.
            // We skip sending the subscription start message.
            self.subscriptions.remove(&query_set_id);
            match sub.on_ended() {
                Some(callback) => {
                    return PendingUnsubscribeResult::RunCallback(callback);
                }
                _ => {
                    return PendingUnsubscribeResult::DoNothing;
                }
            }
        }
        if sub.is_ended() {
            // This should only happen if the subscription was ended due to an error.
            // We don't need to send an unsubscribe message in this case.
            self.subscriptions.remove(&query_set_id);
            return PendingUnsubscribeResult::DoNothing;
        }
        PendingUnsubscribeResult::SendUnsubscribe(ws::v2::Unsubscribe {
            query_set_id,
            request_id: next_request_id(),
            flags: ws::v2::UnsubscribeFlags::SendDroppedRows,
        })
    }

    /// This should be called when we get an unsubscribe applied message from the server.
    pub(crate) fn unsubscribe_applied(&mut self, ctx: &M::SubscriptionEventContext, query_set_id: QuerySetId) {
        let Some(mut sub) = self.subscriptions.remove(&query_set_id) else {
            // TODO: double check error handling.
            log::debug!("Unsubscribe applied called for missing query {query_set_id:?}");
            return;
        };
        if let Some(callback) = sub.on_ended() {
            callback(ctx)
        }
    }

    /// This should be called when we get an unsubscribe applied message from the server.
    pub(crate) fn subscription_error(&mut self, ctx: &M::ErrorContext, query_set_id: QuerySetId) {
        let Some(mut sub) = self.subscriptions.remove(&query_set_id) else {
            // TODO: double check error handling.
            log::warn!("Unsubscribe applied called for missing query {query_set_id:?}");
            return;
        };
        if let Some(callback) = sub.on_error() {
            callback(ctx, ctx.event().clone().unwrap());
        }
    }
}

/// Builder-pattern constructor for subscription queries.
pub struct SubscriptionBuilder<M: SpacetimeModule> {
    on_applied: Option<OnAppliedCallback<M>>,
    on_error: Option<OnErrorCallback<M>>,
    conn: DbContextImpl<M>,
}

impl<M: SpacetimeModule> SubscriptionBuilder<M> {
    #[doc(hidden)]
    /// Call `ctx.subscription_builder()` instead.
    pub fn new(imp: &DbContextImpl<M>) -> Self {
        Self {
            on_applied: None,
            on_error: None,
            conn: imp.clone(),
        }
    }

    /// Register a callback to run when the subscription is applied.
    pub fn on_applied(mut self, callback: impl FnOnce(&M::SubscriptionEventContext) + Send + 'static) -> Self {
        self.on_applied = Some(Box::new(callback));
        self
    }

    /// Register a callback to run when the subscription fails.
    ///
    /// Note that this callback may run either when attempting to apply the subscription,
    /// in which case [`Self::on_applied`] will never run,
    /// or later during the subscription's lifetime if the module's interface changes,
    /// in which case [`Self::on_applied`] may have already run.
    pub fn on_error(mut self, callback: impl FnOnce(&M::ErrorContext, crate::Error) + Send + 'static) -> Self {
        self.on_error = Some(Box::new(callback));
        self
    }

    pub fn subscribe<Queries: IntoQueries>(self, query_sql: Queries) -> M::SubscriptionHandle {
        let query_set_id = next_query_set_id();
        let handle = SubscriptionHandleImpl::new(SubscriptionState::new(
            query_set_id,
            query_sql.into_queries(),
            self.conn.pending_mutations_send.clone(),
            self.on_applied,
            self.on_error,
        ));
        self.conn
            .pending_mutations_send
            .unbounded_send(PendingMutation::Subscribe {
                query_set_id,
                handle: handle.clone(),
            })
            .unwrap();
        M::SubscriptionHandle::new(handle)
    }

    /// Subscribe to all rows from all tables.
    ///
    /// This method is intended as a convenience
    /// for applications where client-side memory use and network bandwidth are not concerns.
    /// Applications where these resources are a constraint
    /// should register more precise queries via [`Self::subscribe`]
    /// in order to replicate only the subset of data which the client needs to function.
    ///
    /// If your client bindings were generated with the `--include-private` flag to `spacetime generate`,
    /// this method will attempt to subscribe to private tables defined by the module.
    /// Such subscriptions will lead to an error unless the client is authenticated as a privileged [`crate::Identity`].
    pub fn subscribe_to_all_tables(self) -> M::SubscriptionHandle {
        let all_subs = M::ALL_TABLE_NAMES
            .iter()
            .map(|table_name| format!("SELECT * FROM {table_name}"))
            .collect::<Vec<_>>();
        log::info!("Subscribing to queries: {all_subs:#?}");
        self.subscribe(all_subs)
    }

    pub fn add_query<T, Q: Query<T>>(self, build: impl Fn(M::QueryBuilder) -> Q) -> TypedSubscriptionBuilder<M> {
        let query = build(M::QueryBuilder::default());
        TypedSubscriptionBuilder {
            builder: self,
            queries: vec![query.into_sql()],
        }
    }
}

// Wrapper around `SubscriptionBuilder` that tracks typed queries
pub struct TypedSubscriptionBuilder<M: SpacetimeModule> {
    builder: SubscriptionBuilder<M>,
    queries: Vec<String>,
}

impl<M: SpacetimeModule> TypedSubscriptionBuilder<M> {
    /// Build a query and invoke `subscribe` in order to subscribe to its results.
    pub fn add_query<T, Q: Query<T>>(mut self, build: impl Fn(M::QueryBuilder) -> Q) -> Self {
        let query = build(M::QueryBuilder::default());
        self.queries.push(query.into_sql());
        self
    }

    /// Subscribe to the queries that have been built with `add_query`.
    pub fn subscribe(self) -> M::SubscriptionHandle {
        self.builder.subscribe(self.queries)
    }
}

/// Types which can be converted into a single query.
//
// This trait is necessary because of Rust's coherence rules.
// If you find and replace it with `Into<Box<str>>`,
// the compiler will complain on the `impl IntoQueries for [T; N]` impl
// that future updates may add `impl Into<Box<str>> for [T; N]`.
pub trait IntoQueryString {
    fn into_query_string(self) -> Box<str>;
}

macro_rules! impl_into_query_string_via_into {
    ($ty:ty $(, $tys:ty)* $(,)?) => {
        impl IntoQueryString for $ty {
            fn into_query_string(self) -> Box<str> {
                self.into()
            }
        }
        $(impl_into_query_string_via_into!($tys);)*
    };
}

impl_into_query_string_via_into! {
    &str, String, Box<str>,
}

/// Types which specify a list of query strings.
pub trait IntoQueries {
    fn into_queries(self) -> Box<[Box<str>]>;
}

impl<T: IntoQueryString> IntoQueries for T {
    fn into_queries(self) -> Box<[Box<str>]> {
        Box::new([self.into_query_string()])
    }
}

impl<T: IntoQueryString, const N: usize> IntoQueries for [T; N] {
    fn into_queries(self) -> Box<[Box<str>]> {
        self.into_iter().map(IntoQueryString::into_query_string).collect()
    }
}

impl<T: IntoQueryString + Clone> IntoQueries for &[T] {
    fn into_queries(self) -> Box<[Box<str>]> {
        self.iter().cloned().map(IntoQueryString::into_query_string).collect()
    }
}

impl<T: IntoQueryString> IntoQueries for Vec<T> {
    fn into_queries(self) -> Box<[Box<str>]> {
        self.into_iter().map(IntoQueryString::into_query_string).collect()
    }
}

impl IntoQueries for Box<[Box<str>]> {
    fn into_queries(self) -> Box<[Box<str>]> {
        self
    }
}

/// This tracks what messages have been exchanged with the server.
#[derive(Debug, PartialEq, Eq, Clone)]
enum SubscriptionServerState {
    Pending, // This hasn't been sent to the server yet.
    Sent,    // We have sent it to the server.
    Applied, // The server has acknowledged it, and we are receiving updates.
    Ended,   // It has been unapplied.
    Error,   // There was an error that ended the subscription.
}

/// We track the state of a subscription here.
/// A reference to this is held by the `SubscriptionHandle` that clients use to unsubscribe,
/// and by the `SubscriptionManager` that handles updates from the server.
pub(crate) struct SubscriptionState<M: SpacetimeModule> {
    query_set_id: QuerySetId,
    query_sql: Box<[Box<str>]>,
    unsubscribe_called: bool,
    status: SubscriptionServerState,
    on_applied: Option<OnAppliedCallback<M>>,
    on_error: Option<OnErrorCallback<M>>,
    on_ended: Option<OnEndedCallback<M>>,
    // This is needed to schedule client operations.
    // Note that we shouldn't have a full connection here.
    pending_mutation_sender: mpsc::UnboundedSender<PendingMutation<M>>,
}

impl<M: SpacetimeModule> SubscriptionState<M> {
    pub(crate) fn new(
        query_set_id: QuerySetId,
        query_sql: Box<[Box<str>]>,
        pending_mutation_sender: mpsc::UnboundedSender<PendingMutation<M>>,
        on_applied: Option<OnAppliedCallback<M>>,
        on_error: Option<OnErrorCallback<M>>,
    ) -> Self {
        Self {
            query_set_id,
            query_sql,
            unsubscribe_called: false,
            status: SubscriptionServerState::Pending,
            on_applied,
            on_error,
            on_ended: None,
            pending_mutation_sender,
        }
    }

    /// Start the subscription.
    /// This updates the state in the handle, and returns the message to be sent to the server.
    /// The caller is responsible for sending the message to the server.
    pub(crate) fn start(&mut self) -> Option<ws::v2::Subscribe> {
        if self.unsubscribe_called {
            // This means that the subscription was cancelled before it was started.
            // We skip sending the subscription start message.
            return None;
        }
        if self.status != SubscriptionServerState::Pending {
            // This should never happen.
            // We should only start a subscription once.
            // If we are starting it again, we have a bug.
            unreachable!("Subscription already started");
        }
        self.status = SubscriptionServerState::Sent;
        Some(ws::v2::Subscribe {
            query_set_id: self.query_set_id,
            query_strings: self.query_sql.clone(),
            request_id: next_request_id(),
        })
    }

    pub fn unsubscribe_then(&mut self, on_end: Option<OnEndedCallback<M>>) -> crate::Result<()> {
        if self.is_ended() {
            return Err(crate::Error::AlreadyEnded);
        }
        // Check if it has already been called.
        if self.unsubscribe_called {
            return Err(crate::Error::AlreadyUnsubscribed);
        }

        self.unsubscribe_called = true;
        self.on_ended = on_end;
        // self.on_ended = Some(Box::new(on_end));

        // We send this even if the status is still Pending, so we can remove it from the manager.
        self.pending_mutation_sender
            .unbounded_send(PendingMutation::Unsubscribe {
                query_set_id: self.query_set_id,
            })
            .unwrap();
        Ok(())
    }

    /// Check if the client ended the subscription before we sent anything to the server.
    pub fn is_cancelled(&self) -> bool {
        self.status == SubscriptionServerState::Pending && self.unsubscribe_called
    }

    pub fn is_ended(&self) -> bool {
        matches!(
            self.status,
            SubscriptionServerState::Ended | SubscriptionServerState::Error
        )
    }

    pub fn is_active(&self) -> bool {
        match self.status {
            SubscriptionServerState::Applied => !self.unsubscribe_called,
            _ => false,
        }
    }

    pub fn on_applied(&mut self) -> Option<OnAppliedCallback<M>> {
        if self.status != SubscriptionServerState::Sent {
            // Potentially log a warning. This might make sense if we are shutting down.
            log::debug!(
                "on_applied called for query {:?} with status: {:?}",
                self.query_set_id,
                self.status
            );
            return None;
        }
        log::debug!("on_applied called for query {:?}", self.query_set_id);
        self.status = SubscriptionServerState::Applied;
        self.on_applied.take()
    }

    pub fn on_ended(&mut self) -> Option<OnAppliedCallback<M>> {
        // TODO: Consider logging a warning if the state is wrong (like being in the Error state).
        if self.is_ended() {
            return None;
        }
        self.status = SubscriptionServerState::Ended;
        self.on_ended.take()
    }

    pub fn on_error(&mut self) -> Option<OnErrorCallback<M>> {
        // TODO: Consider logging a warning if the state is wrong.
        if self.is_ended() {
            return None;
        }
        self.status = SubscriptionServerState::Error;
        self.on_error.take()
    }
}

#[doc(hidden)]
/// Internal implementation held by the module-specific generated `SubscriptionHandle` type.
pub struct SubscriptionHandleImpl<M: SpacetimeModule> {
    pub(crate) inner: Arc<Mutex<SubscriptionState<M>>>,
}

impl<M: SpacetimeModule> Clone for SubscriptionHandleImpl<M> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<M: SpacetimeModule> SubscriptionHandleImpl<M> {
    pub(crate) fn new(inner: SubscriptionState<M>) -> Self {
        Self {
            inner: Arc::new(Mutex::new(inner)),
        }
    }

    pub(crate) fn start(&self) -> Option<ws::v2::Subscribe> {
        let mut inner = self.inner.lock().unwrap();
        inner.start()
    }

    pub(crate) fn is_cancelled(&self) -> bool {
        self.inner.lock().unwrap().is_cancelled()
    }
    pub fn is_ended(&self) -> bool {
        self.inner.lock().unwrap().is_ended()
    }

    pub fn is_active(&self) -> bool {
        self.inner.lock().unwrap().is_active()
    }

    /// Called by the `SubscriptionHandle` method of the same name.
    pub fn unsubscribe_then(self, on_end: Option<OnEndedCallback<M>>) -> crate::Result<()> {
        let mut inner = self.inner.lock().unwrap();
        inner.unsubscribe_then(on_end)
    }

    /// Record that the subscription has been applied and return the callback to run.
    /// The caller is responsible for calling the callback.
    pub(crate) fn on_applied(&mut self) -> Option<OnAppliedCallback<M>> {
        let mut inner = self.inner.lock().unwrap();
        inner.on_applied()
    }

    /// Record that the subscription has been applied and return the callback to run.
    /// The caller is responsible for calling the callback.
    pub(crate) fn on_ended(&mut self) -> Option<OnEndedCallback<M>> {
        let mut inner = self.inner.lock().unwrap();
        inner.on_ended()
    }

    /// Record that the subscription has errored and return the callback to run.
    /// The caller is responsible for calling the callback.
    pub(crate) fn on_error(&mut self) -> Option<OnErrorCallback<M>> {
        let mut inner = self.inner.lock().unwrap();
        inner.on_error()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[allow(unused)]
    // Here to check that these statements compile.
    fn into_queries_box_str(query: Box<str>) {
        let _ = query.clone().into_query_string();
        let _ = <Box<str> as IntoQueryString>::into_query_string(query.clone());
        let _ = query.clone().into_queries();
        let _ = <[Box<str>; 1] as IntoQueries>::into_queries([query.clone()]);
        let _ = [query.clone()].into_queries();
        let slice: &[Box<str>] = std::slice::from_ref(&query);
        let _ = <&[Box<str>] as IntoQueries>::into_queries(slice);
        let _ = slice.into_queries();
        let _ = <Vec<Box<str>> as IntoQueries>::into_queries(vec![query.clone()]);
        let _ = vec![query.clone()].into_queries();
    }

    #[allow(unused)]
    // Here to check that these statements compile.
    fn into_queries_string(query: String) {
        let _ = query.clone().into_query_string();
        let _ = <String as IntoQueryString>::into_query_string(query.clone());
        let _ = query.clone().into_queries();
        let _ = <[String; 1] as IntoQueries>::into_queries([query.clone()]);
        let _ = [query.clone()].into_queries();
        let slice: &[String] = std::slice::from_ref(&query);
        let _ = <&[String] as IntoQueries>::into_queries(slice);
        let _ = slice.into_queries();
        let _ = <Vec<String> as IntoQueries>::into_queries(vec![query.clone()]);
        let _ = vec![query.clone()].into_queries();
    }

    #[allow(unused)]
    // Here to check that these statements compile.
    fn into_queries_str(query: &str) {
        let _ = query.into_query_string();
        let _ = <&str as IntoQueryString>::into_query_string(query);
        let _ = query.into_queries();
        let _ = <[&str; 1] as IntoQueries>::into_queries([query]);
        let _ = [query].into_queries();
        let slice: &[&str] = &[query];
        let _ = <&[&str] as IntoQueries>::into_queries(slice);
        let _ = slice.into_queries();
        let _ = <Vec<&str> as IntoQueries>::into_queries(vec![query]);
        let _ = vec![query].into_queries();
    }
}