Skip to content

Commit 7ae45db

Browse files
committed
Adopt in demo
1 parent 3bc7cf5 commit 7ae45db

File tree

8 files changed

+61
-22
lines changed

8 files changed

+61
-22
lines changed

Demo/PowerSyncExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 56;
6+
objectVersion = 60;
77
objects = {
88

99
/* Begin PBXBuildFile section */

Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/PowerSyncExample/Components/TodoListView.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AVFoundation
22
import IdentifiedCollections
3+
import PowerSync
34
import SwiftUI
45
import SwiftUINavigation
56

@@ -11,6 +12,7 @@ struct TodoListView: View {
1112
@State private var error: Error?
1213
@State private var newTodo: NewTodo?
1314
@State private var editing: Bool = false
15+
@State private var loadingListItems: Bool = false
1416

1517
#if os(iOS)
1618
// Called when a photo has been captured. Individual widgets should register the listener
@@ -33,6 +35,10 @@ struct TodoListView: View {
3335
}
3436
}
3537
}
38+
39+
if (loadingListItems) {
40+
ProgressView()
41+
}
3642

3743
ForEach(todos) { todo in
3844
#if os(iOS)
@@ -142,6 +148,22 @@ struct TodoListView: View {
142148
}
143149
}
144150
}
151+
.task {
152+
if (Secrets.previewSyncStreams) {
153+
// With sync streams, todo items are not loaded by default. We have to request them while we need them.
154+
// Thanks to builtin caching, navingating to the same list multiple times does not have to fetch items again.
155+
loadingListItems = true
156+
do {
157+
// This will make the sync client request items from this list as long as we keep a reference to the stream subscription,
158+
// and a default TTL of one day afterwards.
159+
let streamSubscription = try await system.db.syncStream(name: "todos", params: ["list": JsonValue.string(listId)]).subscribe()
160+
try await streamSubscription.waitForFirstSync()
161+
} catch {
162+
print("Error subscribing to list stream \(error)")
163+
}
164+
loadingListItems = false
165+
}
166+
}
145167
}
146168

147169
func toggleCompletion(of todo: Todo) async {

Demo/PowerSyncExample/PowerSync/SystemManager.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ final class SystemManager {
7878
options: ConnectOptions(
7979
clientConfiguration: SyncClientConfiguration(
8080
requestLogger: SyncRequestLoggerConfiguration(
81-
requestLevel: .headers
81+
requestLevel: .all
8282
) { message in
8383
self.db.logger.debug(message, tag: "SyncRequest")
8484
}
85-
)
85+
),
86+
newClientImplementation: true,
8687
)
8788
)
8889
try await attachments?.startSync()

Demo/PowerSyncExample/Screens/HomeScreen.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ struct HomeScreen: View {
88

99

1010
var body: some View {
11-
1211
ListView()
1312
.toolbar {
1413
ToolbarItem(placement: .cancellationAction) {

Demo/PowerSyncExample/Secrets.template.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,24 @@ extension Secrets {
1717
static var supabaseStorageBucket: String? {
1818
return nil
1919
}
20+
21+
static var previewSyncStreams: Bool {
22+
/*
23+
Set to true to preview https://docs.powersync.com/usage/sync-streams.
24+
When enabling this, also set your sync rules to the following:
25+
26+
streams:
27+
lists:
28+
query: SELECT * FROM lists WHERE owner_id = auth.user_id()
29+
auto_subscribe: true
30+
todos:
31+
query: SELECT * FROM todos WHERE list_id = subscription.parameter('list') AND list_id IN (SELECT id FROM lists WHERE owner_id = auth.user_id())
32+
33+
config:
34+
edition: 2
35+
36+
*/
37+
38+
false
39+
}
2040
}

Demo/PowerSyncExample/_Secrets.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ protocol SecretsProvider {
66
static var supabaseURL: URL { get }
77
static var supabaseAnonKey: String { get }
88
static var supabaseStorageBucket: String? { get }
9+
static var previewSyncStreams: Bool { get }
910
}
1011

1112
// Default conforming type

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,14 @@ public struct ConnectOptions: Sendable {
8181
/// - retryDelay: Delay TimeInterval between retry attempts in milliseconds. Defaults to `5` seconds.
8282
/// - params: Custom sync parameters to send to the server. Defaults to an empty dictionary.
8383
/// - clientConfiguration: Configuration for the HTTP client used to connect to PowerSync.
84+
/// - newClientImplementation: Whether to use a new sync client implemented in Rust. Currently defaults to
85+
/// `false`, but we encourage users to try it out.
8486
public init(
8587
crudThrottle: TimeInterval = 1,
8688
retryDelay: TimeInterval = 5,
8789
params: JsonParam = [:],
88-
clientConfiguration: SyncClientConfiguration? = nil
89-
) {
90-
self.crudThrottle = crudThrottle
91-
self.retryDelay = retryDelay
92-
self.params = params
93-
newClientImplementation = false
94-
self.clientConfiguration = clientConfiguration
95-
}
96-
97-
/// Initializes a ``ConnectOptions`` instance with optional values, including experimental options.
98-
@_spi(PowerSyncExperimental)
99-
public init(
100-
crudThrottle: TimeInterval = 1,
101-
retryDelay: TimeInterval = 5,
102-
params: JsonParam = [:],
90+
clientConfiguration: SyncClientConfiguration? = nil,
10391
newClientImplementation: Bool = false,
104-
clientConfiguration: SyncClientConfiguration? = nil
10592
) {
10693
self.crudThrottle = crudThrottle
10794
self.retryDelay = retryDelay

0 commit comments

Comments
 (0)