Skip to content
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

Migrate Hive v2 to support Flutter web WASM #1291

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4eb0dbc
Convert backend_manager.dart
Rexios80 May 29, 2024
80e08a1
Refactor backend_manager.dart
Rexios80 May 29, 2024
8c06b3a
Migrate storage_backend_js.dart
Rexios80 May 29, 2024
9000568
Migrate box_collection_indexed_db.dart
Rexios80 May 29, 2024
c85611b
Migrate tests
Rexios80 May 29, 2024
1f9dd04
Update conditional imports
Rexios80 May 29, 2024
60028ec
Fix function definitions
Rexios80 May 29, 2024
bb357d5
Fixing type issues
Rexios80 May 29, 2024
498b83a
Fix test
Rexios80 May 29, 2024
24adc6a
Fixing type issues
Rexios80 May 29, 2024
523ae80
Fixing things
Rexios80 May 29, 2024
7914d19
More fixes
Rexios80 May 29, 2024
b8e8092
More fixes
Rexios80 May 29, 2024
456dab3
Update generator
Rexios80 May 29, 2024
888f810
Fix type checking issues
Rexios80 Jun 2, 2024
26c7fa3
Fixing typing issues
Rexios80 Jun 2, 2024
8da7426
Fixing tests
Rexios80 Jun 2, 2024
1392931
Use secure random for box ids in testing
Rexios80 Jun 2, 2024
148d9a4
Cleanup
Rexios80 Jun 2, 2024
a1b7569
More cleanup
Rexios80 Jun 2, 2024
7b3c4c3
Even more cleanup
Rexios80 Jun 2, 2024
d33391c
Refactor `asFuture()` usages to be typed
Rexios80 Jun 2, 2024
9ea3255
Use window.self.indexedDB everywhere
Rexios80 Jun 2, 2024
5db58f5
Regenerate
Rexios80 Jun 2, 2024
0ef4d98
Update action to test both web compilers
Rexios80 Jun 2, 2024
5f347b4
Writing BoxCollection tests
Rexios80 Jun 2, 2024
2a9944e
Fix cursor issues
Rexios80 Jun 2, 2024
ebdef6c
Cleanup
Rexios80 Jun 3, 2024
d98a9a5
Refactor cursor iteration back into a stream
Rexios80 Jun 3, 2024
8df4953
Fixing CI
Rexios80 Jun 3, 2024
339c347
CI should pass
Rexios80 Jun 10, 2024
2797358
Maybe this time
Rexios80 Jun 10, 2024
690d174
Update sdk constraint in all projects
Rexios80 Jun 10, 2024
41be86c
Fix mixin issues
Rexios80 Jun 10, 2024
bc99e26
Update all actions dependencies
Rexios80 Jun 10, 2024
6380501
Fixing more actions issues
Rexios80 Jun 10, 2024
f4334da
...
Rexios80 Jun 10, 2024
d169f5c
...
Rexios80 Jun 10, 2024
a424eb4
Require Dart 3.4.0
Rexios80 Jun 10, 2024
b257e9d
...
Rexios80 Jun 10, 2024
0e8751e
...
Rexios80 Jun 10, 2024
b3fda79
...
Rexios80 Jun 10, 2024
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
10 changes: 5 additions & 5 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ jobs:
image: google/dart:latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Collect coverage
run: |
pub get
pub global activate test_coverage
pub global run test_coverage --exclude "**/js/**"
dart pub get
dart pub global activate test_coverage
dart pub global run test_coverage --exclude "**/js/**"
working-directory: hive
- uses: codecov/codecov-action@v1.0.0
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: hive/coverage/lcov.info
48 changes: 26 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
test-platform: [vm, chrome]
dart-channel: ["2.12.0", "2.13.0", "2.14.0"]
include:
- test-platform: vm
compiler: kernel
- test-platform: chrome
compiler: dart2js
- test-platform: chrome
compiler: dart2wasm
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- uses: browser-actions/setup-chrome@v1
- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.dart-channel }}
sdk: 3.4.0
- name: Install dependencies
run: pub get
run: dart pub get
working-directory: hive
- name: Run tests
run: pub run test -p ${{ matrix.test-platform }}
run: dart test -p ${{ matrix.test-platform }} -c ${{ matrix.compiler }}
working-directory: hive

test-hive-flutter:
runs-on: ubuntu-latest
strategy:
matrix:
flutter-channel: [dev, beta, stable]
flutter-channel: [beta, stable]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-java@v1
with:
java-version: "12.x"
- uses: subosito/flutter-action@v1
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: ${{ matrix.flutter-channel }}
- name: Override dependency version
Expand All @@ -49,17 +52,17 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
dart-channel: ["2.12.0", "2.13.0"]
dart-channel: ["3.4.0"]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ matrix.dart-channel }}
- name: Install dependencies
run: pub get
run: dart pub get
working-directory: hive_generator/example
- name: Generate build_runner output
run: pub run build_runner build --delete-conflicting-outputs
run: dart pub run build_runner build --delete-conflicting-outputs
working-directory: hive_generator/example

check-score:
Expand All @@ -68,9 +71,10 @@ jobs:
matrix:
package: [hive, hive_generator, hive_flutter]
steps:
- uses: actions/checkout@v1
- uses: axel-op/dart-package-analyzer@v3
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
relativePath: ${{ matrix.package }}
minAnnotationLevel: warning
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
- run: |
cd ${{ matrix.package }}
flutter pub get
dart pub global activate pana
pana --no-warning --exit-code-threshold 0
2 changes: 1 addition & 1 deletion hive/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dev_dependencies:
build_runner: any

