33import lo from 'lodash' ;
44import angular from 'angular' ;
55
6- export default function ( app ) {
6+ export default function ( app ) {
77 function StreamSrv (
88 $rootScope ,
99 $http ,
@@ -15,15 +15,15 @@ export default function(app) {
1515 this . isPolling = false ;
1616 this . streamId = null ;
1717
18- this . init = function ( ) {
18+ this . init = function ( ) {
1919 this . streamId = null ;
2020 this . requestStream ( ) ;
2121 } ;
22- this . runCallbacks = function ( id , objectType , message ) {
22+ this . runCallbacks = function ( id , objectType , message ) {
2323 $rootScope . $broadcast ( 'stream:' + id + '-' + objectType , message ) ;
2424 } ;
2525
26- this . handleStreamResponse = function ( data ) {
26+ this . handleStreamResponse = function ( data ) {
2727 if ( ! data || data . length === 0 ) {
2828 return ;
2929 }
@@ -37,9 +37,8 @@ export default function(app) {
3737 let rootId = message . base . rootId ;
3838 let objectType = message . base . objectType ;
3939 let rootIdWithObjectType = rootId + '|' + objectType ;
40- let secondaryObjectTypes = message . summary
41- ? lo . without ( lo . keys ( message . summary ) , objectType )
42- : [ ] ;
40+ let secondaryObjectTypes = message . summary ?
41+ lo . without ( lo . keys ( message . summary ) , objectType ) : [ ] ;
4342
4443 if ( rootId in byRootIds ) {
4544 byRootIds [ rootId ] . push ( message ) ;
@@ -94,7 +93,7 @@ export default function(app) {
9493 this . runCallbacks ( 'any' , 'any' , data ) ;
9594 } ;
9695
97- this . poll = function ( ) {
96+ this . poll = function ( ) {
9897 // Skip polling is a poll is already running
9998 if ( this . streamId === null || this . isPolling === true ) {
10099 return ;
@@ -104,46 +103,46 @@ export default function(app) {
104103 this . isPolling = true ;
105104
106105 // Poll stream changes
107- $http . get ( './api/stream/' + this . streamId ) . then (
108- response => {
109- // Flag polling end
110- this . isPolling = false ;
111-
112- // Handle stream data and callbacks
113- this . handleStreamResponse ( response . data ) ;
114-
115- // Check if the session will expire soon
116- // TODO
117- // if (status === 220) {
118- // AfkSrv.prompt().then(function () {
119- // UserSrv.getUserInfo(AuthService.currentUser.id).then(
120- // function () {},
121- // function (response) {
122- // NotificationService.error('StreamSrv', response.data, response.status);
123- // }
124- // );
125- // });
126- // }
127- this . poll ( ) ;
128- } ,
129- ( data , status ) => {
106+ $http . get ( './api/stream/' + this . streamId )
107+ . then (
108+ response => {
109+ // Flag polling end
110+ this . isPolling = false ;
111+
112+ // Handle stream data and callbacks
113+ this . handleStreamResponse ( response . data ) ;
114+
115+ // Check if the session will expire soon
116+ // TODO
117+ // if (status === 220) {
118+ // AfkSrv.prompt().then(function () {
119+ // UserSrv.getUserInfo(AuthService.currentUser.id).then(
120+ // function () {},
121+ // function (response) {
122+ // NotificationService.error('StreamSrv', response.data, response.status);
123+ // }
124+ // );
125+ // });
126+ // }
127+ this . poll ( ) ;
128+ } )
129+ . catch ( err => {
130130 // Initialize the stream;
131131 this . isPolling = false ;
132132
133- if ( status !== 404 ) {
134- NotificationService . error ( 'StreamSrv' , data , status ) ;
133+ if ( err . status !== 404 ) {
134+ NotificationService . error ( 'StreamSrv' , err . data , err . status ) ;
135135
136- if ( status === 401 ) {
136+ if ( err . status === 401 ) {
137137 return ;
138138 }
139139 }
140140
141141 this . init ( ) ;
142- }
143- ) ;
142+ } ) ;
144143 } ;
145144
146- this . requestStream = function ( ) {
145+ this . requestStream = function ( ) {
147146 if ( this . streamId !== null ) {
148147 return ;
149148 }
@@ -166,7 +165,7 @@ export default function(app) {
166165 * <li>scope {Object}</li>
167166 * <li>callback {Function}</li>
168167 */
169- this . addListener = function ( config ) {
168+ this . addListener = function ( config ) {
170169 if ( ! config . scope ) {
171170 $log . error ( 'No scope provided, use the old listen method' , config ) ;
172171 this . listen ( config . rootId , config . objectType , config . callback ) ;
@@ -182,4 +181,4 @@ export default function(app) {
182181 }
183182
184183 app . service ( 'StreamSrv' , StreamSrv ) ;
185- }
184+ }
0 commit comments