@@ -39,30 +39,45 @@ impl LocallyTrackedSubscription {
39
39
pub enum SubscriptionChangeRequest {
40
40
#[ serde( rename = "subscribe" ) ]
41
41
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.
42
49
#[ serde( rename = "unsubscribe" ) ]
43
- Unsubscribe ( UnsubscribeFromStream ) ,
50
+ Unsubscribe ( StreamKey ) ,
44
51
}
45
52
46
- # [ serde_as ]
53
+ /// A key uniquely identifying a stream.
47
54
#[ derive( Deserialize ) ]
48
- pub struct SubscribeToStream {
49
- pub stream : String ,
55
+ pub struct StreamKey {
56
+ pub name : String ,
50
57
#[ serde( default ) ]
51
58
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 ,
52
74
#[ serde_as( as = "Option<DurationSeconds>" ) ]
53
75
#[ serde( default ) ]
54
76
pub ttl : Option < Duration > ,
55
77
#[ serde( default ) ]
56
78
pub priority : Option < BucketPriority > ,
57
79
}
58
80
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
-
66
81
pub fn apply_subscriptions (
67
82
db : * mut sqlite:: sqlite3 ,
68
83
subscription : SubscriptionChangeRequest ,
@@ -83,17 +98,14 @@ INSERT INTO ps_stream_subscriptions (stream_name, local_priority, local_params,
83
98
)
84
99
. into_db_result ( db) ?;
85
100
86
- stmt. bind_text ( 1 , & subscription. stream , sqlite:: Destructor :: STATIC ) ?;
101
+ stmt. bind_text ( 1 , & subscription. stream . name , sqlite:: Destructor :: STATIC ) ?;
87
102
match & subscription. priority {
88
103
Some ( priority) => stmt. bind_int ( 2 , priority. number ) ,
89
104
None => stmt. bind_null ( 2 ) ,
90
105
} ?;
91
106
stmt. bind_text (
92
107
3 ,
93
- match & subscription. params {
94
- Some ( params) => params. get ( ) ,
95
- None => "null" ,
96
- } ,
108
+ subscription. stream . serialized_params ( ) ,
97
109
sqlite:: Destructor :: STATIC ,
98
110
) ?;
99
111
stmt. bind_int64 (
@@ -109,13 +121,10 @@ INSERT INTO ps_stream_subscriptions (stream_name, local_priority, local_params,
109
121
let stmt = db
110
122
. prepare_v2 ( "UPDATE ps_stream_subscriptions SET ttl = NULL WHERE stream_name = ? AND local_params = ?" )
111
123
. into_db_result ( db) ?;
112
- stmt. bind_text ( 1 , & subscription. stream , sqlite:: Destructor :: STATIC ) ?;
124
+ stmt. bind_text ( 1 , & subscription. name , sqlite:: Destructor :: STATIC ) ?;
113
125
stmt. bind_text (
114
126
2 ,
115
- match & subscription. params {
116
- Some ( params) => params. get ( ) ,
117
- None => "null" ,
118
- } ,
127
+ subscription. serialized_params ( ) ,
119
128
sqlite:: Destructor :: STATIC ,
120
129
) ?;
121
130
stmt. exec ( ) ?;
0 commit comments