Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from 1 commit
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
Prev Previous commit
Introduced _futurizeWithError
  • Loading branch information
gaaclarke committed Oct 12, 2023
commit 7c89a87779e85e37ba5a6839623266b50f002ba6
51 changes: 40 additions & 11 deletions lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1936,18 +1936,15 @@ base class _Image extends NativeFieldWrapperClass1 {
external int get height;

Future<ByteData?> toByteData({ImageByteFormat format = ImageByteFormat.rawRgba}) {
final Completer<ByteData?> completer = Completer<ByteData?>();
final String? syncError = _toByteData(format.index, (Uint8List? encoded, String? error) {
if (encoded != null) {
completer.complete(encoded.buffer.asByteData());
} else {
completer.completeError(Exception(error ?? 'Unknown exception.'));
}
return _futurizeWithError((_CallbackWithError<ByteData?> callback) {
return _toByteData(format.index, (Uint8List? encoded, String? error) {
if (error == null && encoded != null) {
callback(encoded.buffer.asByteData(), null);
} else {
callback(null, error);
}
});
});
if (syncError != null) {
completer.completeError(Exception(syncError));
}
return completer.future;
}

/// Returns an error message on failure, null on success.
Expand Down Expand Up @@ -6891,12 +6888,19 @@ base class _NativeImageDescriptor extends NativeFieldWrapperClass1 implements Im
/// Generic callback signature, used by [_futurize].
typedef _Callback<T> = void Function(T result);

/// Generic callback signature, used by [_futurizeWithError].
typedef _CallbackWithError<T> = void Function(T result, String? error);

/// Signature for a method that receives a [_Callback].
///
/// Return value should be null on success, and a string error message on
/// failure.
typedef _Callbacker<T> = String? Function(_Callback<T?> callback);

/// Signature for a method that receives a [_CallbackWithError].
/// See also: [_Callbacker]
typedef _CallbackerWithError<T> = String? Function(_CallbackWithError<T?> callback);

// Converts a method that receives a value-returning callback to a method that
// returns a Future.
//
Expand Down Expand Up @@ -6948,6 +6952,31 @@ Future<T> _futurize<T>(_Callbacker<T> callbacker) {
return completer.future;
}

/// A variant of `_futurize` that can communicate specific errors.
Future<T> _futurizeWithError<T>(_CallbackerWithError<T> callbacker) {
final Completer<T> completer = Completer<T>.sync();
// If the callback synchronously throws an error, then synchronously
// rethrow that error instead of adding it to the completer. This
// prevents the Zone from receiving an uncaught exception.
bool isSync = true;
final String? error = callbacker((T? t, String? error) {
if (t != null) {
completer.complete(t);
} else {
if (isSync) {
throw Exception(error ?? 'operation failed');
} else {
completer.completeError(Exception(error ?? 'operation failed'));
}
}
});
isSync = false;
if (error != null) {
throw Exception(error);
}
return completer.future;
}

/// An exception thrown by [Canvas.drawImage] and related methods when drawing
/// an [Image] created via [Picture.toImageSync] that is in an invalid state.
///
Expand Down