Skip to content

Commit

Permalink
Merge pull request #639 from OpenFlutter/fix/list
Browse files Browse the repository at this point in the history
🐛 Construct listener gracefully
  • Loading branch information
JarvanMo authored Sep 21, 2024
2 parents 72a628b + 52c0ebd commit e273d76
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
31 changes: 16 additions & 15 deletions lib/src/fluwx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ import 'method_channel/fluwx_platform_interface.dart';
import 'response/wechat_response.dart';

class Fluwx {
late final WeakReference<void Function(WeChatResponse event)>
responseListener;
Fluwx() {
_responseSubscription = FluwxPlatform.instance.responseEventHandler.listen(
_responseEventListener,
onDone: () {
_responseSubscription?.cancel();
},
);
}

final List<WeChatResponseSubscriber> _responseListeners = [];
final _responseListeners = <WeChatResponseSubscriber>[];
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);
}
}

Expand Down Expand Up @@ -128,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();
}
}
11 changes: 5 additions & 6 deletions lib/src/foundation/cancelable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions lib/src/response/wechat_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, _WeChatResponseInvoker> _nameAndResponseMapper = {
Expand Down

0 comments on commit e273d76

Please sign in to comment.