@@ -37,13 +37,17 @@ var __importStar = (this && this.__importStar) || (function () {
3737 return result ;
3838 } ;
3939} ) ( ) ;
40+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
41+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
42+ } ;
4043Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
4144exports . EventEngine = void 0 ;
4245const receiving_1 = require ( "./states/receiving" ) ;
4346const dispatcher_1 = require ( "./dispatcher" ) ;
4447const utils_1 = require ( "../core/utils" ) ;
4548const unsubscribed_1 = require ( "./states/unsubscribed" ) ;
4649const core_1 = require ( "./core" ) ;
50+ const categories_1 = __importDefault ( require ( "../core/constants/categories" ) ) ;
4751const utils = __importStar ( require ( "../core/utils" ) ) ;
4852const events = __importStar ( require ( "./events" ) ) ;
4953/**
@@ -88,6 +92,11 @@ class EventEngine {
8892 return ( 0 , utils_1 . subscriptionTimetokenFromReference ) ( currentTimetoken , referenceTimetoken !== null && referenceTimetoken !== void 0 ? referenceTimetoken : '0' ) ;
8993 }
9094 subscribe ( { channels, channelGroups, timetoken, withPresence, } ) {
95+ var _a ;
96+ // check if the channels and groups are already subscribed
97+ const hasNewChannels = channels === null || channels === void 0 ? void 0 : channels . some ( ( channel ) => ! this . channels . includes ( channel ) ) ;
98+ const hasNewGroups = channelGroups === null || channelGroups === void 0 ? void 0 : channelGroups . some ( ( group ) => ! this . groups . includes ( group ) ) ;
99+ const hasNewSubscriptions = hasNewChannels || hasNewGroups ;
91100 this . channels = [ ...this . channels , ...( channels !== null && channels !== void 0 ? channels : [ ] ) ] ;
92101 this . groups = [ ...this . groups , ...( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ] ;
93102 if ( withPresence ) {
@@ -98,7 +107,29 @@ class EventEngine {
98107 this . engine . transition ( events . restore ( Array . from ( new Set ( [ ...this . channels , ...( channels !== null && channels !== void 0 ? channels : [ ] ) ] ) ) , Array . from ( new Set ( [ ...this . groups , ...( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ] ) ) , timetoken ) ) ;
99108 }
100109 else {
101- this . engine . transition ( events . subscriptionChange ( Array . from ( new Set ( [ ...this . channels , ...( channels !== null && channels !== void 0 ? channels : [ ] ) ] ) ) , Array . from ( new Set ( [ ...this . groups , ...( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ] ) ) ) ) ;
110+ if ( hasNewSubscriptions ) {
111+ this . engine . transition ( events . subscriptionChange ( Array . from ( new Set ( [ ...this . channels , ...( channels !== null && channels !== void 0 ? channels : [ ] ) ] ) ) , Array . from ( new Set ( [ ...this . groups , ...( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ] ) ) ) ) ;
112+ }
113+ else {
114+ this . dependencies . config
115+ . logger ( )
116+ . debug ( 'EventEngine' , 'Skipping state transition - all channels/groups already subscribed. Emitting SubscriptionChanged event.' ) ;
117+ // Get current timetoken from state context
118+ const currentState = this . engine . currentState ;
119+ const currentContext = this . engine . currentContext ;
120+ let currentTimetoken = '0' ;
121+ if ( ( currentState === null || currentState === void 0 ? void 0 : currentState . label ) === receiving_1 . ReceivingState . label && currentContext ) {
122+ const receivingContext = currentContext ;
123+ currentTimetoken = ( _a = receivingContext . cursor ) === null || _a === void 0 ? void 0 : _a . timetoken ;
124+ }
125+ // Manually emit SubscriptionChanged status event
126+ this . dependencies . emitStatus ( {
127+ category : categories_1 . default . PNSubscriptionChangedCategory ,
128+ affectedChannels : Array . from ( new Set ( this . channels ) ) ,
129+ affectedChannelGroups : Array . from ( new Set ( this . groups ) ) ,
130+ currentTimetoken,
131+ } ) ;
132+ }
102133 }
103134 if ( this . dependencies . join ) {
104135 this . dependencies . join ( {
0 commit comments