Skip to content

Commit 0f1d5fa

Browse files
nateboschcommit-bot@chromium.org
authored andcommitted
Centralize functions to convert Promise
Merge the two copies of a function that convert a JS promise to a Dart Future. Reuse this function for the specialized case of converting to a `Future<Map>`. - Rename `convertNativePromiseToDartFuture` to the shorter and more widely used `promiseToFuture`. Update one existing use of the old name. - Make the method generic to match the previous implementation of `promiseToFuture`. - Update the code generator template to not emit the second copy of the method. - Use the existing `promiseToFuture` with a `.then` call to implement `promiseToFutureAsMap`. Update the doc comment to follow best practices. - Duplicate all above changes to nnbd SDK copy. Change-Id: Ib3bbf17477efec3666b24fd4019ca8ce68f0b5b3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/119174 Commit-Queue: Nate Bosch <[email protected]> Reviewed-by: Stephen Adams <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent e3ca5ee commit 0f1d5fa

File tree

8 files changed

+34
-97
lines changed

8 files changed

+34
-97
lines changed

sdk/lib/html/dart2js/html_dart2js.dart

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,13 @@ Window get window => JS('Window', 'window');
9090
HtmlDocument get document =>
9191
JS('returns:HtmlDocument;depends:none;effects:none;gvn:true', 'document');
9292

93-
// Supoort to convert JS Promise to a Dart Future.
94-
Future<T> promiseToFuture<T>(jsPromise) {
95-
var completer = new Completer<T>();
96-
97-
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
98-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
99-
100-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
101-
convertDartClosureToJS(thenErrorCode, 1));
102-
103-
return completer.future;
104-
}
105-
106-
// Supoort to convert JS Promise to a Dart Future<Map<String, dynamic>>. Each property of the JS
107-
// object is added to the Map as a key of type String with a value of type dynamic.
108-
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) {
109-
var completer = new Completer<Map<String, dynamic>>();
110-
111-
var thenSuccessCode = (promiseValue) =>
112-
completer.complete(convertNativeToDart_Dictionary(promiseValue));
113-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
114-
115-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
116-
convertDartClosureToJS(thenErrorCode, 1));
117-
118-
return completer.future;
119-
}
93+
/// Convert a JS Promise to a Future<Map<String, dynamic>>.
94+
///
95+
/// On a successful result the native JS result will be converted to a Dart Map.
96+
/// See [convertNativeToDart_Dictionary]. On a rejected promise the error is
97+
/// forwarded without change.
98+
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) =>
99+
promiseToFuture(jsPromise).then(convertNativeToDart_Dictionary);
120100

121101
// Workaround for tags like <cite> that lack their own Element subclass --
122102
// Dart issue 1990.

sdk/lib/html/html_common/conversions.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ abstract class _AcceptStructuredClone {
236236
}
237237

238238
if (isJavaScriptPromise(e)) {
239-
return convertNativePromiseToDartFuture(e);
239+
return promiseToFuture(e);
240240
}
241241

242242
if (isJavaScriptSimpleObject(e)) {

sdk/lib/html/html_common/conversions_dart2js.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ bool isImmutableJavaScriptArray(value) =>
9898
bool isJavaScriptPromise(value) =>
9999
JS('bool', r'typeof Promise != "undefined" && # instanceof Promise', value);
100100

101-
Future convertNativePromiseToDartFuture(promise) {
102-
var completer = new Completer();
103-
var then = convertDartClosureToJS((result) => completer.complete(result), 1);
104-
var error =
105-
convertDartClosureToJS((result) => completer.completeError(result), 1);
106-
var newPromise = JS('', '#.then(#)["catch"](#)', promise, then, error);
101+
Future<T> promiseToFuture<T>(promise) {
102+
var completer = new Completer<T>();
103+
var then = convertDartClosureToJS((r) => completer.complete(r), 1);
104+
var error = convertDartClosureToJS((e) => completer.completeError(e), 1);
105+
JS('', '#.then(#, #)', promise, then, error);
107106
return completer.future;
108107
}
109108

sdk_nnbd/lib/html/dart2js/html_dart2js.dart

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,13 @@ Window get window => JS('Window', 'window');
9292
HtmlDocument get document =>
9393
JS('returns:HtmlDocument;depends:none;effects:none;gvn:true', 'document');
9494

95-
// Supoort to convert JS Promise to a Dart Future.
96-
Future<T> promiseToFuture<T>(jsPromise) {
97-
var completer = new Completer<T>();
98-
99-
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
100-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
101-
102-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
103-
convertDartClosureToJS(thenErrorCode, 1));
104-
105-
return completer.future;
106-
}
107-
108-
// Supoort to convert JS Promise to a Dart Future<Map<String, dynamic>>. Each property of the JS
109-
// object is added to the Map as a key of type String with a value of type dynamic.
110-
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) {
111-
var completer = new Completer<Map<String, dynamic>>();
112-
113-
var thenSuccessCode = (promiseValue) =>
114-
completer.complete(convertNativeToDart_Dictionary(promiseValue));
115-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
116-
117-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
118-
convertDartClosureToJS(thenErrorCode, 1));
119-
120-
return completer.future;
121-
}
95+
/// Convert a JS Promise to a Future<Map<String, dynamic>>.
96+
///
97+
/// On a successful result the native JS result will be converted to a Dart Map.
98+
/// See [convertNativeToDart_Dictionary]. On a rejected promise the error is
99+
/// forwarded without change.
100+
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) =>
101+
promiseToFuture(jsPromise).then(convertNativeToDart_Dictionary);
122102

