Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ class FirebaseRemoteConfig extends FirebasePluginPlatform {
/// Starts listening for real-time config updates from the Remote Config backend and automatically
/// fetches updates from the RC backend when they are available.
///
/// This feature is not supported on Web.
/// On web, you must call [fetchAndActivate] before listening to this stream. Events will only be
/// received after an initial call to [fetchAndActivate].
///
/// If a connection to the Remote Config backend is not already open, calling this method will
/// open it. Multiple listeners can be added by calling this method again, but subsequent calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ class FirebaseRemoteConfigWeb extends FirebaseRemoteConfigPlatform {

@override
Stream<RemoteConfigUpdate> get onConfigUpdated {
throw UnsupportedError('onConfigUpdated is not supported for web');
return _delegate.onConfigUpdated
.map((event) => RemoteConfigUpdate(event.updatedKeys));
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert' show utf8;
import 'dart:js_interop';

Expand Down Expand Up @@ -158,6 +159,32 @@ class RemoteConfig
.setCustomSignals(jsObject, customSignals.jsify()! as JSObject)
.toDart;
}

StreamController<RemoteConfigUpdatePayload>? _onConfigUpdatedController;

Stream<RemoteConfigUpdatePayload> get onConfigUpdated {
if (_onConfigUpdatedController == null) {
_onConfigUpdatedController =
StreamController<RemoteConfigUpdatePayload>.broadcast(sync: true);
final errorWrapper = (JSObject error) {
_onConfigUpdatedController?.addError(error);
};
final nextWrapper =
(remote_config_interop.ConfigUpdateJsImpl configUpdate) {
_onConfigUpdatedController
?.add(RemoteConfigUpdatePayload._fromJsObject(configUpdate));
};
remote_config_interop.ConfigUpdateObserver observer =
remote_config_interop.ConfigUpdateObserver(
error: errorWrapper.toJS,
next: nextWrapper.toJS,
);

remote_config_interop.onConfigUpdate(jsObject, observer);
}

return _onConfigUpdatedController!.stream;
}
}

ValueSource getSource(String source) {
Expand Down Expand Up @@ -220,3 +247,19 @@ enum RemoteConfigLogLevel {
error,
silent,
}

class RemoteConfigUpdatePayload
extends JsObjectWrapper<remote_config_interop.ConfigUpdateJsImpl> {
RemoteConfigUpdatePayload._fromJsObject(
remote_config_interop.ConfigUpdateJsImpl jsObject,
) : super.fromJsObject(jsObject);

Set<String> get updatedKeys {
final updatedKeysSet = <String>{};
final callback = (JSAny key, JSString value, JSAny set) {
updatedKeysSet.add(value.toDart);
};
jsObject.getUpdatedKeys().forEach(callback.toJS);
return updatedKeysSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,41 @@ extension SettingsJsImplExtension on SettingsJsImpl {
external JSNumber get fetchTimeoutMillis;
external set fetchTimeoutMillis(JSNumber value);
}

@JS()
@staticInterop
@anonymous
abstract class ConfigUpdateObserver {
external factory ConfigUpdateObserver({
JSAny complete,
JSAny error,
JSAny next,
});
}

extension ConfigUpdateObserverJsImpl on ConfigUpdateObserver {
external JSAny get next;
external JSAny get error;
external JSAny get complete;
}

@JS()
@staticInterop
@anonymous
abstract class ConfigUpdateJsImpl {}

extension ConfigUpdateJsImplExtension on ConfigUpdateJsImpl {
external JSSet getUpdatedKeys();
}

@JS()
@staticInterop
external JSFunction onConfigUpdate(
RemoteConfigJsImpl remoteConfig,
ConfigUpdateObserver observer,
);

@JS('Set')
extension type JSSet._(JSObject _) implements JSObject {
external void forEach(JSAny callback);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ void main() {

await configSubscription.cancel();
},
// This feature is not supported on Web
skip: kIsWeb,
);

test('default values', () async {
Expand Down
Loading