Skip to content

WIP: Use sync worker #169

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 45 additions & 0 deletions demos/django-todolist/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "5874a72aa4c779a02553007c47dacbefba2374dc"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: android
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: ios
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: linux
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: macos
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: web
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
- platform: windows
create_revision: 5874a72aa4c779a02553007c47dacbefba2374dc
base_revision: 5874a72aa4c779a02553007c47dacbefba2374dc

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.powersync.django_todolist

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity()
12 changes: 12 additions & 0 deletions demos/django-todolist/ios/RunnerTests/RunnerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Flutter
import UIKit
import XCTest

class RunnerTests: XCTestCase {

func testExample() {
// If you add code to the Runner application, consider adding tests here.
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
}

}
9 changes: 8 additions & 1 deletion demos/django-todolist/lib/powersync.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file performs setup of the PowerSync database
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -99,8 +100,14 @@ Future<bool> isLoggedIn() async {
}

Future<String> getDatabasePath() async {
const dbFilename = 'powersync-demo.db';
// getApplicationSupportDirectory is not supported on Web
if (kIsWeb) {
return dbFilename;
}

final dir = await getApplicationSupportDirectory();
return join(dir.path, 'powersync-demo.db');
return join(dir.path, dbFilename);
}

// opens the database and connects if logged in
Expand Down
24 changes: 12 additions & 12 deletions demos/django-todolist/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -212,18 +212,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
mutex:
dependency: transitive
description:
Expand Down Expand Up @@ -479,10 +479,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -519,10 +519,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.5"
web:
dependency: transitive
description:
Expand Down
4 changes: 4 additions & 0 deletions demos/django-todolist/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ dev_dependencies:

flutter_lints: ^3.0.1

dependency_overrides:
sqlite_async:
path: /home/simon/src/sqlite_async.dart/packages/sqlite_async

flutter:
uses-material-design: true
Binary file added demos/django-todolist/web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/django-todolist/web/icons/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/django-todolist/web/icons/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions demos/django-todolist/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.

The path provided below has to start and end with a slash "/" in order for
it to work correctly.

For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">

<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="django_todolist">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>django_todolist</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>
35 changes: 35 additions & 0 deletions demos/django-todolist/web/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "django_todolist",
"short_name": "django_todolist",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "A new Flutter project.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
{
"src": "icons/Icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/Icon-512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "icons/Icon-maskable-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "icons/Icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}
4 changes: 4 additions & 0 deletions demos/supabase-anonymous-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ dev_dependencies:

flutter_lints: ^3.0.1

dependency_overrides:
sqlite_async:
path: /home/simon/src/sqlite_async.dart/packages/sqlite_async

flutter:
uses-material-design: true
4 changes: 4 additions & 0 deletions demos/supabase-edge-function-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ dev_dependencies:

flutter_lints: ^3.0.1

dependency_overrides:
sqlite_async:
path: /home/simon/src/sqlite_async.dart/packages/sqlite_async

flutter:
uses-material-design: true
4 changes: 4 additions & 0 deletions demos/supabase-simple-chat/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ dev_dependencies:
# rules and activating additional ones.
flutter_lints: ^3.0.1

dependency_overrides:
sqlite_async:
path: /home/simon/src/sqlite_async.dart/packages/sqlite_async

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand Down
4 changes: 4 additions & 0 deletions demos/supabase-todolist/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ dev_dependencies:

flutter_lints: ^3.0.1

dependency_overrides:
sqlite_async:
path: /home/simon/src/sqlite_async.dart/packages/sqlite_async

flutter:
uses-material-design: true
15 changes: 10 additions & 5 deletions packages/powersync/lib/src/database/powersync_db_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
@protected
Future<void> baseInit() async {
statusStream = statusStreamController.stream;
updates = database.updates
.map((update) =>
PowerSyncUpdateNotification.fromUpdateNotification(update))
.where((update) => update.isNotEmpty)
.cast<UpdateNotification>();
updates = powerSyncUpdateNotifications(database.updates);

await database.initialize();
await _checkVersion();
Expand Down Expand Up @@ -466,3 +462,12 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
await database.refreshSchema();
}
}

Stream<UpdateNotification> powerSyncUpdateNotifications(
Stream<UpdateNotification> inner) {
return inner
.map((update) =>
PowerSyncUpdateNotification.fromUpdateNotification(update))
.where((update) => update.isNotEmpty)
.cast<UpdateNotification>();
}
45 changes: 33 additions & 12 deletions packages/powersync/lib/src/database/web/web_powersync_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'package:powersync/src/streaming_sync.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:powersync/src/schema_logic.dart' as schema_logic;

import '../../web/sync_controller.dart';

/// A PowerSync managed database.
///
/// Web implementation for [PowerSyncDatabase]
Expand Down Expand Up @@ -93,7 +95,8 @@ class PowerSyncDatabaseImpl
Logger? logger}) {
final db = SqliteDatabase.withFactory(openFactory, maxReaders: 1);
return PowerSyncDatabaseImpl.withDatabase(
schema: schema, logger: logger, database: db);
schema: schema, logger: logger, database: db)
..openFactory = openFactory;
}

/// Open a PowerSyncDatabase on an existing [SqliteDatabase].
Expand All @@ -119,14 +122,15 @@ class PowerSyncDatabaseImpl
/// The connection is automatically re-opened if it fails for any reason.
///
/// Status changes are reported on [statusStream].
baseConnect(
{required PowerSyncBackendConnector connector,

/// Throttle time between CRUD operations
/// Defaults to 10 milliseconds.
required Duration crudThrottleTime,
required Future<void> Function() reconnect,
Map<String, dynamic>? params}) async {
baseConnect({
required PowerSyncBackendConnector connector,

/// Throttle time between CRUD operations
/// Defaults to 10 milliseconds.
required Duration crudThrottleTime,
required Future<void> Function() reconnect,
Map<String, dynamic>? params,
}) async {
await initialize();

// Disconnect if connected
Expand All @@ -135,9 +139,23 @@ class PowerSyncDatabaseImpl

await isInitialized;

// TODO better multitab support
final storage = BucketStorage(database);
final sync = StreamingSyncImplementation(
StreamingSync sync;
// Try using a shared worker for the synchronization implementation to avoid
// duplicating work across tabs.
try {
sync = await SyncWorkerHandle.start(
this,
connector,
Uri.base.resolve('/powersync_sync.worker.js'),
);
} catch (e) {
logger.warning(
'Could not use shared worker for synchronization, falling back to locks.',
e,
);

sync = StreamingSyncImplementation(
adapter: storage,
credentialsCallback: connector.getCredentialsCached,
invalidCredentialsCallback: connector.fetchCredentials,
Expand All @@ -148,7 +166,10 @@ class PowerSyncDatabaseImpl
syncParameters: params,
// Only allows 1 sync implementation to run at a time per database
// This should be global (across tabs) when using Navigator locks.
identifier: database.openFactory.path);
identifier: database.openFactory.path,
);
}

sync.statusStream.listen((event) {
setStatus(event);
});
Expand Down
Loading