Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Native: Add the `NativeSqliteOpenFactory.beforeOpen` method, which can be overridden to configure
SQLite asynchronously before opening databases.
- Web: Upgrade `package:sqlite3_web` to flush IndexedDB writes automatically, deprecate `flush()`.

## 0.14.3

Expand Down
27 changes: 19 additions & 8 deletions packages/sqlite_async/lib/src/web/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import 'database.dart';
import 'update_notifications.dart';
import 'web_mutex.dart';

const _deprecatedFlush = Deprecated(
'The flush parameter no longer does anything, IndexedDB databases are '
'persisted after each write lock.',
);

/// An endpoint that can be used, by any running JavaScript context in the same
/// website, to connect to an existing [WebSqliteConnection].
///
Expand Down Expand Up @@ -74,36 +79,42 @@ abstract class WebSqliteConnection implements SqliteConnection {
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
/// See [WebSqliteConnection.flush] for details.
@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext, bool? flush});
{Duration? lockTimeout,
String? debugContext,
@_deprecatedFlush bool? flush});

@override
Future<T> abortableWriteLock<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Future<void>? abortTrigger,
String? debugContext,
bool? flush});
@_deprecatedFlush bool? flush});

/// Same as [SqliteConnection.writeTransaction].
///
/// Has an additional [flush] (defaults to true). This can be set to false
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
/// See [WebSqliteConnection.flush] for details.
@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout,
bool? flush});
@_deprecatedFlush bool? flush});

/// Flush changes to the underlying storage.
///
/// When this returns, all changes previously written will be persisted
/// to storage.
/// In older versions of the `sqlite3` package, writes on IndexedDB databases
/// used to be asynchronous and might not complete if a tab writing to a
/// database was closed shortly after making its write.
///
/// This only has an effect when IndexedDB storage is used.
/// This is no longer an issue, recent versions persist changes in an
/// IndexedDB transaction before the transaction completes. Thus, this method
/// is deprecated and should not be used anymore.
@_deprecatedFlush
Future<void> flush();
}
21 changes: 2 additions & 19 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ final class WebDatabase extends SqliteDatabaseImpl
(unscoped) => ScopedReadContext.assumeReadLock(unscoped, callback),
abortTrigger: abortTrigger,
debugContext: debugContext,
flush: false,
);
}

Expand All @@ -121,7 +120,6 @@ final class WebDatabase extends SqliteDatabaseImpl
},
);
},
flush: flush ?? true,
abortTrigger: lockTimeout?.asTimeout,
);
}
Expand All @@ -133,7 +131,6 @@ final class WebDatabase extends SqliteDatabaseImpl
callback,
abortTrigger: lockTimeout?.asTimeout,
debugContext: debugContext,
flush: flush,
);
}

Expand All @@ -147,40 +144,26 @@ final class WebDatabase extends SqliteDatabaseImpl
(unscoped) {
return ScopedWriteContext.assumeWriteLock(unscoped, callback);
},
flush: flush ?? true,
debugContext: debugContext,
abortTrigger: abortTrigger,
);
}

Future<T> _lockInternal<T>(
Future<T> Function(_UnscopedContext) callback, {
required bool flush,
Future<void>? abortTrigger,
String? debugContext,
}) async {
if (_mutex case var mutex?) {
return await mutex.lock(abortTrigger: abortTrigger, () async {
final context = _UnscopedContext(this, null);
try {
return await callback(context);
} finally {
if (flush) {
await this.flush();
}
}
return await callback(context);
});
} else {
return await _database.requestLock(abortTrigger: abortTrigger,
(token) async {
final context = _UnscopedContext(this, token);
try {
return await callback(context);
} finally {
if (flush) {
await this.flush();
}
}
return await callback(context);
}).translateAbortExceptions(debugContext ?? 'lock');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class AsyncWebDatabaseImpl extends SqliteDatabaseImpl
await isInitialized;
return _runZoned(() {
return _connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext, flush: flush);
lockTimeout: lockTimeout, debugContext: debugContext);
}, debugContext: debugContext ?? 'execute()');
}

Expand All @@ -114,7 +114,7 @@ final class AsyncWebDatabaseImpl extends SqliteDatabaseImpl
await isInitialized;
return _runZoned(() {
return _connection.abortableWriteLock(callback,
abortTrigger: abortTrigger, debugContext: debugContext, flush: flush);
abortTrigger: abortTrigger, debugContext: debugContext);
}, debugContext: debugContext ?? 'execute()');
}

Expand All @@ -125,8 +125,7 @@ final class AsyncWebDatabaseImpl extends SqliteDatabaseImpl
bool? flush}) async {
await isInitialized;
return _runZoned(
() => _connection.writeTransaction(callback,
lockTimeout: lockTimeout, flush: flush),
() => _connection.writeTransaction(callback, lockTimeout: lockTimeout),
debugContext: 'writeTransaction()');
}

Expand Down
6 changes: 3 additions & 3 deletions packages/sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sqlite_async
description: High-performance asynchronous interface for SQLite on Dart and Flutter.
version: 0.14.3
version: 0.14.4
resolution: workspace
repository: https://github.com/powersync-ja/sqlite_async.dart
environment:
Expand All @@ -13,8 +13,8 @@ topics:
- flutter

dependencies:
sqlite3: ^3.2.0
sqlite3_web: '>=0.8.0 <0.10.0'
sqlite3: ^3.4.0
sqlite3_web: ^0.9.3
sqlite3_connection_pool: ^0.2.4
async: ^2.10.0
collection: ^1.17.0
Expand Down