Skip to content

Commit 775397d

Browse files
authored
feat(performance, web): migrate web to js_interop to be compatible with WASM (#12515)
* feat(performance, web): migrate web to js_interop to be compatible with WASM * fix tests
1 parent 14817e2 commit 775397d

File tree

5 files changed

+66
-398
lines changed

5 files changed

+66
-398
lines changed

packages/firebase_performance/firebase_performance_web/lib/src/interop/performance.dart

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:js_interop';
6+
57
import 'package:firebase_core_web/firebase_core_web_interop.dart' hide jsify;
68

79
import 'performance_interop.dart' as performance_interop;
@@ -38,45 +40,46 @@ class Performance
3840
: super.fromJsObject(jsObject);
3941

4042
Trace trace(String traceName) =>
41-
Trace.fromJsObject(performance_interop.trace(jsObject, traceName));
43+
Trace.fromJsObject(performance_interop.trace(jsObject, traceName.toJS));
4244

4345
/// Non-null App for this instance of firestore service.
4446
App get app => App.getInstance(jsObject.app);
4547

46-
bool get instrumentationEnabled => jsObject.instrumentationEnabled;
47-
bool get dataCollectionEnabled => jsObject.dataCollectionEnabled;
48+
bool get instrumentationEnabled => jsObject.instrumentationEnabled.toDart;
49+
bool get dataCollectionEnabled => jsObject.dataCollectionEnabled.toDart;
4850
}
4951

5052
class Trace extends JsObjectWrapper<performance_interop.TraceJsImpl> {
5153
Trace.fromJsObject(performance_interop.TraceJsImpl jsObject)
5254
: super.fromJsObject(jsObject);
5355

54-
String getAttribute(String attr) => jsObject.getAttribute(attr);
56+
String getAttribute(String attr) => jsObject.getAttribute(attr.toJS).toDart;
5557

5658
Map<String, String> getAttributes() {
5759
return dartify(jsObject.getAttributes()).cast<String, String>();
5860
}
5961

60-
int getMetric(String metricName) => jsObject.getMetric(metricName);
62+
int getMetric(String metricName) =>
63+
jsObject.getMetric(metricName.toJS).toDartInt;
6164

6265
void incrementMetric(String metricName, [int? num]) {
6366
if (num != null) {
64-
return jsObject.incrementMetric(metricName, num);
67+
return jsObject.incrementMetric(metricName.toJS, num.toJS);
6568
} else {
66-
return jsObject.incrementMetric(metricName);
69+
return jsObject.incrementMetric(metricName.toJS);
6770
}
6871
}
6972

7073
void putMetric(String metricName, int num) {
71-
return jsObject.putMetric(metricName, num);
74+
return jsObject.putMetric(metricName.toJS, num.toJS);
7275
}
7376

7477
void putAttribute(String attr, String value) {
75-
return jsObject.putAttribute(attr, value);
78+
return jsObject.putAttribute(attr.toJS, value.toJS);
7679
}
7780

7881
void removeAttribute(String attr) {
79-
return jsObject.removeAttribute(attr);
82+
return jsObject.removeAttribute(attr.toJS);
8083
}
8184

8285
void start() {
@@ -97,8 +100,8 @@ class PerformanceSettings
97100
bool? instrumentationEnabled,
98101
]) {
99102
final jsObject = performance_interop.PerformanceSettingsJsImpl(
100-
dataCollectionEnabled: dataCollectionEnabled,
101-
instrumentationEnabled: instrumentationEnabled,
103+
dataCollectionEnabled: dataCollectionEnabled?.toJS,
104+
instrumentationEnabled: instrumentationEnabled?.toJS,
102105
);
103106
return _expando[jsObject] ??= PerformanceSettings._fromJsObject(jsObject);
104107
}
@@ -107,13 +110,13 @@ class PerformanceSettings
107110
performance_interop.PerformanceSettingsJsImpl jsObject,
108111
) : super.fromJsObject(jsObject);
109112

110-
bool? get dataCollectionEnabled => jsObject.dataCollectionEnabled;
113+
bool? get dataCollectionEnabled => jsObject.dataCollectionEnabled?.toDart;
111114
set dataCollectionEnabled(bool? b) {
112-
jsObject.dataCollectionEnabled = b;
115+
jsObject.dataCollectionEnabled = b?.toJS;
113116
}
114117

115-
bool? get instrumentationEnabled => jsObject.instrumentationEnabled;
118+
bool? get instrumentationEnabled => jsObject.instrumentationEnabled?.toDart;
116119
set instrumentationEnabled(bool? b) {
117-
jsObject.instrumentationEnabled = b;
120+
jsObject.instrumentationEnabled = b?.toJS;
118121
}
119122
}