environment:
sdk: '>=2.12.0-0 <3.0.0'
sdk: ^3.0.0

dependency_overrides:
hive:
Expand Down
2 changes: 1 addition & 1 deletion hive/lib/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'package:hive/src/util/extensions.dart';
import 'package:meta/meta.dart';

export 'src/box_collection/box_collection_stub.dart'
if (dart.library.html) 'package:hive/src/box_collection/box_collection_indexed_db.dart'
if (dart.library.js_interop) 'package:hive/src/box_collection/box_collection_indexed_db.dart'
if (dart.library.io) 'package:hive/src/box_collection/box_collection.dart';
export 'src/object/hive_object.dart' show HiveObject, HiveObjectMixin;

Expand Down
79 changes: 40 additions & 39 deletions hive/lib/src/backend/js/native/backend_manager.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import 'dart:async';
import 'dart:html';
import 'dart:indexed_db';
import 'dart:js' as js;
import 'dart:js_interop';
import 'package:hive/hive.dart';
import 'package:hive/src/backend/js/native/storage_backend_js.dart';
import 'package:hive/src/backend/js/native/utils.dart';
import 'package:hive/src/backend/storage_backend.dart';
import 'package:web/web.dart';

/// Opens IndexedDB databases
class BackendManager implements BackendManagerInterface {
IdbFactory? get indexedDB => js.context.hasProperty('window')
? window.indexedDB
: WorkerGlobalScope.instance.indexedDB;
IDBFactory? get indexedDB => window.self.indexedDB;

@override
Future<StorageBackend> open(String name, String? path, bool crashRecovery,
Expand All @@ -19,29 +17,28 @@ class BackendManager implements BackendManagerInterface {
final databaseName = collection ?? name;
final objectStoreName = collection == null ? 'box' : name;

var db =
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) {
var db = e.target.result as Database;
if (!(db.objectStoreNames ?? []).contains(objectStoreName)) {
final request = indexedDB!.open(databaseName, 1);
request.onupgradeneeded = (IDBVersionChangeEvent e) {
var db = (e.target as IDBOpenDBRequest).result as IDBDatabase;
if (!db.objectStoreNames.contains(objectStoreName)) {
db.createObjectStore(objectStoreName);
}
});
}.toJS;
var db = await request.asFuture<IDBDatabase>();

// in case the objectStore is not contained, re-open the db and
// update version
if (!(db.objectStoreNames ?? []).contains(objectStoreName)) {
if (!db.objectStoreNames.contains(objectStoreName)) {
print(
'Creating objectStore $objectStoreName in database $databaseName...');
db = await indexedDB!.open(
databaseName,
version: (db.version ?? 1) + 1,
onUpgradeNeeded: (e) {
var db = e.target.result as Database;
if (!(db.objectStoreNames ?? []).contains(objectStoreName)) {
db.createObjectStore(objectStoreName);
}
},
);
final request = indexedDB!.open(databaseName, db.version + 1);
request.onupgradeneeded = (IDBVersionChangeEvent e) {
var db = (e.target as IDBOpenDBRequest).result as IDBDatabase;
if (!db.objectStoreNames.contains(objectStoreName)) {
db.createObjectStore(objectStoreName);
}
}.toJS;
db = await request.asFuture<IDBDatabase>();
}

print('Got object store $objectStoreName in database $databaseName.');
Expand All @@ -59,17 +56,18 @@ class BackendManager implements BackendManagerInterface {

// directly deleting the entire DB if a non-collection Box
if (collection == null) {
await indexedDB!.deleteDatabase(databaseName);
await indexedDB!.deleteDatabase(databaseName).asFuture();
} else {
final db =
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) {
var db = e.target.result as Database;
if ((db.objectStoreNames ?? []).contains(objectStoreName)) {
final request = indexedDB!.open(databaseName, 1);
request.onupgradeneeded = (IDBVersionChangeEvent e) {
var db = (e.target as IDBOpenDBRequest).result as IDBDatabase;
if (db.objectStoreNames.contains(objectStoreName)) {
db.deleteObjectStore(objectStoreName);
}
});
if ((db.objectStoreNames ?? []).isEmpty) {
indexedDB!.deleteDatabase(databaseName);
}.toJS;
final db = await request.asFuture<IDBDatabase>();
if (db.objectStoreNames.length == 0) {
await indexedDB!.deleteDatabase(databaseName).asFuture();
}
}
}
Expand All @@ -83,17 +81,20 @@ class BackendManager implements BackendManagerInterface {
try {
var _exists = true;
if (collection == null) {
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) {
e.target.transaction!.abort();
final request = indexedDB!.open(databaseName, 1);
request.onupgradeneeded = (IDBVersionChangeEvent e) {
(e.target as IDBOpenDBRequest).transaction!.abort();
_exists = false;
});
}.toJS;
await request.asFuture();
} else {
final db =
await indexedDB!.open(collection, version: 1, onUpgradeNeeded: (e) {
var db = e.target.result as Database;
_exists = (db.objectStoreNames ?? []).contains(objectStoreName);
});
_exists = (db.objectStoreNames ?? []).contains(objectStoreName);
final request = indexedDB!.open(collection, 1);
request.onupgradeneeded = (IDBVersionChangeEvent e) {
var db = (e.target as IDBOpenDBRequest).result as IDBDatabase;
_exists = db.objectStoreNames.contains(objectStoreName);
}.toJS;
final db = await request.asFuture<IDBDatabase>();
_exists = db.objectStoreNames.contains(objectStoreName);
}
return _exists;
} catch (error) {
Expand Down
Loading