From 7cca9f207aac3d0e9ceb51ffec4bf239eed69c3b Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 20 Sep 2024 20:00:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Construct=20listener=20grace?= =?UTF-8?q?fully?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/fluwx.dart | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/src/fluwx.dart b/lib/src/fluwx.dart index 5dd35841..2645aea2 100644 --- a/lib/src/fluwx.dart +++ b/lib/src/fluwx.dart @@ -25,20 +25,21 @@ import 'method_channel/fluwx_platform_interface.dart'; import 'response/wechat_response.dart'; class Fluwx { - late final WeakReference - responseListener; + Fluwx() { + _responseSubscription = FluwxPlatform.instance.responseEventHandler.listen( + _responseEventListener, + onDone: () { + _responseSubscription?.cancel(); + }, + ); + } - final List _responseListeners = []; + final _responseListeners = []; + StreamSubscription? _responseSubscription; - Fluwx() { - responseListener = WeakReference((event) { - for (var listener in _responseListeners) { - listener(event); - } - }); - final target = responseListener.target; - if (target != null) { - FluwxPlatform.instance.responseEventHandler.listen(target); + void _responseEventListener(WeChatResponse event) { + for (final listener in _responseListeners.toList()) { + listener(event); } } From 52c0ebd6ef09da2df6f9ab05465f212f78fe6cfb Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 20 Sep 2024 20:01:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/fluwx.dart | 6 +++--- lib/src/foundation/cancelable.dart | 11 +++++------ lib/src/response/wechat_response.dart | 1 + 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/src/fluwx.dart b/lib/src/fluwx.dart index 2645aea2..74355718 100644 --- a/lib/src/fluwx.dart +++ b/lib/src/fluwx.dart @@ -129,17 +129,17 @@ class Fluwx { /// Unsubscribe responses from WeChat @Deprecated("use [removeSubscriber] instead") - unsubscribeResponse(WeChatResponseSubscriber listener) { + void unsubscribeResponse(WeChatResponseSubscriber listener) { removeSubscriber(listener); } /// remove your subscriber from WeChat - removeSubscriber(WeChatResponseSubscriber listener) { + void removeSubscriber(WeChatResponseSubscriber listener) { _responseListeners.remove(listener); } /// remove all existing - clearSubscribers() { + void clearSubscribers() { _responseListeners.clear(); } } diff --git a/lib/src/foundation/cancelable.dart b/lib/src/foundation/cancelable.dart index 2a3522a9..003a90e8 100644 --- a/lib/src/foundation/cancelable.dart +++ b/lib/src/foundation/cancelable.dart @@ -17,20 +17,19 @@ * the License. */ -import '../response/wechat_response.dart'; +import 'package:flutter/foundation.dart'; -typedef WeChatResponseSubscriber = Function(WeChatResponse response); mixin FluwxCancelable { - cancel(); + void cancel(); } class FluwxCancelableImpl implements FluwxCancelable { - final Function onCancel; + const FluwxCancelableImpl({required this.onCancel}); - FluwxCancelableImpl({required this.onCancel}); + final VoidCallback onCancel; @override - cancel() { + void cancel() { onCancel(); } } diff --git a/lib/src/response/wechat_response.dart b/lib/src/response/wechat_response.dart index 67ce61a9..13fbc7db 100644 --- a/lib/src/response/wechat_response.dart +++ b/lib/src/response/wechat_response.dart @@ -22,6 +22,7 @@ import 'dart:typed_data'; const String _errCode = 'errCode'; const String _errStr = 'errStr'; +typedef WeChatResponseSubscriber = void Function(WeChatResponse response); typedef _WeChatResponseInvoker = WeChatResponse Function(Map argument); Map _nameAndResponseMapper = {