Skip to content

Implement sync client #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use nested options for parameters
  • Loading branch information
simolus3 committed Jun 10, 2025
commit 9e737ec69de0e426a3a1213c88d42df15dd63aff
27 changes: 16 additions & 11 deletions crates/core/src/sync/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::ToString;
use alloc::{string::String, vec::Vec};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use sqlite::{ResultCode, Value};
use sqlite_nostd::{self as sqlite, ColumnType};
use sqlite_nostd::{Connection, Context};
Expand All @@ -16,15 +16,20 @@ use crate::error::SQLiteError;
use super::streaming_sync::SyncClient;
use super::sync_status::DownloadSyncStatus;

/// Payload provided by SDKs when requesting a sync iteration.
#[derive(Default, Deserialize)]
pub struct StartSyncStream {
/// Bucket parameters to include in the request when opening a sync stream.
#[serde(default)]
pub parameters: Option<serde_json::Map<String, serde_json::Value>>,
}

/// A request sent from a client SDK to the [SyncClient] with a `powersync_control` invocation.
pub enum SyncControlRequest<'a> {
/// The client requests to start a sync iteration.
///
/// Earlier iterations are implicitly dropped when receiving this request.
StartSyncStream {
/// Bucket parameters to include in the request when opening a sync stream.
parameters: Option<serde_json::Map<String, serde_json::Value>>,
},
StartSyncStream(StartSyncStream),
/// The client requests to stop the current sync iteration.
StopSyncStream,
/// The client is forwading a sync event to the core extension.
Expand Down Expand Up @@ -137,13 +142,13 @@ pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {

let op = op.text();
let event = match op {
"start" => SyncControlRequest::StartSyncStream {
parameters: if payload.value_type() == ColumnType::Text {
Some(serde_json::from_str(payload.text())?)
"start" => SyncControlRequest::StartSyncStream({
if payload.value_type() == ColumnType::Text {
serde_json::from_str(payload.text())?
} else {
None
},
},
StartSyncStream::default()
}
}),
"stop" => SyncControlRequest::StopSyncStream,
"line_text" => SyncControlRequest::SyncEvent(SyncEvent::TextLine {
data: if payload.value_type() == ColumnType::Text {
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/sync/streaming_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl SyncClient {
event: SyncControlRequest<'a>,
) -> Result<Vec<Instruction>, SQLiteError> {
match event {
SyncControlRequest::StartSyncStream { parameters } => {
SyncControlRequest::StartSyncStream(options) => {
self.state.tear_down()?;

let mut handle = SyncIterationHandle::new(self.db, parameters)?;
let mut handle = SyncIterationHandle::new(self.db, options.parameters)?;
let instructions = handle.initialize()?;
self.state = ClientState::IterationActive(handle);

Expand Down
10 changes: 8 additions & 2 deletions dart/test/goldens/starting_stream.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[
{
"operation": "start",
"data": null,
"data": {
"parameters": {
"foo": "bar"
}
},
"output": [
{
"UpdateSyncStatus": {
Expand All @@ -21,7 +25,9 @@
"raw_data": true,
"binary_data": true,
"client_id": "test-test-test-test",
"parameters": null
"parameters": {
"foo": "bar"
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion dart/test/sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ void _syncTests<T>({
group('goldens', () {
syncTest('starting stream', (_) {
matcher.load('starting_stream');
invokeControl('start', null);
invokeControl(
'start',
json.encode({
'parameters': {'foo': 'bar'}
}),
);
});

syncTest('simple sync iteration', (_) {
Expand Down