Skip to content

Commit 85a9b18

Browse files
authored
feat(remote_config, web): migrate web to js_interop to be compatible with WASM (#12489)
* feat(analytics, web): migrate web to js_interop to be compatible with WASM * fix version
1 parent 363f88a commit 85a9b18

File tree

3 files changed

+84
-46
lines changed

3 files changed

+84
-46
lines changed

packages/firebase_remote_config/firebase_remote_config_web/lib/src/interop/firebase_remote_config.dart

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:convert' show utf8;
6+
import 'dart:js_interop';
7+
68
import 'package:firebase_core_web/firebase_core_web_interop.dart';
79
import 'package:firebase_remote_config_platform_interface/firebase_remote_config_platform_interface.dart';
10+
811
import 'firebase_remote_config_interop.dart' as remote_config_interop;
912

1013
/// Given an AppJSImp, return the Remote Config instance.
@@ -55,12 +58,14 @@ class RemoteConfig
5558

5659
/// Returns the timestamp of the last *successful* fetch.
5760
DateTime get fetchTime {
58-
return DateTime.fromMillisecondsSinceEpoch(jsObject.fetchTimeMillis);
61+
return DateTime.fromMillisecondsSinceEpoch(
62+
jsObject.fetchTimeMillis.toDartInt,
63+
);
5964
}
6065

6166
/// The status of the last fetch attempt.
6267
RemoteConfigFetchStatus get lastFetchStatus {
63-
switch (jsObject.lastFetchStatus) {
68+
switch (jsObject.lastFetchStatus.toDart) {
6469
case 'no-fetch-yet':
6570
return RemoteConfigFetchStatus.notFetchedYet;
6671
case 'success':
@@ -70,29 +75,33 @@ class RemoteConfig
7075
case 'throttle':
7176
return RemoteConfigFetchStatus.throttle;
7277
default:
73-
throw UnimplementedError(jsObject.lastFetchStatus);
78+
throw UnimplementedError(jsObject.lastFetchStatus.toDart);
7479
}
7580
}
7681

7782
/// Makes the last fetched config available to the getters.
7883
/// Returns a future which resolves to `true` if the current call activated the fetched configs.
7984
/// If the fetched configs were already activated, the promise will resolve to `false`.
80-
Future<bool> activate() async =>
81-
handleThenable(remote_config_interop.activate(jsObject));
85+
Future<bool> activate() async => remote_config_interop
86+
.activate(jsObject)
87+
.toDart
88+
.then((value) => value! as bool);
8289

8390
/// Ensures the last activated config are available to the getters.
8491
Future<void> ensureInitialized() async =>
85-
handleThenable(remote_config_interop.ensureInitialized(jsObject));
92+
remote_config_interop.ensureInitialized(jsObject).toDart;
8693

8794
/// Fetches and caches configuration from the Remote Config service.
8895
Future<void> fetch() async =>
89-
handleThenable(remote_config_interop.fetchConfig(jsObject));
96+
remote_config_interop.fetchConfig(jsObject).toDart;
9097

9198
/// Performs fetch and activate operations, as a convenience.
9299
/// Returns a promise which resolves to true if the current call activated the fetched configs.
93100
/// If the fetched configs were already activated, the promise will resolve to false.
94101
Future<bool> fetchAndActivate() async =>
95-
handleThenable(remote_config_interop.fetchAndActivate(jsObject));
102+
remote_config_interop.fetchAndActivate(jsObject).toDart.then(
103+
(value) => value! as bool,
104+
);
96105

97106
/// Returns all config values.
98107
Map<String, RemoteConfigValue> getAll() {
@@ -104,23 +113,28 @@ class RemoteConfig
104113
}
105114

106115
RemoteConfigValue getValue(String key) => RemoteConfigValue(
107-
utf8.encode(remote_config_interop.getValue(jsObject, key).asString()),
108-
getSource(remote_config_interop.getValue(jsObject, key).getSource()),
116+
utf8.encode(
117+
remote_config_interop.getValue(jsObject, key.toJS).asString().toDart,
118+
),
119+
getSource(
120+
remote_config_interop.getValue(jsObject, key.toJS).getSource().toDart,
121+
),
109122
);
110123

111124
/// Gets the value for the given key as a boolean.
112125
/// Convenience method for calling `remoteConfig.getValue(key).asString()`.
113126
bool getBoolean(String key) =>
114-
remote_config_interop.getBoolean(jsObject, key);
127+
remote_config_interop.getBoolean(jsObject, key.toJS).toDart;
115128

116129
/// Gets the value for the given key as a number.
117130
/// Convenience method for calling `remoteConfig.getValue(key).asNumber()`.
118-
num getNumber(String key) => remote_config_interop.getNumber(jsObject, key);
131+
num getNumber(String key) =>
132+
remote_config_interop.getNumber(jsObject, key.toJS).toDartDouble;
119133

120134
/// Gets the value for the given key as a string.
121135
/// Convenience method for calling `remoteConfig.getValue(key).asString()`.
122136
String getString(String key) =>
123-
remote_config_interop.getString(jsObject, key);
137+
remote_config_interop.getString(jsObject, key.toJS).toDart;
124138

125139
void setLogLevel(RemoteConfigLogLevel value) {
126140
remote_config_interop.setLogLevel(
@@ -129,7 +143,8 @@ class RemoteConfig
129143
RemoteConfigLogLevel.debug: 'debug',
130144
RemoteConfigLogLevel.error: 'error',
131145
RemoteConfigLogLevel.silent: 'silent',
132-
}[value]!,
146+
}[value]!
147+
.toJS,
133148
);
134149
}
135150
}
@@ -157,19 +172,19 @@ class RemoteConfigSettings
157172
/// Defines the maximum age in milliseconds of an entry in the config cache before
158173
/// it is considered stale. Defaults to twelve hours.
159174
Duration get minimumFetchInterval =>
160-
Duration(milliseconds: jsObject.minimumFetchIntervalMillis);
175+
Duration(milliseconds: jsObject.minimumFetchIntervalMillis.toDartInt);
161176

