Skip to content

Commit

Permalink
fix(configurator): Fix configurator web unsupported error
Browse files Browse the repository at this point in the history
  • Loading branch information
riscait committed Oct 22, 2024
1 parent a02b1d0 commit ae40a14
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
6 changes: 3 additions & 3 deletions packages/altfire_configurator/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import 'dart:async';
class Config<T> {
Config({
required T value,
required StreamSubscription<void> subscription,
required StreamSubscription<void>? subscription,
}) : _value = value,
_subscription = subscription;

/// The subscription to the stream of updated parameter information.
final StreamSubscription<void> _subscription;
final StreamSubscription<void>? _subscription;

/// The current value of the parameter.
T get value => _value;
final T _value;

/// Closes the [Config].
Future<void> dispose() async {
await _subscription.cancel();
await _subscription?.cancel();
}
}
89 changes: 52 additions & 37 deletions packages/altfire_configurator/lib/src/configurator.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import 'dart:convert';

import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart';

import 'config.dart';

typedef ValueChanged<T> = void Function(T value);
typedef _ValueChanged<T> = void Function(T value);

/// A class that wraps Remote Config.
/// Its role is to "fetch the configured parameters from remote and provide
/// them".
///
/// Exposes [fetchAndActivate] and configuration methods for Remote Config.
class Configurator {
/// If [rc] is null, a singleton instance of [FirebaseRemoteConfig] is used.
Configurator({
FirebaseRemoteConfig? rc,
}) : _rc = rc ?? FirebaseRemoteConfig.instance;
Expand Down Expand Up @@ -116,99 +117,113 @@ class Configurator {
/// Returns a [Config] of type [String].
Config<String> getStringConfig(
String key, {
required ValueChanged<String> onConfigUpdated,
_ValueChanged<String>? onConfigUpdated,
}) {
return Config<String>(
value: getString(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getString(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getString(key));
}),
);
}

/// Returns a [Config] of type [int].
Config<int> getIntConfig(
String key, {
required ValueChanged<int> onConfigUpdated,
_ValueChanged<int>? onConfigUpdated,
}) {
return Config<int>(
value: getInt(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getInt(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getInt(key));
}),
);
}

/// Returns a [Config] of type [double].
Config<double> getDoubleConfig(
String key, {
required ValueChanged<double> onConfigUpdated,
_ValueChanged<double>? onConfigUpdated,
}) {
return Config<double>(
value: getDouble(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getDouble(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getDouble(key));
}),
);
}

/// Returns a [Config] of type [bool].
Config<bool> getBoolConfig(
String key, {
required ValueChanged<bool> onConfigUpdated,
_ValueChanged<bool>? onConfigUpdated,
}) {
return Config<bool>(
value: getBool(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getBool(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getBool(key));
}),
);
}

/// Returns a [Config] of type [Map].
Config<Map<String, Object?>> getJsonConfig(
String key, {
required ValueChanged<Map<String, Object?>> onConfigUpdated,
_ValueChanged<Map<String, Object?>>? onConfigUpdated,
}) {
return Config<Map<String, Object?>>(
value: getJson(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getJson(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getJson(key));
}),
);
}

/// Returns a [Config] of type [List] of [Map].
Config<List<Map<String, Object?>>> getListJsonConfig(
String key, {
required ValueChanged<List<Map<String, Object?>>> onConfigUpdated,
_ValueChanged<List<Map<String, Object?>>>? onConfigUpdated,
}) {
return Config<List<Map<String, Object?>>>(
value: getListJson(key),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getListJson(key));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getListJson(key));
}),
);
}

/// Returns a [Config] of type [T].
Config<T> getDataConfig<T extends Object>(
String key, {
required T Function(Map<String, Object?>) fromJson,
required ValueChanged<T> onConfigUpdated,
_ValueChanged<T>? onConfigUpdated,
}) {
return Config<T>(
value: getData<T>(key: key, fromJson: fromJson),
subscription: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getData(key: key, fromJson: fromJson));
}),
subscription: kIsWeb || onConfigUpdated == null
? null
: filteredOnConfigUpdated(key).listen((event) async {
await activate();
onConfigUpdated(getData(key: key, fromJson: fromJson));
}),
);
}
}
2 changes: 2 additions & 0 deletions packages/altfire_configurator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ environment:

dependencies:
firebase_remote_config: ^5.1.3
flutter:
sdk: flutter
meta: ^1.12.0

dev_dependencies:
Expand Down

0 comments on commit ae40a14

Please sign in to comment.