@@ -29,7 +29,6 @@ angular.module('mm.addons.mod_chat')
2929 var chatId = $stateParams . chatid ,
3030 courseId = $stateParams . courseid ,
3131 title = $stateParams . title ,
32- polling ,
3332 chatLastTime = 0 ,
3433 pollingRunning = false ;
3534
@@ -115,12 +114,23 @@ angular.module('mm.addons.mod_chat')
115114 return $q . reject ( ) ;
116115 }
117116
117+ // Start the polling to get chat messages periodically.
118+ function startPolling ( ) {
119+ // We already have the polling in place.
120+ if ( $scope . polling ) {
121+ return ;
122+ }
123+
124+ // Start polling.
125+ $scope . polling = $interval ( getMessagesInterval , mmaChatPollInterval ) ;
126+ }
127+
118128 // Convenience function to be called every certain time to get chat messages.
119129 function getMessagesInterval ( ) {
120130 $log . debug ( 'Polling for messages' ) ;
121131 if ( ! $mmApp . isOnline ( ) || pollingRunning ) {
122132 // Obviously we cannot check for new messages when the app is offline.
123- return ;
133+ return $q . reject ( ) ;
124134 }
125135
126136 pollingRunning = true ;
@@ -130,8 +140,11 @@ angular.module('mm.addons.mod_chat')
130140 return loginUser ( ) . then ( function ( ) {
131141 return getMessages ( ) ;
132142 } ) . catch ( function ( error ) {
133- // Fail again. Stop polling.
134- $interval . cancel ( polling ) ;
143+ // Fail again. Stop polling if needed.
144+ if ( $scope . polling ) {
145+ $interval . cancel ( $scope . polling ) ;
146+ $scope . polling = undefined ;
147+ }
135148 return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
136149 } ) ;
137150 } ) . finally ( function ( ) {
@@ -176,9 +189,23 @@ angular.module('mm.addons.mod_chat')
176189 } ) ;
177190 } ;
178191
192+ $scope . reconnect = function ( ) {
193+ var modal = $mmUtil . showModalLoading ( ) ;
194+
195+ // Call startPolling would take a while for the first execution, so we'll execute it manually to check if it works now.
196+ return getMessagesInterval ( ) . then ( function ( ) {
197+ // It works, start the polling again.
198+ startPolling ( ) ;
199+ } ) . finally ( function ( ) {
200+ modal . dismiss ( ) ;
201+ } ) ;
202+ } ;
203+
179204 // Login the user.
180205 loginUser ( ) . then ( function ( ) {
181- return getMessages ( ) . catch ( function ( error ) {
206+ return getMessages ( ) . then ( function ( ) {
207+ startPolling ( ) ;
208+ } ) . catch ( function ( error ) {
182209 return showError ( error , 'mma.mod_chat.errorwhileretrievingmessages' ) ;
183210 } ) ;
184211 } , function ( error ) {
@@ -199,22 +226,11 @@ angular.module('mm.addons.mod_chat')
199226 }
200227 } ;
201228
202- // Set up the polling on a view enter, this allows for the user to go back and resume the polling.
203- $scope . $on ( '$ionicView.enter' , function ( ) {
204- // Strange case, we already have the polling in place.
205- if ( polling ) {
206- return ;
207- }
208-
209- // Start polling.
210- polling = $interval ( getMessagesInterval , mmaChatPollInterval ) ;
211- } ) ;
212-
213229 // Removing the polling as we leave the page.
214230 $scope . $on ( '$ionicView.leave' , function ( ) {
215- if ( polling ) {
231+ if ( $scope . polling ) {
216232 $log . debug ( 'Cancelling polling for conversation' ) ;
217- $interval . cancel ( polling ) ;
233+ $interval . cancel ( $scope . polling ) ;
218234 }
219235 } ) ;
220236
0 commit comments