162177
set minimumFetchInterval(Duration value) {
163-
jsObject.minimumFetchIntervalMillis = value.inMilliseconds;
178+
jsObject.minimumFetchIntervalMillis = value.inMilliseconds.toJS;
164179
}
165180

166181
/// Defines the maximum amount of time to wait for a response when fetching
167182
/// configuration from the Remote Config server. Defaults to one minute.
168183
Duration get fetchTimeoutMillis =>
169-
Duration(milliseconds: jsObject.fetchTimeoutMillis);
184+
Duration(milliseconds: jsObject.fetchTimeoutMillis.toDartInt);
170185

171186
set fetchTimeoutMillis(Duration value) {
172-
jsObject.fetchTimeoutMillis = value.inMilliseconds;
187+
jsObject.fetchTimeoutMillis = value.inMilliseconds.toJS;
173188
}
174189
}
175190

packages/firebase_remote_config/firebase_remote_config_web/lib/src/interop/firebase_remote_config_interop.dart

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,93 @@
77
@JS('firebase_remote_config')
88
library firebase.remote_config_interop;
99

10-
import 'package:js/js.dart';
10+
import 'dart:js_interop';
11+
1112
import 'package:firebase_core_web/firebase_core_web_interop.dart';
1213

1314
@JS()
15+
@staticInterop
1416
external RemoteConfigJsImpl getRemoteConfig([AppJsImpl? app]);
1517

1618
@JS()
17-
external PromiseJsImpl<bool> activate(RemoteConfigJsImpl remoteConfig);
19+
@staticInterop
20+
external JSPromise /* bool */ activate(RemoteConfigJsImpl remoteConfig);
1821

1922
@JS()
20-
external PromiseJsImpl<void> ensureInitialized(RemoteConfigJsImpl remoteConfig);
23+
@staticInterop
24+
external JSPromise ensureInitialized(RemoteConfigJsImpl remoteConfig);
2125

2226
@JS()
23-
external PromiseJsImpl<bool> fetchAndActivate(RemoteConfigJsImpl remoteConfig);
27+
@staticInterop
28+
external JSPromise /* bool */ fetchAndActivate(RemoteConfigJsImpl remoteConfig);
2429

2530
@JS()
26-
external PromiseJsImpl<void> fetchConfig(RemoteConfigJsImpl remoteConfig);
31+
@staticInterop
32+
external JSPromise fetchConfig(RemoteConfigJsImpl remoteConfig);
2733

