Skip to content

Commit

Permalink
Adopt priorities in example app
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Feb 21, 2025
1 parent 4ebad73 commit b060725
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
19 changes: 19 additions & 0 deletions demos/supabase-todolist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ Create a new PowerSync instance, connecting to the database of the Supabase proj

Then deploy the following sync rules:

```yaml
bucket_definitions:
user_lists:
priority: 1
parameters: select id as list_id from lists where owner_id = request.user_id()
data:
- select * from lists where id = bucket.list_id

user_todos:
parameters: select id as list_id from lists where owner_id = request.user_id()
data:
- select * from todos where list_id = bucket.list_id
```
The rules synchronize list with a higher priority the items within the list. This can be
useful to keep the list overview page reactive during a large sync cycle affecting many
rows in the `user_todos` bucket. The two buckets can also be unified into a single one if
priorities are not important (the app will work without changes):

```yaml
bucket_definitions:
user_lists:
Expand Down
5 changes: 4 additions & 1 deletion demos/supabase-todolist/lib/widgets/lists_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:powersync/powersync.dart';

import './list_item.dart';
import './list_item_dialog.dart';
Expand Down Expand Up @@ -51,6 +52,8 @@ class ListsWidget extends StatefulWidget {
}

class _ListsWidgetState extends State<ListsWidget> {
static final _listsPriority = BucketPriority(1);

List<TodoList> _data = [];
bool hasSynced = false;
StreamSubscription? _subscription;
Expand All @@ -75,7 +78,7 @@ class _ListsWidgetState extends State<ListsWidget> {
return;
}
setState(() {
hasSynced = status.hasSynced ?? false;
hasSynced = status.statusForPriority(_listsPriority).hasSynced ?? false;
});
});
}
Expand Down
19 changes: 14 additions & 5 deletions demos/supabase-todolist/lib/widgets/todo_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,20 @@ class TodoListWidgetState extends State<TodoListWidget> {

@override
Widget build(BuildContext context) {
return ListView(
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: _data.map((todo) {
return TodoItemWidget(todo: todo);
}).toList(),
return StreamBuilder(
stream: TodoList.watchSyncStatus().map((e) => e.hasSynced),
builder: (context, snapshot) {
if (snapshot.data ?? false) {
return const Text('Busy with sync');
}

return ListView(
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: _data.map((todo) {
return TodoItemWidget(todo: todo);
}).toList(),
);
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
8 changes: 4 additions & 4 deletions demos/supabase-todolist/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -478,28 +478,28 @@ packages:
path: "../../packages/powersync"
relative: true
source: path
version: "1.11.2"
version: "1.11.3"
powersync_attachments_helper:
dependency: "direct main"
description:
path: "../../packages/powersync_attachments_helper"
relative: true
source: path
version: "0.6.18"
version: "0.6.18+1"
powersync_core:
dependency: "direct overridden"
description:
path: "../../packages/powersync_core"
relative: true
source: path
version: "1.1.2"
version: "1.1.3"
powersync_flutter_libs:
dependency: "direct overridden"
description:
path: "../../packages/powersync_flutter_libs"
relative: true
source: path
version: "0.4.4"
version: "0.4.5"
pub_semver:
dependency: transitive
description:
Expand Down

0 comments on commit b060725

Please sign in to comment.