@@ -39,30 +39,45 @@ impl LocallyTrackedSubscription {
3939pub enum SubscriptionChangeRequest {
4040 #[ serde( rename = "subscribe" ) ]
4141 Subscribe ( SubscribeToStream ) ,
42+
43+ /// Explicitly unsubscribes from a stream. This corresponds to the `unsubscribeAll()` API in the
44+ /// SDKs.
45+ ///
46+ /// Unsubscribing a single stream subscription happens internally in the SDK by reducing its
47+ /// refcount. Once no references are remaining, it's no longer listed in
48+ /// [StartSyncStream.active_streams] which will cause it to get unsubscribed after its TTL.
4249 #[ serde( rename = "unsubscribe" ) ]
43- Unsubscribe ( UnsubscribeFromStream ) ,
50+ Unsubscribe ( StreamKey ) ,
4451}
4552
46- # [ serde_as ]
53+ /// A key uniquely identifying a stream.
4754#[ derive( Deserialize ) ]
48- pub struct SubscribeToStream {
49- pub stream : String ,
55+ pub struct StreamKey {
56+ pub name : String ,
5057 #[ serde( default ) ]
5158 pub params : Option < Box < serde_json:: value:: RawValue > > ,
59+ }
60+
61+ impl StreamKey {
62+ pub fn serialized_params ( & self ) -> & str {
63+ match & self . params {
64+ Some ( params) => params. get ( ) ,
65+ None => "null" ,
66+ }
67+ }
68+ }
69+
70+ #[ serde_as]
71+ #[ derive( Deserialize ) ]
72+ pub struct SubscribeToStream {
73+ pub stream : StreamKey ,
5274 #[ serde_as( as = "Option<DurationSeconds>" ) ]
5375 #[ serde( default ) ]
5476 pub ttl : Option < Duration > ,
5577 #[ serde( default ) ]
5678 pub priority : Option < BucketPriority > ,
5779}
5880
59- #[ derive( Deserialize ) ]
60- pub struct UnsubscribeFromStream {
61- pub stream : String ,
62- #[ serde( default ) ]
63- pub params : Option < Box < serde_json:: value:: RawValue > > ,
64- }
65-
6681pub fn apply_subscriptions (
6782 db : * mut sqlite:: sqlite3 ,
6883 subscription : SubscriptionChangeRequest ,
@@ -83,17 +98,14 @@ INSERT INTO ps_stream_subscriptions (stream_name, local_priority, local_params,
8398 )
8499 . into_db_result ( db) ?;
85100
86- stmt. bind_text ( 1 , & subscription. stream , sqlite:: Destructor :: STATIC ) ?;
101+ stmt. bind_text ( 1 , & subscription. stream . name , sqlite:: Destructor :: STATIC ) ?;
87102 match & subscription. priority {
88103 Some ( priority) => stmt. bind_int ( 2 , priority. number ) ,
89104 None => stmt. bind_null ( 2 ) ,
90105 } ?;
91106 stmt. bind_text (
92107 3 ,
93- match & subscription. params {
94- Some ( params) => params. get ( ) ,
95- None => "null" ,
96- } ,
108+ subscription. stream . serialized_params ( ) ,
97109 sqlite:: Destructor :: STATIC ,
98110 ) ?;
99111 stmt. bind_int64 (
@@ -109,13 +121,10 @@ INSERT INTO ps_stream_subscriptions (stream_name, local_priority, local_params,
109121 let stmt = db
110122 . prepare_v2 ( "UPDATE ps_stream_subscriptions SET ttl = NULL WHERE stream_name = ? AND local_params = ?" )
111123 . into_db_result ( db) ?;
112- stmt. bind_text ( 1 , & subscription. stream , sqlite:: Destructor :: STATIC ) ?;
124+ stmt. bind_text ( 1 , & subscription. name , sqlite:: Destructor :: STATIC ) ?;
113125 stmt. bind_text (
114126 2 ,
115- match & subscription. params {
116- Some ( params) => params. get ( ) ,
117- None => "null" ,
118- } ,
127+ subscription. serialized_params ( ) ,
119128 sqlite:: Destructor :: STATIC ,
120129 ) ?;
121130 stmt. exec ( ) ?;
0 commit comments