-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upstream FluffyBox patches (isar#956)
- add `BoxCollection` - prepare support for other web backends Fixes: isar#939 Signed-off-by: TheOneWithTheBraid <the-one@with-the-braid.cf>
- Loading branch information
Showing
17 changed files
with
705 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!lib/src/backend/js/web_worker/web_worker.dart.js* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,18 @@ | ||
import 'dart:html'; | ||
import 'dart:indexed_db'; | ||
import 'dart:js' as js; | ||
import 'package:hive/hive.dart'; | ||
import 'package:hive/src/backend/js/storage_backend_js.dart'; | ||
import 'package:hive/src/backend/storage_backend.dart'; | ||
|
||
/// Opens IndexedDB databases | ||
class BackendManager implements BackendManagerInterface { | ||
IdbFactory? get indexedDB => js.context.hasProperty('window') | ||
? window.indexedDB | ||
: WorkerGlobalScope.instance.indexedDB; | ||
|
||
@override | ||
Future<StorageBackend> open( | ||
String name, String? path, bool crashRecovery, HiveCipher? cipher) async { | ||
var db = await indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) { | ||
var db = e.target.result as Database; | ||
if (!db.objectStoreNames!.contains('box')) { | ||
db.createObjectStore('box'); | ||
} | ||
}); | ||
|
||
return StorageBackendJs(db, cipher); | ||
} | ||
import 'native/backend_manager.dart' as native; | ||
|
||
@override | ||
Future<void> deleteBox(String name, String? path) { | ||
return indexedDB!.deleteDatabase(name); | ||
} | ||
/// Opens IndexedDB databases | ||
abstract class BackendManager { | ||
BackendManager._(); | ||
|
||
@override | ||
Future<bool> boxExists(String name, String? path) async { | ||
// https://stackoverflow.com/a/17473952 | ||
try { | ||
var _exists = true; | ||
await indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) { | ||
e.target.transaction!.abort(); | ||
_exists = false; | ||
}); | ||
return _exists; | ||
} catch (error) { | ||
return false; | ||
// dummy implementation as the WebWorker branch is not stable yet | ||
static BackendManagerInterface select( | ||
[HiveStorageBackendPreference? backendPreference]) { | ||
switch (backendPreference) { | ||
default: | ||
return native.BackendManager(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'dart:async'; | ||
import 'dart:html'; | ||
import 'dart:indexed_db'; | ||
import 'dart:js' as js; | ||
import 'package:hive/hive.dart'; | ||
import 'package:hive/src/backend/js/native/storage_backend_js.dart'; | ||
import 'package:hive/src/backend/storage_backend.dart'; | ||
|
||
/// Opens IndexedDB databases | ||
class BackendManager implements BackendManagerInterface { | ||
IdbFactory? get indexedDB => js.context.hasProperty('window') | ||
? window.indexedDB | ||
: WorkerGlobalScope.instance.indexedDB; | ||
|
||
@override | ||
Future<StorageBackend> open(String name, String? path, bool crashRecovery, | ||
HiveCipher? cipher, String? collection) async { | ||
// compatibility for old store format | ||
final databaseName = collection ?? name; | ||
final objectStoreName = collection == null ? 'box' : name; | ||
|
||
final db = | ||
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { | ||
var db = e.target.result as Database; | ||
if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { | ||
db.createObjectStore(objectStoreName); | ||
} | ||
}); | ||
|
||
return StorageBackendJs(db, cipher, objectStoreName); | ||
} | ||
|
||
@override | ||
Future<void> deleteBox(String name, String? path, String? collection) async { | ||
// compatibility for old store format | ||
final databaseName = collection ?? name; | ||
final objectStoreName = collection == null ? 'box' : name; | ||
|
||
// directly deleting the entire DB if a non-collection Box | ||
if (collection == null) { | ||
await indexedDB!.deleteDatabase(databaseName); | ||
} else { | ||
final db = | ||
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { | ||
var db = e.target.result as Database; | ||
if ((db.objectStoreNames ?? []).contains(objectStoreName)) { | ||
db.deleteObjectStore(objectStoreName); | ||
} | ||
}); | ||
if ((db.objectStoreNames ?? []).isEmpty) { | ||
indexedDB!.deleteDatabase(databaseName); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<bool> boxExists(String name, String? path, String? collection) async { | ||
// compatibility for old store format | ||
final databaseName = collection ?? name; | ||
final objectStoreName = collection == null ? 'box' : name; | ||
// https://stackoverflow.com/a/17473952 | ||
try { | ||
var _exists = true; | ||
if (collection == null) { | ||
await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { | ||
e.target.transaction!.abort(); | ||
_exists = false; | ||
}); | ||
} 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); | ||
} | ||
return _exists; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.