123103
// Workaround for tags like <cite> that lack their own Element subclass --
124104
// Dart issue 1990.

sdk_nnbd/lib/html/html_common/conversions.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ abstract class _AcceptStructuredClone {
238238
}
239239

240240
if (isJavaScriptPromise(e)) {
241-
return convertNativePromiseToDartFuture(e);
241+
return promiseToFuture(e);
242242
}
243243

244244
if (isJavaScriptSimpleObject(e)) {

sdk_nnbd/lib/html/html_common/conversions_dart2js.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ bool isImmutableJavaScriptArray(value) =>
100100
bool isJavaScriptPromise(value) =>
101101
JS('bool', r'typeof Promise != "undefined" && # instanceof Promise', value);
102102

103-
Future convertNativePromiseToDartFuture(promise) {
104-
var completer = new Completer();
105-
var then = convertDartClosureToJS((result) => completer.complete(result), 1);
106-
var error =
107-
convertDartClosureToJS((result) => completer.completeError(result), 1);
108-
var newPromise = JS('', '#.then(#)["catch"](#)', promise, then, error);
103+
Future<T> promiseToFuture<T>(promise) {
104+
var completer = new Completer<T>();
105+
var then = convertDartClosureToJS((r) => completer.complete(r), 1);
106+
var error = convertDartClosureToJS((e) => completer.completeError(e), 1);
107+
JS('', '#.then(#, #)', promise, then, error);
109108
return completer.future;
110109
}
111110

tools/dom/scripts/systemnative.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
'protocol', 'search', 'username'
2323
]
2424

25-
_promise_to_future = Conversion('convertNativePromiseToDartFuture', 'dynamic',
26-
'Future')
25+
_promise_to_future = Conversion('promiseToFuture', 'dynamic', 'Future')
2726

2827

2928
def array_type(data_type):

tools/dom/templates/html/dart2js/html_dart2js.darttemplate

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,13 @@ Window get window => JS('Window', 'window');
109109
HtmlDocument get document =>
110110
JS('returns:HtmlDocument;depends:none;effects:none;gvn:true', 'document');
111111

112-
// Supoort to convert JS Promise to a Dart Future.
113-
Future<T> promiseToFuture<T>(jsPromise) {
114-
var completer = new Completer<T>();
115-
116-
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
117-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
118-
119-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
120-
convertDartClosureToJS(thenErrorCode, 1));
121-
122-
return completer.future;
123-
}
124-
125-
// Supoort to convert JS Promise to a Dart Future<Map<String, dynamic>>. Each property of the JS
126-
// object is added to the Map as a key of type String with a value of type dynamic.
127-
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) {
128-
var completer = new Completer<Map<String, dynamic>>();
129-
130-
var thenSuccessCode = (promiseValue) =>
131-
completer.complete(convertNativeToDart_Dictionary(promiseValue));
132-
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
133-
134-
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
135-
convertDartClosureToJS(thenErrorCode, 1));
136-
137-
return completer.future;
138-
}
112+
/// Convert a JS Promise to a Future<Map<String, dynamic>>.
113+
///
114+
/// On a successful result the native JS result will be converted to a Dart Map.
115+
/// See [convertNativeToDart_Dictionary]. On a rejected promise the error is
116+
/// forwarded without change.
117+
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) =>
118+
promiseToFuture(jsPromise).then(convertNativeToDart_Dictionary);
139119

140120
// Workaround for tags like <cite> that lack their own Element subclass --
141121
// Dart issue 1990.

0 commit comments

Comments
 (0)