@@ -98,6 +98,118 @@ describe('Presence', function() {
98
98
] , done ) ;
99
99
} ) ;
100
100
101
+ it ( 'gets presence during a destroy' , function ( done ) {
102
+ var localPresence1 = presence1 . create ( 'presence-1' ) ;
103
+ var presence2a ;
104
+
105
+ async . series ( [
106
+ presence2 . subscribe . bind ( presence2 ) ,
107
+ function ( next ) {
108
+ presence2 . destroy ( errorHandler ( done ) ) ;
109
+ next ( ) ;
110
+ } ,
111
+ function ( next ) {
112
+ presence2a = connection2 . getPresence ( 'test-channel' ) ;
113
+ presence2a . subscribe ( function ( error ) {
114
+ next ( error ) ;
115
+ } ) ;
116
+ } ,
117
+ function ( next ) {
118
+ localPresence1 . submit ( { index : 5 } , errorHandler ( done ) ) ;
119
+ presence2a . once ( 'receive' , function ( ) {
120
+ next ( ) ;
121
+ } ) ;
122
+ }
123
+ ] , done ) ;
124
+ } ) ;
125
+
126
+ it ( 'destroys old local presence but keeps new local presence when getting during destroy' , function ( done ) {
127
+ presence2 . create ( 'presence-2' ) ;
128
+ var presence2a ;
129
+
130
+ async . series ( [
131
+ presence2 . subscribe . bind ( presence2 ) ,
132
+ function ( next ) {
133
+ presence2 . destroy ( function ( ) {
134
+ expect ( presence2 ) . to . equal ( presence2a ) ;
135
+ expect ( Object . keys ( presence2 . localPresences ) ) . to . eql ( [ 'presence-2a' ] ) ;
136
+ done ( ) ;
137
+ } ) ;
138
+ next ( ) ;
139
+ } ,
140
+ function ( next ) {
141
+ presence2a = connection2 . getPresence ( 'test-channel' ) ;
142
+ presence2a . create ( 'presence-2a' ) ;
143
+ presence2a . subscribe ( function ( error ) {
144
+ next ( error ) ;
145
+ } ) ;
146
+ }
147
+ ] , errorHandler ( done ) ) ;
148
+ } ) ;
149
+
150
+ it ( 'destroys old local presence but keeps new local presence when getting during destroy' , function ( done ) {
151
+ presence2 . create ( 'presence-2' ) ;
152
+ var presence2a ;
153
+
154
+ async . series ( [
155
+ presence2 . subscribe . bind ( presence2 ) ,
156
+ function ( next ) {
157
+ presence2 . destroy ( function ( ) {
158
+ expect ( presence2 ) . to . equal ( presence2a ) ;
159
+ expect ( Object . keys ( presence2 . localPresences ) ) . to . eql ( [ 'presence-2a' ] ) ;
160
+ done ( ) ;
161
+ } ) ;
162
+ next ( ) ;
163
+ } ,
164
+ function ( next ) {
165
+ presence2a = connection2 . getPresence ( 'test-channel' ) ;
166
+ presence2a . create ( 'presence-2a' ) ;
167
+ presence2a . subscribe ( function ( error ) {
168
+ next ( error ) ;
169
+ } ) ;
170
+ }
171
+ ] , errorHandler ( done ) ) ;
172
+ } ) ;
173
+
174
+ it ( 'throws if trying to create local presence when wanting destroy' , function ( done ) {
175
+ presence2 . destroy ( errorHandler ( done ) ) ;
176
+ expect ( function ( ) {
177
+ presence2 . create ( 'presence-2' ) ;
178
+ } ) . to . throw ( 'Presence is being destroyed' ) ;
179
+ done ( ) ;
180
+ } ) ;
181
+
182
+ it ( 'gets presence after destroy unsubscribe' , function ( done ) {
183
+ var localPresence2 = presence2 . create ( 'presence-2' ) ;
184
+ var presence2a ;
185
+
186
+ var flushLocalPresence2Destroy ;
187
+ sinon . stub ( localPresence2 , 'destroy' ) . callsFake ( function ( callback ) {
188
+ flushLocalPresence2Destroy = callback ;
189
+ } ) ;
190
+
191
+ async . series ( [
192
+ presence2 . subscribe . bind ( presence2 ) ,
193
+ function ( next ) {
194
+ presence2 . destroy ( function ( ) {
195
+ expect ( connection2 . getPresence ( 'test-channel' ) ) . to . equal ( presence2a ) ;
196
+ done ( ) ;
197
+ } ) ;
198
+ next ( ) ;
199
+ } ,
200
+ // Wait for the destroy unsubscribe callback to start, where we check
201
+ // _wantsDestroy for the first time
202
+ presence2 . unsubscribe . bind ( presence2 ) ,
203
+ function ( next ) {
204
+ presence2a = connection2 . getPresence ( 'test-channel' ) ;
205
+ presence2a . subscribe ( function ( error ) {
206
+ next ( error ) ;
207
+ } ) ;
208
+ flushLocalPresence2Destroy ( ) ;
209
+ }
210
+ ] , errorHandler ( done ) ) ;
211
+ } ) ;
212
+
101
213
it ( 'requests existing presence from other subscribed clients when subscribing' , function ( done ) {
102
214
var localPresence1 = presence1 . create ( 'presence-1' ) ;
103
215
async . series ( [
0 commit comments