packages/firebase_performance/firebase_performance_web/lib/src/interop/performance_interop.dart

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,84 @@
55
@JS('firebase_performance')
66
library firebase.performance_interop;
77

8+
import 'dart:js_interop';
9+
810
import 'package:firebase_core_web/firebase_core_web_interop.dart';
9-
import 'package:js/js.dart';
1011

1112
@JS()
13+
@staticInterop
1214
external PerformanceJsImpl getPerformance([AppJsImpl? app]);
1315

1416
@JS()
17+
@staticInterop
1518
external PerformanceJsImpl initializePerformance(
1619
AppJsImpl app, [
1720
PerformanceSettingsJsImpl? settings,
1821
]);
1922

2023
@JS()
21-
external TraceJsImpl trace(PerformanceJsImpl performance, String traceName);
24+
@staticInterop
25+
external TraceJsImpl trace(PerformanceJsImpl performance, JSString traceName);
2226

2327
@JS('Performance')
24-
abstract class PerformanceJsImpl {
28+
@staticInterop
29+
abstract class PerformanceJsImpl {}
30+
31+
extension PerformanceJsImplExtension on PerformanceJsImpl {
2532
external AppJsImpl get app;
26-
external bool dataCollectionEnabled;
27-
external bool instrumentationEnabled;
33+
external JSBoolean dataCollectionEnabled;
34+
external JSBoolean instrumentationEnabled;
2835
}
2936

3037
@JS('Trace')
38+
@staticInterop
3139
@anonymous
32-
class TraceJsImpl {
33-
external String getAttribute(String attr);
34-
external Object getAttributes();
35-
external int getMetric(String metricName);
36-
external void incrementMetric(String metricName, [int? num]);
37-
external void putMetric(String metricName, int num);
38-
external void putAttribute(String attr, String value);
39-
external void removeAttribute(String attr);
40+
class TraceJsImpl {}
41+
42+
extension TraceJsImplExtension on TraceJsImpl {
43+
external JSString getAttribute(JSString attr);
44+
external JSAny getAttributes();
45+
external JSNumber getMetric(JSString metricName);
46+
external void incrementMetric(JSString metricName, [JSNumber? num]);
47+
external void putMetric(JSString metricName, JSNumber num);
48+
external void putAttribute(JSString attr, JSString value);
49+
external void removeAttribute(JSString attr);
4050
external void start();
41-
external void record(int number, int duration, [RecordOptions? options]);
51+
external void record(
52+
JSNumber number,
53+
JSNumber duration, [
54+
RecordOptions? options,
55+
]);
4256
external void stop();
4357
}
4458

4559
@JS()
60+
@staticInterop
4661
@anonymous
4762
class RecordOptions {
63+
external factory RecordOptions({JSAny? metrics, JSAny? attributes});
64+
}
65+
66+
extension RecordOptionsExtension on RecordOptions {
4867
/* map of metrics */
49-
external Object? get metrics;
68+
external JSAny? get metrics;
5069
/* map of attributes */
51-
external Object? get attributes;
52-
external factory RecordOptions({Object? metrics, Object? attributes});
70+
external JSAny? get attributes;
5371
}
5472

5573
@JS()
74+
@staticInterop
5675
@anonymous
5776
class PerformanceSettingsJsImpl {
58-
external bool? get dataCollectionEnabled;
59-
external set dataCollectionEnabled(bool? b);
60-
external bool? get instrumentationEnabled;
61-
external set instrumentationEnabled(bool? b);
6277
external factory PerformanceSettingsJsImpl({
63-
bool? dataCollectionEnabled,
64-
bool? instrumentationEnabled,
78+
JSBoolean? dataCollectionEnabled,
79+
JSBoolean? instrumentationEnabled,
6580
});
6681
}
82+
83+
extension PerformanceSettingsJsImplExtension on PerformanceSettingsJsImpl {
84+
external JSBoolean? get dataCollectionEnabled;
85+
external set dataCollectionEnabled(JSBoolean? b);
86+
external JSBoolean? get instrumentationEnabled;
87+
external set instrumentationEnabled(JSBoolean? b);
88+
}

packages/firebase_performance/firebase_performance_web/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ dependencies:
1717
flutter_web_plugins:
1818
sdk: flutter
1919
js: ^0.6.3
20+
web: ^0.5.1
21+
2022

2123
dev_dependencies:
22-
build_runner: ^2.3.3
2324
flutter_test:
2425
sdk: flutter
25-
mockito: ^5.0.10
2626

2727
# For information on the generic Dart part of this file, see the
2828
# following page: https://dart.dev/tools/pub/pubspec

packages/firebase_performance/firebase_performance_web/test/firebase_performance_web_test.dart

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)