From 62af37028902d298e3fc091cc5c1d3acba965ba1 Mon Sep 17 00:00:00 2001 From: "Vladimir E. Koltunov" Date: Fri, 24 Jan 2025 23:45:01 +0300 Subject: [PATCH 1/9] Fix type check and cast in onError --- pkgs/async/lib/src/subscription_stream.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkgs/async/lib/src/subscription_stream.dart b/pkgs/async/lib/src/subscription_stream.dart index 9ce4942c3..db750a4bf 100644 --- a/pkgs/async/lib/src/subscription_stream.dart +++ b/pkgs/async/lib/src/subscription_stream.dart @@ -76,10 +76,10 @@ class _CancelOnErrorSubscriptionWrapper super.onError((Object error, StackTrace stackTrace) { // Wait for the cancel to complete before sending the error event. super.cancel().whenComplete(() { - if (handleError is ZoneBinaryCallback) { - handleError(error, stackTrace); - } else if (handleError != null) { - (handleError as ZoneUnaryCallback)(error); + if (handleError is ZoneBinaryCallback || handleError is ZoneBinaryCallback) { + handleError?.call(error, stackTrace); + } else if (handleError is ZoneUnaryCallback || handleError is ZoneUnaryCallback) { + handleError?.call(error); } }); }); From 894c1eb9f13ad8ce9b758a90feedc481ed247cd8 Mon Sep 17 00:00:00 2001 From: "Vladimir E. Koltunov" Date: Fri, 24 Jan 2025 23:48:16 +0300 Subject: [PATCH 2/9] Update CHANGELOG.md --- pkgs/async/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/async/CHANGELOG.md b/pkgs/async/CHANGELOG.md index 06ac7d11b..ca81027fa 100644 --- a/pkgs/async/CHANGELOG.md +++ b/pkgs/async/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.13.0 + +- Fix type check and cast in SubscriptionStream's cancelOnError wrapper + ## 2.12.0 - Require Dart 3.4. From 468b80fb7022c13600998d242d37676130b2d25d Mon Sep 17 00:00:00 2001 From: "Vladimir E. Koltunov" Date: Fri, 24 Jan 2025 23:48:36 +0300 Subject: [PATCH 3/9] Update pubspec.yaml --- pkgs/async/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/async/pubspec.yaml b/pkgs/async/pubspec.yaml index be05c8553..db2d84f4c 100644 --- a/pkgs/async/pubspec.yaml +++ b/pkgs/async/pubspec.yaml @@ -1,5 +1,5 @@ name: async -version: 2.12.0 +version: 2.13.0 description: Utility functions and classes related to the 'dart:async' library. repository: https://github.com/dart-lang/core/tree/main/pkgs/async issue_tracker: https://github.com/dart-lang/core/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aasync From ddb2d96a1cfb48d1b149a48902dab63bcb89448f Mon Sep 17 00:00:00 2001 From: "vladimir.koltunov" Date: Mon, 27 Jan 2025 13:06:49 +0300 Subject: [PATCH 4/9] test added. formatted. --- pkgs/async/lib/src/subscription_stream.dart | 16 +++-- pkgs/async/test/subscription_stream_test.dart | 72 +++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/pkgs/async/lib/src/subscription_stream.dart b/pkgs/async/lib/src/subscription_stream.dart index db750a4bf..af3cb94da 100644 --- a/pkgs/async/lib/src/subscription_stream.dart +++ b/pkgs/async/lib/src/subscription_stream.dart @@ -76,12 +76,20 @@ class _CancelOnErrorSubscriptionWrapper super.onError((Object error, StackTrace stackTrace) { // Wait for the cancel to complete before sending the error event. super.cancel().whenComplete(() { - if (handleError is ZoneBinaryCallback || handleError is ZoneBinaryCallback) { - handleError?.call(error, stackTrace); - } else if (handleError is ZoneUnaryCallback || handleError is ZoneUnaryCallback) { - handleError?.call(error); + if (handleError is ZoneBinaryCallback) { + handleError(error, stackTrace); + } else if (handleError is ZoneBinaryCallback) { + handleError(error, stackTrace); + } else if (handleError is ZoneUnaryCallback) { + handleError(error); + } else if (handleError is ZoneUnaryCallback) { + handleError(error); } }); }); } } + +abstract class BinaryWrapper { + void binaryCall(T1 t1, T2 t2); +} diff --git a/pkgs/async/test/subscription_stream_test.dart b/pkgs/async/test/subscription_stream_test.dart index 0d245f388..8abe9b93f 100644 --- a/pkgs/async/test/subscription_stream_test.dart +++ b/pkgs/async/test/subscription_stream_test.dart @@ -148,8 +148,80 @@ void main() { }); } }); + + group('subscriptionStream error callback', () { + test('- binary typed', () async { + var completer = Completer(); + var stream = createErrorStream(); + var sourceSubscription = stream.listen(null, cancelOnError: true); + var subscriptionStream = SubscriptionStream(sourceSubscription); + + void f(Object error, StackTrace stackTrace) { + completer.complete(); + } + + subscriptionStream.listen((_) {}, + onError: f, + onDone: () => throw 'should not happen', + cancelOnError: true); + await completer.future; + await flushMicrotasks(); + }); + + test('- binary dynamic', () async { + var completer = Completer(); + var stream = createErrorStream(); + var sourceSubscription = stream.listen(null, cancelOnError: true); + var subscriptionStream = SubscriptionStream(sourceSubscription); + + subscriptionStream.listen((_) {}, + onError: (error, stackTrace) { + completer.complete(); + }, + onDone: () => throw 'should not happen', + cancelOnError: true); + await completer.future; + await flushMicrotasks(); + }); + + test('- unary typed', () async { + var completer = Completer(); + var stream = createErrorStream(); + var sourceSubscription = stream.listen(null, cancelOnError: true); + var subscriptionStream = SubscriptionStream(sourceSubscription); + + void f(Object error) { + completer.complete(); + } + + subscriptionStream.listen((_) {}, + onError: f, + onDone: () => throw 'should not happen', + cancelOnError: true); + await completer.future; + await flushMicrotasks(); + }); + + test('- unary dynamic', () async { + var completer = Completer(); + var stream = createErrorStream(); + var sourceSubscription = stream.listen(null, cancelOnError: true); + var subscriptionStream = SubscriptionStream(sourceSubscription); + + subscriptionStream.listen((_) {}, + onError: (error) { + completer.complete(); + }, + onDone: () => throw 'should not happen', + cancelOnError: true); + await completer.future; + await flushMicrotasks(); + }); + }); } +typedef BinaryFunc = void Function(Object s, StackTrace tr); + Stream createStream() async* { yield 1; await flushMicrotasks(); From f84fab1d44dce518429fae4672d7f81c74bb651f Mon Sep 17 00:00:00 2001 From: "vladimir.koltunov" Date: Tue, 28 Jan 2025 06:51:16 +0300 Subject: [PATCH 5/9] test descriptions changed --- pkgs/async/test/subscription_stream_test.dart | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkgs/async/test/subscription_stream_test.dart b/pkgs/async/test/subscription_stream_test.dart index 8abe9b93f..acdbdf139 100644 --- a/pkgs/async/test/subscription_stream_test.dart +++ b/pkgs/async/test/subscription_stream_test.dart @@ -150,7 +150,7 @@ void main() { }); group('subscriptionStream error callback', () { - test('- binary typed', () async { + test('binary typed', () async { var completer = Completer(); var stream = createErrorStream(); var sourceSubscription = stream.listen(null, cancelOnError: true); @@ -168,7 +168,7 @@ void main() { await flushMicrotasks(); }); - test('- binary dynamic', () async { + test('binary dynamic', () async { var completer = Completer(); var stream = createErrorStream(); var sourceSubscription = stream.listen(null, cancelOnError: true); @@ -184,7 +184,7 @@ void main() { await flushMicrotasks(); }); - test('- unary typed', () async { + test('unary typed', () async { var completer = Completer(); var stream = createErrorStream(); var sourceSubscription = stream.listen(null, cancelOnError: true); @@ -202,7 +202,7 @@ void main() { await flushMicrotasks(); }); - test('- unary dynamic', () async { + test('unary dynamic', () async { var completer = Completer(); var stream = createErrorStream(); var sourceSubscription = stream.listen(null, cancelOnError: true); @@ -220,8 +220,6 @@ void main() { }); } -typedef BinaryFunc = void Function(Object s, StackTrace tr); - Stream createStream() async* { yield 1; await flushMicrotasks(); From d055407ffaac87ae1062e5926a62669deac823e6 Mon Sep 17 00:00:00 2001 From: "vladimir.koltunov" Date: Tue, 28 Jan 2025 07:38:14 +0300 Subject: [PATCH 6/9] more generic implementation --- pkgs/async/lib/src/subscription_stream.dart | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkgs/async/lib/src/subscription_stream.dart b/pkgs/async/lib/src/subscription_stream.dart index af3cb94da..96a7a2b18 100644 --- a/pkgs/async/lib/src/subscription_stream.dart +++ b/pkgs/async/lib/src/subscription_stream.dart @@ -76,13 +76,9 @@ class _CancelOnErrorSubscriptionWrapper super.onError((Object error, StackTrace stackTrace) { // Wait for the cancel to complete before sending the error event. super.cancel().whenComplete(() { - if (handleError is ZoneBinaryCallback) { + if (handleError is dynamic Function(Object, StackTrace)) { handleError(error, stackTrace); - } else if (handleError is ZoneBinaryCallback) { - handleError(error, stackTrace); - } else if (handleError is ZoneUnaryCallback) { - handleError(error); - } else if (handleError is ZoneUnaryCallback) { + } else if (handleError is dynamic Function(Object)) { handleError(error); } }); From 20679acdd67f33792bccb2fe64be9b281b6f214c Mon Sep 17 00:00:00 2001 From: "vladimir.koltunov" Date: Tue, 28 Jan 2025 11:43:19 +0300 Subject: [PATCH 7/9] removed unused code --- pkgs/async/lib/src/subscription_stream.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkgs/async/lib/src/subscription_stream.dart b/pkgs/async/lib/src/subscription_stream.dart index 96a7a2b18..e6630721a 100644 --- a/pkgs/async/lib/src/subscription_stream.dart +++ b/pkgs/async/lib/src/subscription_stream.dart @@ -85,7 +85,3 @@ class _CancelOnErrorSubscriptionWrapper }); } } - -abstract class BinaryWrapper { - void binaryCall(T1 t1, T2 t2); -} From a546211ae72ee0eb2c05337ffa0366358fa49bfd Mon Sep 17 00:00:00 2001 From: "vladimir.koltunov" Date: Tue, 28 Jan 2025 22:29:07 +0300 Subject: [PATCH 8/9] ErrorResult::handler fix --- pkgs/async/lib/src/result/error.dart | 9 +++++++-- pkgs/async/test/result/result_test.dart | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkgs/async/lib/src/result/error.dart b/pkgs/async/lib/src/result/error.dart index 48f71b1d1..8c694b792 100644 --- a/pkgs/async/lib/src/result/error.dart +++ b/pkgs/async/lib/src/result/error.dart @@ -47,10 +47,15 @@ class ErrorResult implements Result { /// has to be a function expecting only one argument, which will be called /// with only the error. void handle(Function errorHandler) { - if (errorHandler is ZoneBinaryCallback) { + if (errorHandler is dynamic Function(Object, StackTrace)) { errorHandler(error, stackTrace); + } else if (errorHandler is dynamic Function(Object)) { + errorHandler(error); } else { - (errorHandler as ZoneUnaryCallback)(error); + throw ArgumentError( + 'is nor Function(Object, StackTrace) neither Function(Object)', + 'errorHandler', + ); } } diff --git a/pkgs/async/test/result/result_test.dart b/pkgs/async/test/result/result_test.dart index 13a5d5366..96620463f 100644 --- a/pkgs/async/test/result/result_test.dart +++ b/pkgs/async/test/result/result_test.dart @@ -281,6 +281,29 @@ void main() { expect(called, isTrue); }); + test('handle typed binary', () { + final result = ErrorResult('error', stack); + var called = false; + void f(Object error, StackTrace stackTrace) { + called = true; + } + + result.handle(f); + expect(called, isTrue); + }); + + test('handle typed unary', () { + final result = ErrorResult('error', stack); + var called = false; + + void f(Object error) { + called = true; + } + + result.handle(f); + expect(called, isTrue); + }); + test('handle unary and binary', () { var result = ErrorResult('error', stack); var called = false; From d79d064538e3df9586b559fe598146e2b763dc46 Mon Sep 17 00:00:00 2001 From: "Vladimir E. Koltunov" Date: Tue, 28 Jan 2025 22:30:40 +0300 Subject: [PATCH 9/9] Update pkgs/async/lib/src/result/error.dart Co-authored-by: Kevin Moore --- pkgs/async/lib/src/result/error.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/async/lib/src/result/error.dart b/pkgs/async/lib/src/result/error.dart index 8c694b792..924db7a09 100644 --- a/pkgs/async/lib/src/result/error.dart +++ b/pkgs/async/lib/src/result/error.dart @@ -53,7 +53,7 @@ class ErrorResult implements Result { errorHandler(error); } else { throw ArgumentError( - 'is nor Function(Object, StackTrace) neither Function(Object)', + 'is neither Function(Object, StackTrace) nor Function(Object)', 'errorHandler', ); }