From 8c5db3438b95849b9e90159ac243715df6acf131 Mon Sep 17 00:00:00 2001 From: Plague Fox Date: Thu, 3 Aug 2023 16:36:20 +0400 Subject: [PATCH] On push event --- lib/src/client/centrifuge.dart | 3 +++ lib/src/client/observer.dart | 4 ++++ lib/src/model/channel_push.dart | 5 +++++ lib/src/model/event.dart | 3 +++ 4 files changed, 15 insertions(+) diff --git a/lib/src/client/centrifuge.dart b/lib/src/client/centrifuge.dart index 6a89c88..b487be4 100644 --- a/lib/src/client/centrifuge.dart +++ b/lib/src/client/centrifuge.dart @@ -97,6 +97,7 @@ abstract base class CentrifugeBase implements ICentrifuge { @protected @mustCallSuper void _initCentrifuge() { + logger.fine('Centrifuge client initialized'); Centrifuge.observer?.onCreate(this); } @@ -124,6 +125,7 @@ abstract base class CentrifugeBase implements ICentrifuge { @mustCallSuper Future close() async { await _transport.close(); + logger.fine('Centrifuge client closed'); Centrifuge.observer?.onClose(this); } } @@ -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); diff --git a/lib/src/client/observer.dart b/lib/src/client/observer.dart index 9a2f032..fb0bd89 100644 --- a/lib/src/client/observer.dart +++ b/lib/src/client/observer.dart @@ -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 { @@ -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) {} diff --git a/lib/src/model/channel_push.dart b/lib/src/model/channel_push.dart index e6f8873..d47803d 100644 --- a/lib/src/model/channel_push.dart +++ b/lib/src/model/channel_push.dart @@ -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. @@ -12,4 +13,8 @@ abstract base class CentrifugeChannelPush extends CentrifugeEvent { /// Channel final String channel; + + @override + @nonVirtual + bool get isPush => true; } diff --git a/lib/src/model/event.dart b/lib/src/model/event.dart index d8b0656..318d8a3 100644 --- a/lib/src/model/event.dart +++ b/lib/src/model/event.dart @@ -15,4 +15,7 @@ abstract base class CentrifugeEvent { /// Timestamp of event final DateTime timestamp; + + /// Whether this event is a push event. + bool get isPush; }