@@ -53,6 +53,7 @@ var core_1 = require("./core");
53
53
var dispatcher_1 = require ( "./dispatcher" ) ;
54
54
var events = __importStar ( require ( "./events" ) ) ;
55
55
var unsubscribed_1 = require ( "./states/unsubscribed" ) ;
56
+ var utils = __importStar ( require ( "../core/utils" ) ) ;
56
57
var EventEngine = /** @class */ ( function ( ) {
57
58
function EventEngine ( dependencies ) {
58
59
var _this = this ;
@@ -78,44 +79,47 @@ var EventEngine = /** @class */ (function () {
78
79
EventEngine . prototype . subscribe = function ( _a ) {
79
80
var _this = this ;
80
81
var channels = _a . channels , channelGroups = _a . channelGroups , timetoken = _a . timetoken , withPresence = _a . withPresence ;
81
- this . channels = Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . channels ) , false ) , __read ( ( channels !== null && channels !== void 0 ? channels : [ ] ) ) , false ) ) ) ;
82
- this . groups = Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . groups ) , false ) , __read ( ( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ) , false ) ) ) ;
82
+ this . channels = __spreadArray ( __spreadArray ( [ ] , __read ( this . channels ) , false ) , __read ( ( channels !== null && channels !== void 0 ? channels : [ ] ) ) , false ) ;
83
+ this . groups = __spreadArray ( __spreadArray ( [ ] , __read ( this . groups ) , false ) , __read ( ( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ) , false ) ;
83
84
if ( withPresence ) {
84
85
this . channels . map ( function ( c ) { return _this . channels . push ( "" . concat ( c , "-pnpres" ) ) ; } ) ;
85
86
this . groups . map ( function ( g ) { return _this . groups . push ( "" . concat ( g , "-pnpres" ) ) ; } ) ;
86
87
}
87
88
if ( timetoken ) {
88
- this . engine . transition ( events . restore ( this . channels , this . groups , timetoken ) ) ;
89
+ this . engine . transition ( events . restore ( Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . channels ) , false ) , __read ( ( channels !== null && channels !== void 0 ? channels : [ ] ) ) , false ) ) ) , Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . groups ) , false ) , __read ( ( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ) , false ) ) ) , timetoken ) ) ;
89
90
}
90
91
else {
91
- this . engine . transition ( events . subscriptionChange ( this . channels , this . groups ) ) ;
92
+ this . engine . transition ( events . subscriptionChange ( Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . channels ) , false ) , __read ( ( channels !== null && channels !== void 0 ? channels : [ ] ) ) , false ) ) ) , Array . from ( new Set ( __spreadArray ( __spreadArray ( [ ] , __read ( this . groups ) , false ) , __read ( ( channelGroups !== null && channelGroups !== void 0 ? channelGroups : [ ] ) ) , false ) ) ) ) ) ;
92
93
}
93
94
if ( this . dependencies . join ) {
94
95
this . dependencies . join ( {
95
- channels : this . channels . filter ( function ( c ) { return ! c . endsWith ( '-pnpres' ) ; } ) ,
96
- groups : this . groups . filter ( function ( g ) { return ! g . endsWith ( '-pnpres' ) ; } ) ,
96
+ channels : Array . from ( new Set ( this . channels . filter ( function ( c ) { return ! c . endsWith ( '-pnpres' ) ; } ) ) ) ,
97
+ groups : Array . from ( new Set ( this . groups . filter ( function ( g ) { return ! g . endsWith ( '-pnpres' ) ; } ) ) ) ,
97
98
} ) ;
98
99
}
99
100
} ;
100
101
EventEngine . prototype . unsubscribe = function ( _a ) {
101
102
var _this = this ;
102
- var channels = _a . channels , groups = _a . groups ;
103
- var channlesWithPres = channels === null || channels === void 0 ? void 0 : channels . slice ( 0 ) ;
104
- channels === null || channels === void 0 ? void 0 : channels . map ( function ( c ) { return channlesWithPres . push ( "" . concat ( c , "-pnpres" ) ) ; } ) ;
105
- this . channels = this . channels . filter ( function ( channel ) { return ! ( channlesWithPres === null || channlesWithPres === void 0 ? void 0 : channlesWithPres . includes ( channel ) ) ; } ) ;
106
- var groupsWithPres = groups === null || groups === void 0 ? void 0 : groups . slice ( 0 ) ;
107
- groups === null || groups === void 0 ? void 0 : groups . map ( function ( g ) { return groupsWithPres . push ( "" . concat ( g , "-pnpres" ) ) ; } ) ;
108
- this . groups = this . groups . filter ( function ( group ) { return ! ( groupsWithPres === null || groupsWithPres === void 0 ? void 0 : groupsWithPres . includes ( group ) ) ; } ) ;
109
- if ( this . dependencies . presenceState ) {
110
- channels === null || channels === void 0 ? void 0 : channels . forEach ( function ( c ) { return delete _this . dependencies . presenceState [ c ] ; } ) ;
111
- groups === null || groups === void 0 ? void 0 : groups . forEach ( function ( g ) { return delete _this . dependencies . presenceState [ g ] ; } ) ;
112
- }
113
- this . engine . transition ( events . subscriptionChange ( this . channels . slice ( 0 ) , this . groups . slice ( 0 ) ) ) ;
114
- if ( this . dependencies . leave ) {
115
- this . dependencies . leave ( {
116
- channels : channels ,
117
- groups : groups ,
118
- } ) ;
103
+ var _b = _a . channels , channels = _b === void 0 ? [ ] : _b , _c = _a . channelGroups , channelGroups = _c === void 0 ? [ ] : _c ;
104
+ var filteredChannels = utils . removeSingleOccurance ( this . channels , __spreadArray ( __spreadArray ( [ ] , __read ( channels ) , false ) , __read ( channels . map ( function ( c ) { return "" . concat ( c , "-pnpres" ) ; } ) ) , false ) ) ;
105
+ var filteredGroups = utils . removeSingleOccurance ( this . groups , __spreadArray ( __spreadArray ( [ ] , __read ( channelGroups ) , false ) , __read ( channelGroups . map ( function ( c ) { return "" . concat ( c , "-pnpres" ) ; } ) ) , false ) ) ;
106
+ if ( new Set ( this . channels ) . size !== new Set ( filteredChannels ) . size ||
107
+ new Set ( this . groups ) . size !== new Set ( filteredGroups ) . size ) {
108
+ var channelsToLeave = utils . findUniqueCommonElements ( this . channels , channels ) ;
109
+ var groupstoLeave = utils . findUniqueCommonElements ( this . groups , channelGroups ) ;
110
+ if ( this . dependencies . presenceState ) {
111
+ channelsToLeave === null || channelsToLeave === void 0 ? void 0 : channelsToLeave . forEach ( function ( c ) { return delete _this . dependencies . presenceState [ c ] ; } ) ;
112
+ groupstoLeave === null || groupstoLeave === void 0 ? void 0 : groupstoLeave . forEach ( function ( g ) { return delete _this . dependencies . presenceState [ g ] ; } ) ;
113
+ }
114
+ this . channels = filteredChannels ;
115
+ this . groups = filteredGroups ;
116
+ this . engine . transition ( events . subscriptionChange ( Array . from ( new Set ( this . channels . slice ( 0 ) ) ) , Array . from ( new Set ( this . groups . slice ( 0 ) ) ) ) ) ;
117
+ if ( this . dependencies . leave ) {
118
+ this . dependencies . leave ( {
119
+ channels : channelsToLeave . slice ( 0 ) ,
120
+ groups : groupstoLeave . slice ( 0 ) ,
121
+ } ) ;
122
+ }
119
123
}
120
124
} ;
121
125
EventEngine . prototype . unsubscribeAll = function ( ) {
0 commit comments