Skip to content

Commit

Permalink
refactor(firebase_dart_flutter): WriteBatch constructor now takes a D…
Browse files Browse the repository at this point in the history
…atabaseReference
  • Loading branch information
rbellens committed Sep 11, 2023
1 parent 60ed127 commit 9d79d93
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/firebase_dart_plus/lib/src/write_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:sortedmap/sortedmap.dart';
import 'package:firebase_dart/database.dart';

extension FirebaseDatabaseWithWriteBatch on FirebaseDatabase {
WriteBatch batch() => WriteBatch(this);
WriteBatch batch() => WriteBatch(reference());
}

/// A WriteBatch is a series of write operations to be performed as one unit.
Expand Down Expand Up @@ -42,16 +42,16 @@ extension FirebaseDatabaseWithWriteBatch on FirebaseDatabase {
///
///
class WriteBatch {
final FirebaseDatabase _database;
final DatabaseReference _rootReference;

final List<TreeOperation> _operations = [];

bool _committed = false;

WriteBatch(this._database);
WriteBatch(DatabaseReference ref) : _rootReference = ref.root();

DatabaseReference reference() =>
TransactionalDatabaseReference(this, _database.reference());
TransactionalDatabaseReference(this, _rootReference);

Future<void> commit() async {
if (_committed) throw StateError('Batch already committed');
Expand All @@ -64,7 +64,7 @@ class WriteBatch {

if (updates.length == 1) {
var e = updates.entries.first;
await _database.reference().child(e.key).set(e.value);
await _rootReference.child(e.key).set(e.value);
} else {
var p = updates.keys.reduce((value, element) {
var a = Name.parsePath(value);
Expand All @@ -78,12 +78,12 @@ class WriteBatch {
return a.length < b.length ? value : element;
});
if (p.isEmpty) {
await _database.reference().update(updates);
await _rootReference.update(updates);
} else {
updates = {
for (var e in updates.entries) e.key.substring(p.length + 1): e.value
};
await _database.reference().child(p).update(updates);
await _rootReference.child(p).update(updates);
}
}

Expand Down Expand Up @@ -274,8 +274,7 @@ class TransactionalDatabaseReference extends TransactionalQuery
}

@override
Uri get url => Uri.parse(_transaction._database.databaseURL)
.replace(path: _path.join('/'));
Uri get url => _transaction._rootReference.url.replace(path: _path.join('/'));
}

extension _QueryX on Query {
Expand Down

0 comments on commit 9d79d93

Please sign in to comment.