Updated to expose APIs promiseToFuture calls for users.
Fixes #33226 R=kevmoo@google.com, sra@google.com Change-Id: I04995b33e9445d0fcef8d874315f14fd272018d1 Reviewed-on: https://dart-review.googlesource.com/57482 Commit-Queue: Terry Lucas <terry@google.com> Reviewed-by: Stephen Adams <sra@google.com>
This commit is contained in:
committed by
commit-bot@chromium.org
parent
8cd33751c6
commit
5ec940a2e0
@@ -102,40 +102,28 @@ HtmlDocument get document =>
|
||||
JS('returns:HtmlDocument;depends:none;effects:none;gvn:true', 'document');
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future.
|
||||
Future<T> promiseToFuture<T>(thePromise) {
|
||||
Future<T> promiseToFuture<T>(jsPromise) {
|
||||
var completer = new Completer<T>();
|
||||
|
||||
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future that returns a MapLike (Class with Map mixin).
|
||||
Future promiseToFutureMap(thePromise) {
|
||||
var completer = new Completer();
|
||||
|
||||
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future that returns a Dictionary as a Dart Map.
|
||||
Future<Map> promiseToFutureDictionary(thePromise) {
|
||||
var completer = new Completer<Map>();
|
||||
// Supoort to convert JS Promise to a Dart Future<Map<String, dynamic>>. Each property of the JS
|
||||
// object is added to the Map as a key of type String with a value of type dynamic.
|
||||
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) {
|
||||
var completer = new Completer<Map<String, dynamic>>();
|
||||
|
||||
var thenSuccessCode = (promiseValue) =>
|
||||
completer.complete(convertNativeToDart_Dictionary(promiseValue));
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
@@ -23376,8 +23364,8 @@ class ImageCapture extends Interceptor {
|
||||
@DomName('ImageCapture.getPhotoSettings')
|
||||
@DocsEditable()
|
||||
@Experimental() // untriaged
|
||||
Future<Map> getPhotoSettings() =>
|
||||
promiseToFutureDictionary(JS("", "#.getPhotoSettings()", this));
|
||||
Future<Map<String, dynamic>> getPhotoSettings() =>
|
||||
promiseToFutureAsMap(JS("", "#.getPhotoSettings()", this));
|
||||
|
||||
@DomName('ImageCapture.grabFrame')
|
||||
@DocsEditable()
|
||||
@@ -28253,8 +28241,8 @@ class NavigationPreloadManager extends Interceptor {
|
||||
@DomName('NavigationPreloadManager.getState')
|
||||
@DocsEditable()
|
||||
@Experimental() // untriaged
|
||||
Future<Map> getState() =>
|
||||
promiseToFutureDictionary(JS("", "#.getState()", this));
|
||||
Future<Map<String, dynamic>> getState() =>
|
||||
promiseToFutureAsMap(JS("", "#.getState()", this));
|
||||
}
|
||||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
@@ -31582,8 +31570,8 @@ class PaymentInstruments extends Interceptor {
|
||||
@DomName('PaymentInstruments.get')
|
||||
@DocsEditable()
|
||||
@Experimental() // untriaged
|
||||
Future<Map> get(String instrumentKey) =>
|
||||
promiseToFutureDictionary(JS("", "#.get(#)", this, instrumentKey));
|
||||
Future<Map<String, dynamic>> get(String instrumentKey) =>
|
||||
promiseToFutureAsMap(JS("", "#.get(#)", this, instrumentKey));
|
||||
|
||||
@DomName('PaymentInstruments.has')
|
||||
@DocsEditable()
|
||||
@@ -34806,7 +34794,7 @@ class RtcPeerConnection extends EventTarget {
|
||||
|
||||
@DomName('RTCPeerConnection.getStats')
|
||||
@DocsEditable()
|
||||
Future getStats() => promiseToFutureMap(JS("", "#.getStats()", this));
|
||||
Future getStats() => promiseToFuture<dynamic>(JS("", "#.getStats()", this));
|
||||
|
||||
@DomName('RTCPeerConnection.removeStream')
|
||||
@DocsEditable()
|
||||
@@ -37903,8 +37891,8 @@ class StorageManager extends Interceptor {
|
||||
@DomName('StorageManager.estimate')
|
||||
@DocsEditable()
|
||||
@Experimental() // untriaged
|
||||
Future<Map> estimate() =>
|
||||
promiseToFutureDictionary(JS("", "#.estimate()", this));
|
||||
Future<Map<String, dynamic>> estimate() =>
|
||||
promiseToFutureAsMap(JS("", "#.estimate()", this));
|
||||
|
||||
@DomName('StorageManager.persist')
|
||||
@DocsEditable()
|
||||
|
||||
@@ -2,9 +2,9 @@ part of html_common;
|
||||
|
||||
/// Converts a JavaScript object with properties into a Dart Map.
|
||||
/// Not suitable for nested objects.
|
||||
Map convertNativeToDart_Dictionary(object) {
|
||||
Map<String, dynamic> convertNativeToDart_Dictionary(object) {
|
||||
if (object == null) return null;
|
||||
var dict = {};
|
||||
var dict = <String, dynamic>{};
|
||||
var keys = JS('JSExtendableArray', 'Object.getOwnPropertyNames(#)', object);
|
||||
for (final key in keys) {
|
||||
dict[key] = JS('var', '#[#]', object, key);
|
||||
|
||||
@@ -1293,12 +1293,12 @@ class Dart2JSBackend(HtmlDartGenerator):
|
||||
promiseCall = 'promiseToFuture'
|
||||
if promiseFound is not(None):
|
||||
if 'maplike' in promiseFound:
|
||||
promiseCall = 'promiseToFutureMap'
|
||||
promiseCall = 'promiseToFuture<dynamic>'
|
||||
promiseType = 'Future'
|
||||
elif promiseFound['type'] == 'dictionary':
|
||||
# It's a dictionary so return as a Map.
|
||||
promiseCall = 'promiseToFutureDictionary'
|
||||
promiseType = 'Future<Map>'
|
||||
promiseCall = 'promiseToFutureAsMap'
|
||||
promiseType = 'Future<Map<String, dynamic>>'
|
||||
else:
|
||||
paramType = promiseFound['type']
|
||||
promiseCall = 'promiseToFuture<%s>' % paramType
|
||||
|
||||
@@ -122,39 +122,28 @@ HtmlDocument get document =>
|
||||
JS('returns:HtmlDocument;depends:none;effects:none;gvn:true', 'document');
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future.
|
||||
Future<T> promiseToFuture<T>(thePromise) {
|
||||
Future<T> promiseToFuture<T>(jsPromise) {
|
||||
var completer = new Completer<T>();
|
||||
|
||||
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1), convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future that returns a MapLike (Class with Map mixin).
|
||||
Future promiseToFutureMap(thePromise) {
|
||||
var completer = new Completer();
|
||||
|
||||
var thenSuccessCode = (promiseValue) => completer.complete(promiseValue);
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
// Supoort to convert JS Promise to a Dart Future that returns a Dictionary as a Dart Map.
|
||||
Future<Map> promiseToFutureDictionary(thePromise) {
|
||||
var completer = new Completer<Map>();
|
||||
// Supoort to convert JS Promise to a Dart Future<Map<String, dynamic>>. Each property of the JS
|
||||
// object is added to the Map as a key of type String with a value of type dynamic.
|
||||
Future<Map<String, dynamic>> promiseToFutureAsMap(jsPromise) {
|
||||
var completer = new Completer<Map<String, dynamic>>();
|
||||
|
||||
var thenSuccessCode = (promiseValue) =>
|
||||
completer.complete(convertNativeToDart_Dictionary(promiseValue));
|
||||
var thenErrorCode = (promiseError) => completer.completeError(promiseError);
|
||||
|
||||
JS("", "#.then(#, #)", thePromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
JS("", "#.then(#, #)", jsPromise, convertDartClosureToJS(thenSuccessCode, 1),
|
||||
convertDartClosureToJS(thenErrorCode, 1));
|
||||
|
||||
return completer.future;
|
||||
|
||||
Reference in New Issue
Block a user