Skip to content

Commit

Permalink
feat: add region to function call
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 4, 2024
1 parent c4b85a1 commit 016ad78
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
2 changes: 2 additions & 0 deletions functions_call_flutter/lib/functions_call_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library;

export 'package:tekartik_firebase_functions/firebase_functions.dart'
show regionBelgium, regionUsCentral1, regionFrankfurt;
export 'src/functions_call.dart'
show
FirebaseFunctionsCallService,
Expand Down
11 changes: 8 additions & 3 deletions functions_call_flutter/lib/src/functions_call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class FirebaseFunctionsCallableOptions {

/// Sets whether or not to use limited-use App Check tokens when invoking the associated function.
bool limitedUseAppCheckToken;

@override
String toString() =>
'FirebaseFunctionsCallableOptions(timeout: $timeout${limitedUseAppCheckToken == true ? ', limitedUseAppCheckToken: $limitedUseAppCheckToken' : ''})';
}

/// Firebase functions call
Expand Down Expand Up @@ -72,15 +76,16 @@ mixin FirebaseFunctionsCallDefaultMixin implements FirebaseFunctionsCall {
/// Firebase functions call service
abstract class FirebaseFunctionsCallService {
/// Get the firebase functions call instance
FirebaseFunctionsCall functionsCall(App app);
FirebaseFunctionsCall functionsCall(App app, {required String region});
}

/// Firebase functions call service default mixin
mixin FirebaseFunctionsCallServiceDefaultMixin
implements FirebaseFunctionsCallService {
@override
FirebaseFunctionsCall functionsCall(App app) {
throw UnimplementedError('FirebaseFunctionsCallService.functionsCall');
FirebaseFunctionsCall functionsCall(App app, {required String region}) {
throw UnimplementedError(
'FirebaseFunctionsCallService.functionsCall(${app.name}, $region)');
}
}

Expand Down
35 changes: 23 additions & 12 deletions functions_call_flutter/lib/src/functions_call_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cloud_functions/cloud_functions.dart' as native;
import 'package:tekartik_common_utils/common_utils_import.dart';
import 'package:tekartik_firebase/firebase.dart';
// ignore: implementation_imports
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'
Expand All @@ -16,22 +17,32 @@ FirebaseFunctionsCallService get firebaseFunctionsCallServiceFlutter =>

/// Firebase functions call service flutter
class FirebaseFunctionsCallServiceFlutter
with FirebaseProductServiceMixin<FirebaseFunctionsCallFlutter>
implements FirebaseFunctionsCallService {
/// Most implementation need a single instance, keep it in memory!
final _instances = <String, FirebaseFunctionsCallFlutter>{};

FirebaseFunctionsCallFlutter _getInstance(App app, String region,
FirebaseFunctionsCallFlutter Function() createIfNotFound) {
var key = '${app.name}_$region';
var instance = _instances[key];
if (instance == null) {
var newInstance = instance = createIfNotFound();
_instances[key] = newInstance;
}
return instance;
}

@override
FirebaseFunctionsCallFlutter functionsCall(App app) {
return getInstance(app, () {
FirebaseFunctionsCallFlutter functionsCall(App app,
{required String region}) {
return _getInstance(app, region, () {
assert(app is FirebaseAppFlutter, 'invalid firebase app type');
var appFlutter = app as FirebaseAppFlutter;
if (appFlutter.isDefault!) {
return FirebaseFunctionsCallFlutter(
this, native.FirebaseFunctions.instance);
} else {
return FirebaseFunctionsCallFlutter(
this,
native.FirebaseFunctions.instanceFor(
app: appFlutter.nativeInstance!));
}

return FirebaseFunctionsCallFlutter(
this,
native.FirebaseFunctions.instanceFor(
app: appFlutter.nativeInstance!, region: region));
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions functions_call_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ dependencies:
url: https://github.com/tekartik/firebase.dart
ref: dart3a
path: firebase
tekartik_common_utils:
git:
url: https://github.com/tekartik/common_utils.dart
ref: dart3a
tekartik_firebase_functions:
git:
url: https://github.com/tekartik/firebase_functions.dart
ref: dart3a
path: firebase_functions
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 016ad78

Please sign in to comment.