Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Construct listener gracefully #639

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading