Skip to content

Commit 54c111b

Browse files
committed
Fixing empty local-only state to online transition breaking watch calls.
1 parent 9ad96f4 commit 54c111b

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

demos/local-only-todolist/lib/models/schema.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ Schema makeSchema({synced = bool}) {
7373
Table(table.name, table.columns,
7474
indexes: table.indexes, viewName: onlineName(table.name)),
7575
for (var table in tables)
76-
Table.localOnly('local_${table.name}', table.columns,
76+
Table.localOnly('inactive_local_${table.name}', table.columns,
7777
indexes: table.indexes, viewName: localName(table.name))
7878
]);
7979
}
8080

81-
switchToOnlineSchema(PowerSyncDatabase db, String userId) async {
81+
switchToSyncedSchema(PowerSyncDatabase db, String userId) async {
8282
await db.updateSchema(makeSchema(synced: true));
8383
await setSyncEnabled(true);
8484

demos/local-only-todolist/lib/powersync.dart

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,42 @@ Future<String> getDatabasePath() async {
157157
}
158158

159159
Future<void> openDatabase() async {
160-
var isSyncMode = await getSyncEnabled();
161-
db = PowerSyncDatabase(
162-
schema: makeSchema(synced: isSyncMode),
163-
path: await getDatabasePath(),
164-
logger: attachedLogger);
165-
await db.initialize();
160+
await _openDatabase();
166161

167162
await loadSupabase();
168163
if (isLoggedIn()) {
169164
// If the user is already logged in, connect immediately.
170165
// Otherwise, connect once logged via login/signup view.
171-
connectDatabase();
166+
await connectDatabase();
172167
}
173168
}
174169

170+
Future<void> _openDatabase() async {
171+
var isSyncMode = await getSyncEnabled();
172+
db = PowerSyncDatabase(
173+
schema: makeSchema(synced: isSyncMode),
174+
path: await getDatabasePath(),
175+
logger: attachedLogger);
176+
await db.initialize();
177+
}
178+
175179
Future<void> connectDatabase() async {
176-
if (isLoggedIn()) {
180+
if (!isLoggedIn()) {
177181
log.severe("Can't connect database without being signed in");
178182
}
179183
SupabaseConnector? currentConnector;
180184
var isSyncMode = await getSyncEnabled();
181185

182186
if (!isSyncMode) {
183-
await switchToOnlineSchema(db, getUserId());
187+
await switchToSyncedSchema(db, getUserId());
188+
// Without closing and reopening the database the list pages breaks if there is no data in the tables when logging in/signing up.
189+
await db.close();
190+
await _openDatabase();
184191
}
185192

186193
currentConnector = SupabaseConnector(db);
187-
db.connect(connector: currentConnector);
194+
await db.connect(connector: currentConnector);
195+
// await Future.delayed(const Duration(seconds: 5));
188196

189197
Supabase.instance.client.auth.onAuthStateChange.listen((data) async {
190198
final AuthChangeEvent event = data.event;

0 commit comments

Comments
 (0)