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

Fix reconnecting issues #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const val PRESENCE_PREFIX = "presence-"

class PusherService : MChannel {
private var _pusherInstance: Pusher? = null
private var _connectionListener: ConnectionListener? = ConnectionListener()

companion object {
var enableLogging: Boolean = false
Expand Down Expand Up @@ -120,7 +121,7 @@ class PusherService : MChannel {
}

private fun connect(result: Result) {
_pusherInstance?.connect(ConnectionListener(), ConnectionState.ALL)
_pusherInstance?.connect(_connectionListener, ConnectionState.ALL)
result.success(null)
}

Expand Down Expand Up @@ -174,7 +175,7 @@ class PusherService : MChannel {
private fun unsubscribe(call: MethodCall, result: Result) {
try {
val src = call.arguments as Map<String, Any>
val args = JSONObject(src);
val args = JSONObject(src)
val channelName = args.getString("channelName")

_pusherInstance?.unsubscribe(channelName)
Expand Down
42 changes: 21 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0-nullsafety.1"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.3"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0-nullsafety.3"
version: "1.15.0"
cupertino_icons:
dependency: "direct main"
description:
Expand All @@ -56,7 +56,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -73,28 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10-nullsafety.1"
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0-nullsafety.1"
version: "1.8.0"
pusher_client:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.1.3"
version: "1.1.4"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -106,56 +106,56 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0-nullsafety.2"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0-nullsafety.1"
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19-nullsafety.2"
version: "0.3.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.3"
version: "2.1.0"
sdks:
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.20.0 <2.0.0"
dart: ">=2.13.0 <3.0.0"
flutter: ">=1.20.0"
7 changes: 4 additions & 3 deletions lib/src/contracts/stream_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import 'package:flutter/services.dart';
abstract class StreamHandler {
static const EventChannel _eventStream =
const EventChannel('com.github.chinloyal/pusher_client_stream');
StreamSubscription _eventStreamSubscription;
StreamSubscription? _eventStreamSubscription;

static Map<String, dynamic Function(dynamic)> _listeners = {};

/// Add a listener to the event channel stream for pusher,
/// any class that extends [StreamHandler] should use this method.
void registerListener(String classId, dynamic Function(dynamic) method) {
StreamHandler._listeners[classId] = method;
_eventStreamSubscription?.cancel();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this line are you able to receive connection state change events after binding to an event? Because whenever you bind it calls registerListener so wouldn't this line cancel the stream causing all classes using the event channel to not receive any events?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, connection state changes still work after binding because Channel and PusherClient extend this abstract class on their own. bind() and connect() only re-subscribes their own instance.


StreamHandler._listeners[classId] = method;
_eventStreamSubscription =
_eventStream.receiveBroadcastStream().listen(_eventHandler);
}

/// This method will close the entire event channel stream
/// which is why it should only be used by [PusherClient]
void cancelEventChannelStream() {
_eventStreamSubscription.cancel();
_eventStreamSubscription?.cancel();
}

void _eventHandler(event) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/models/connection_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ part 'connection_error.g.dart';

class ConnectionError {
/// A message indicating the cause of the error.
final String message;
final String? message;

/// The error code for the message. Can be null.
final String code;
final String? code;

/// The exception that was thrown, if any. Can be null.
final String exception;
final String? exception;

ConnectionError({this.message, this.code, this.exception});

Expand Down
6 changes: 3 additions & 3 deletions lib/src/models/connection_error.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ part of 'connection_error.dart';

ConnectionError _$ConnectionErrorFromJson(Map<String, dynamic> json) {
return ConnectionError(
message: json['message'] as String,
code: json['code'] as String,
exception: json['exception'] as String,
message: json['message'] as String?,
code: json['code'] as String?,
exception: json['exception'] as String?,
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/connection_state_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ part 'connection_state_change.g.dart';
class ConnectionStateChange {
/// The current connection state. The state the connection has transitioned
/// to.
final String currentState;
final String? currentState;

/// The previous connections state. The state the connection has transitioned
/// from.
final String previousState;
final String? previousState;

ConnectionStateChange({
this.currentState,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/connection_state_change.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ part of 'connection_state_change.dart';
ConnectionStateChange _$ConnectionStateChangeFromJson(
Map<String, dynamic> json) {
return ConnectionStateChange(
currentState: json['currentState'] as String,
previousState: json['previousState'] as String,
currentState: json['currentState'] as String?,
previousState: json['previousState'] as String?,
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/models/event_stream_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:pusher_client/src/pusher/pusher_event.dart';
part 'event_stream_result.g.dart';

class EventStreamResult {
final ConnectionStateChange connectionStateChange;
final ConnectionError connectionError;
final PusherEvent pusherEvent;
final ConnectionStateChange? connectionStateChange;
final ConnectionError? connectionError;
final PusherEvent? pusherEvent;

EventStreamResult({
this.connectionStateChange,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/pusher/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import 'package:pusher_client/src/contracts/stream_handler.dart';
import 'package:pusher_client/src/models/event_stream_result.dart';
import 'package:pusher_client/src/pusher/pusher_event.dart';

typedef EventCallback = Map<String, void Function(PusherEvent?)>;

class Channel extends StreamHandler {
static const MethodChannel _mChannel =
const MethodChannel('com.github.chinloyal/pusher_client');
static const classId = 'Channel';

static Map<String, void Function(PusherEvent)> _eventCallbacks =
Map<String, void Function(PusherEvent)>();
static EventCallback _eventCallbacks = EventCallback();

final String name;

Expand All @@ -31,7 +32,7 @@ class Channel extends StreamHandler {
/// [eventName] is received on this channel.
Future<void> bind(
String eventName,
void Function(PusherEvent event) onEvent,
void Function(PusherEvent? event) onEvent,
) async {
registerListener(classId, _eventHandler);
_eventCallbacks[this.name + eventName] = onEvent;
Expand Down Expand Up @@ -80,7 +81,7 @@ class Channel extends StreamHandler {

if (result.isPusherEvent) {
var callback = _eventCallbacks[
result.pusherEvent.channelName + result.pusherEvent.eventName];
result.pusherEvent!.channelName! + result.pusherEvent!.eventName!];
if (callback != null) {
callback(result.pusherEvent);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/pusher/pusher_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ part 'pusher_auth.g.dart';

class PusherAuth {
/// The endpoint to be called when authenticating.
final String endpoint;
final String? endpoint;

/// Additional headers to be sent as part of the request.
final Map<String, String> headers;
final Map<String, String>? headers;

PusherAuth(
this.endpoint, {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/pusher/pusher_auth.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ part of 'pusher_auth.dart';

PusherAuth _$PusherAuthFromJson(Map<String, dynamic> json) {
return PusherAuth(
json['endpoint'] as String,
headers: (json['headers'] as Map<String, dynamic>)?.map(
json['endpoint'] as String?,
headers: (json['headers'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(k, e as String),
),
);
Expand Down
Loading