Skip to content

Commit

Permalink
On push event
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Aug 3, 2023
1 parent f345d1d commit 8c5db34
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/src/client/centrifuge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ abstract base class CentrifugeBase implements ICentrifuge {
@protected
@mustCallSuper
void _initCentrifuge() {
logger.fine('Centrifuge client initialized');
Centrifuge.observer?.onCreate(this);
}

Expand Down Expand Up @@ -124,6 +125,7 @@ abstract base class CentrifugeBase implements ICentrifuge {
@mustCallSuper
Future<void> close() async {
await _transport.close();
logger.fine('Centrifuge client closed');
Centrifuge.observer?.onClose(this);
}
}
Expand Down Expand Up @@ -187,6 +189,7 @@ base mixin CentrifugeEventReceiverMixin
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
void _onEvent(CentrifugeEvent event) {
Centrifuge.observer?.onEvent(this, event);
if (event is! CentrifugeChannelPush) return;
// This is a push to a channel.
_clientSubscriptionManager.onPush(event);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/client/observer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:centrifuge_dart/centrifuge.dart';
import 'package:centrifuge_dart/src/client/centrifuge_interface.dart';
import 'package:centrifuge_dart/src/model/event.dart';

/// An interface for observing the behavior of Centrifuge instances.
abstract class CentrifugeObserver {
Expand All @@ -10,6 +11,9 @@ abstract class CentrifugeObserver {
/// to [CentrifugeState$Connecting].
void onConnected(ICentrifuge client, CentrifugeState$Connected state) {}

/// Called whenever a [ICentrifuge] client receives a [CentrifugeEvent].
void onEvent(ICentrifuge client, CentrifugeEvent event) {}

/// Called whenever a [ICentrifuge] client changes its state
/// to [CentrifugeState$Disconnected].
void onDisconnected(ICentrifuge client, CentrifugeState$Disconnected state) {}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/model/channel_push.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:centrifuge_dart/src/model/event.dart';
import 'package:meta/meta.dart';

/// {@template centrifuge_channel_push}
/// Base class for all channel push events.
Expand All @@ -12,4 +13,8 @@ abstract base class CentrifugeChannelPush extends CentrifugeEvent {

/// Channel
final String channel;

@override
@nonVirtual
bool get isPush => true;
}
3 changes: 3 additions & 0 deletions lib/src/model/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ abstract base class CentrifugeEvent {

/// Timestamp of event
final DateTime timestamp;

/// Whether this event is a push event.
bool get isPush;
}

0 comments on commit 8c5db34

Please sign in to comment.