Skip to content

Commit 9e737ec

Browse files
committed
Use nested options for parameters
1 parent d675370 commit 9e737ec

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

crates/core/src/sync/interface.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloc::boxed::Box;
66
use alloc::rc::Rc;
77
use alloc::string::ToString;
88
use alloc::{string::String, vec::Vec};
9-
use serde::Serialize;
9+
use serde::{Deserialize, Serialize};
1010
use sqlite::{ResultCode, Value};
1111
use sqlite_nostd::{self as sqlite, ColumnType};
1212
use sqlite_nostd::{Connection, Context};
@@ -16,15 +16,20 @@ use crate::error::SQLiteError;
1616
use super::streaming_sync::SyncClient;
1717
use super::sync_status::DownloadSyncStatus;
1818

19+
/// Payload provided by SDKs when requesting a sync iteration.
20+
#[derive(Default, Deserialize)]
21+
pub struct StartSyncStream {
22+
/// Bucket parameters to include in the request when opening a sync stream.
23+
#[serde(default)]
24+
pub parameters: Option<serde_json::Map<String, serde_json::Value>>,
25+
}
26+
1927
/// A request sent from a client SDK to the [SyncClient] with a `powersync_control` invocation.
2028
pub enum SyncControlRequest<'a> {
2129
/// The client requests to start a sync iteration.
2230
///
2331
/// Earlier iterations are implicitly dropped when receiving this request.
24-
StartSyncStream {
25-
/// Bucket parameters to include in the request when opening a sync stream.
26-
parameters: Option<serde_json::Map<String, serde_json::Value>>,
27-
},
32+
StartSyncStream(StartSyncStream),
2833
/// The client requests to stop the current sync iteration.
2934
StopSyncStream,
3035
/// The client is forwading a sync event to the core extension.
@@ -137,13 +142,13 @@ pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
137142

138143
let op = op.text();
139144
let event = match op {
140-
"start" => SyncControlRequest::StartSyncStream {
141-
parameters: if payload.value_type() == ColumnType::Text {
142-
Some(serde_json::from_str(payload.text())?)
145+
"start" => SyncControlRequest::StartSyncStream({
146+
if payload.value_type() == ColumnType::Text {
147+
serde_json::from_str(payload.text())?
143148
} else {
144-
None
145-
},
146-
},
149+
StartSyncStream::default()
150+
}
151+
}),
147152
"stop" => SyncControlRequest::StopSyncStream,
148153
"line_text" => SyncControlRequest::SyncEvent(SyncEvent::TextLine {
149154
data: if payload.value_type() == ColumnType::Text {

crates/core/src/sync/streaming_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ impl SyncClient {
4949
event: SyncControlRequest<'a>,
5050
) -> Result<Vec<Instruction>, SQLiteError> {
5151
match event {
52-
SyncControlRequest::StartSyncStream { parameters } => {
52+
SyncControlRequest::StartSyncStream(options) => {
5353
self.state.tear_down()?;
5454

55-
let mut handle = SyncIterationHandle::new(self.db, parameters)?;
55+
let mut handle = SyncIterationHandle::new(self.db, options.parameters)?;
5656
let instructions = handle.initialize()?;
5757
self.state = ClientState::IterationActive(handle);
5858

dart/test/goldens/starting_stream.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[
22
{
33
"operation": "start",
4-
"data": null,
4+
"data": {
5+
"parameters": {
6+
"foo": "bar"
7+
}
8+
},
59
"output": [
610
{
711
"UpdateSyncStatus": {
@@ -21,7 +25,9 @@
2125
"raw_data": true,
2226
"binary_data": true,
2327
"client_id": "test-test-test-test",
24-
"parameters": null
28+
"parameters": {
29+
"foo": "bar"
30+
}
2531
}
2632
}
2733
}

dart/test/sync_test.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ void _syncTests<T>({
141141
group('goldens', () {
142142
syncTest('starting stream', (_) {
143143
matcher.load('starting_stream');
144-
invokeControl('start', null);
144+
invokeControl(
145+
'start',
146+
json.encode({
147+
'parameters': {'foo': 'bar'}
148+
}),
149+
);
145150
});
146151

147152
syncTest('simple sync iteration', (_) {

0 commit comments

Comments
 (0)