Skip to content

Commit

Permalink
feat: add product mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 1, 2024
1 parent 7209bd6 commit 3c0f094
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions firebase/lib/src/firebase_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ mixin FirebaseAppMixin implements App {
});
}
}

/// Helper for any app service (firestore, storage...)
mixin FirebaseProductServiceMixin<T> {
/// Most implementation need a single instance, keep it in memory!
final _instances = <App, T>{};

T getInstance(App app, T Function() createIfNotFound) {
var instance = _instances[app];
if (instance == null) {
var newInstance = instance = createIfNotFound();
_instances[app] = newInstance;
}
return instance!;
}
}
20 changes: 20 additions & 0 deletions firebase/test/firebase_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class FirebaseAppServiceMock implements FirebaseAppService {

class FirebaseAppOptionsMock with FirebaseAppOptionsMixin {}

class FirebaseProductMock {}

class FirebaseProductServiceMock
with FirebaseProductServiceMixin<FirebaseProductMock> {
FirebaseProductMock product(App app) =>
getInstance(app, () => FirebaseProductMock());
}

void main() {
group('firebase', () {
test('service', () async {
Expand All @@ -66,5 +74,17 @@ void main() {
await app.delete();
expect(service.initCount, 0);
});
test('product service', () {
var firebase = FirebaseMock();
var app1 = firebase.initializeApp(name: 'app1');
var app2 = firebase.initializeApp(name: 'app2');
expect(app1, isNot(app2));
var service = FirebaseProductServiceMock();
var product1 = service.product(app1);
var product2 = service.product(app2);
var product1bis = service.product(app1);
expect(product1, isNot(product2));
expect(product1, product1bis);
});
});
}

0 comments on commit 3c0f094

Please sign in to comment.