2834
@JS()
29-
external dynamic getAll(RemoteConfigJsImpl remoteConfig);
35+
@staticInterop
36+
external JSAny getAll(RemoteConfigJsImpl remoteConfig);
3037

3138
@JS()
32-
external bool getBoolean(RemoteConfigJsImpl remoteConfig, String key);
39+
@staticInterop
40+
external JSBoolean getBoolean(RemoteConfigJsImpl remoteConfig, JSString key);
3341

3442
@JS()
35-
external num getNumber(RemoteConfigJsImpl remoteConfig, String key);
43+
@staticInterop
44+
external JSNumber getNumber(RemoteConfigJsImpl remoteConfig, JSString key);
3645

3746
@JS()
38-
external String getString(RemoteConfigJsImpl remoteConfig, String key);
47+
@staticInterop
48+
external JSString getString(RemoteConfigJsImpl remoteConfig, JSString key);
3949

4050
@JS()
41-
external ValueJsImpl getValue(RemoteConfigJsImpl remoteConfig, String key);
51+
@staticInterop
52+
external ValueJsImpl getValue(RemoteConfigJsImpl remoteConfig, JSString key);
4253

4354
// TODO - api to be implemented
4455
@JS()
45-
external PromiseJsImpl<void> isSupported();
56+
@staticInterop
57+
external JSPromise isSupported();
4658

4759
@JS()
48-
external void setLogLevel(RemoteConfigJsImpl remoteConfig, String logLevel);
60+
@staticInterop
61+
external void setLogLevel(RemoteConfigJsImpl remoteConfig, JSString logLevel);
4962

5063
@JS('RemoteConfig')
51-
abstract class RemoteConfigJsImpl {
64+
@staticInterop
65+
abstract class RemoteConfigJsImpl {}
66+
67+
extension RemoteConfigJsImplExtension on RemoteConfigJsImpl {
5268
external AppJsImpl get app;
5369
external SettingsJsImpl get settings;
5470
external set settings(SettingsJsImpl value);
55-
external Object get defaultConfig;
56-
external set defaultConfig(Object value);
57-
external int get fetchTimeMillis;
58-
external String get lastFetchStatus;
71+
external JSObject get defaultConfig;
72+
external set defaultConfig(JSObject value);
73+
external JSNumber get fetchTimeMillis;
74+
external JSString get lastFetchStatus;
5975
}
6076

6177
@JS()
78+
@staticInterop
6279
@anonymous
63-
abstract class ValueJsImpl {
64-
external bool asBoolean();
65-
external num asNumber();
66-
external String asString();
67-
external String getSource();
80+
abstract class ValueJsImpl {}
81+
82+
extension ValueJsImplExtension on ValueJsImpl {
83+
external JSBoolean asBoolean();
84+
external JSNumber asNumber();
85+
external JSString asString();
86+
external JSString getSource();
6887
}
6988

7089
@JS()
90+
@staticInterop
7191
@anonymous
72-
abstract class SettingsJsImpl {
73-
external int get minimumFetchIntervalMillis;
74-
external set minimumFetchIntervalMillis(int value);
75-
external int get fetchTimeoutMillis;
76-
external set fetchTimeoutMillis(int value);
92+
abstract class SettingsJsImpl {}
93+
94+
extension SettingsJsImplExtension on SettingsJsImpl {
95+
external JSNumber get minimumFetchIntervalMillis;
96+
external set minimumFetchIntervalMillis(JSNumber value);
97+
external JSNumber get fetchTimeoutMillis;
98+
external set fetchTimeoutMillis(JSNumber value);
7799
}

packages/firebase_remote_config/firebase_remote_config_web/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository: https://github.com/firebase/flutterfire/tree/master/packages/firebas
66
version: 1.4.26
77

88
environment:
9-
sdk: '>=2.18.0 <4.0.0'
9+
sdk: '>=3.2.0 <4.0.0'
1010
flutter: '>=3.3.0'
1111

1212
dependencies:
@@ -18,6 +18,7 @@ dependencies:
1818
flutter_web_plugins:
1919
sdk: flutter
2020
js: ^0.6.3
21+
web: ^0.5.1
2122

2223
dev_dependencies:
2324
build_runner: ^2.3.3

0 commit comments

Comments
 (0)