@@ -30,10 +30,12 @@ var MODULE_NAME = 'NOTIFICATION_CENTER';
30
30
* @constructor
31
31
* @param {Object } options
32
32
* @param {Object } options.logger An instance of a logger to log messages with
33
+ * @param {object } options.errorHandler An instance of errorHandler to handle any unexpected error
33
34
* @returns {Object }
34
35
*/
35
36
function NotificationCenter ( options ) {
36
37
this . logger = options . logger ;
38
+ this . errorHandler = options . errorHandler ;
37
39
this . __notificationListeners = { } ;
38
40
fns . forOwn ( enums . NOTIFICATION_TYPES , function ( notificationTypeEnum ) {
39
41
this . __notificationListeners [ notificationTypeEnum ] = [ ] ;
@@ -51,36 +53,42 @@ function NotificationCenter(options) {
51
53
* can happen if the first argument is not a valid notification type, or if the same callback
52
54
* function was already added as a listener by a prior call to this function.
53
55
*/
54
- NotificationCenter . prototype . addNotificationListener = function ( notificationType , callback ) {
55
- var isNotificationTypeValid = fns . values ( enums . NOTIFICATION_TYPES )
56
- . indexOf ( notificationType ) > - 1 ;
57
- if ( ! isNotificationTypeValid ) {
58
- return - 1 ;
59
- }
56
+ NotificationCenter . prototype . addNotificationListener = function ( notificationType , callback ) {
57
+ try {
58
+ var isNotificationTypeValid = fns . values ( enums . NOTIFICATION_TYPES )
59
+ . indexOf ( notificationType ) > - 1 ;
60
+ if ( ! isNotificationTypeValid ) {
61
+ return - 1 ;
62
+ }
60
63
61
- if ( ! this . __notificationListeners [ notificationType ] ) {
62
- this . __notificationListeners [ notificationType ] = [ ] ;
63
- }
64
+ if ( ! this . __notificationListeners [ notificationType ] ) {
65
+ this . __notificationListeners [ notificationType ] = [ ] ;
66
+ }
64
67
65
- var callbackAlreadyAdded = false ;
66
- fns . forEach ( this . __notificationListeners [ notificationType ] , function ( listenerEntry ) {
67
- if ( listenerEntry . callback === callback ) {
68
- callbackAlreadyAdded = true ;
69
- return false ;
68
+ var callbackAlreadyAdded = false ;
69
+ fns . forEach ( this . __notificationListeners [ notificationType ] , function ( listenerEntry ) {
70
+ if ( listenerEntry . callback === callback ) {
71
+ callbackAlreadyAdded = true ;
72
+ return false ;
73
+ }
74
+ } ) ;
75
+ if ( callbackAlreadyAdded ) {
76
+ return - 1 ;
70
77
}
71
- } ) ;
72
- if ( callbackAlreadyAdded ) {
73
- return - 1 ;
74
- }
75
78
76
- this . __notificationListeners [ notificationType ] . push ( {
77
- id : this . __listenerId ,
78
- callback : callback ,
79
- } ) ;
79
+ this . __notificationListeners [ notificationType ] . push ( {
80
+ id : this . __listenerId ,
81
+ callback : callback ,
82
+ } ) ;
80
83
81
- var returnId = this . __listenerId ;
82
- this . __listenerId += 1 ;
83
- return returnId ;
84
+ var returnId = this . __listenerId ;
85
+ this . __listenerId += 1 ;
86
+ return returnId ;
87
+ } catch ( e ) {
88
+ this . logger . log ( LOG_LEVEL . ERROR , e . message ) ;
89
+ this . errorHandler . handleError ( e ) ;
90
+ return - 1 ;
91
+ }
84
92
} ;
85
93
86
94
/**
@@ -89,45 +97,59 @@ NotificationCenter.prototype.addNotificationListener = function(notificationType
89
97
* @returns {boolean } Returns true if the listener was found and removed, and false
90
98
* otherwise.
91
99
*/
92
- NotificationCenter . prototype . removeNotificationListener = function ( listenerId ) {
93
- var indexToRemove ;
94
- var typeToRemove ;
95
- fns . forOwn ( this . __notificationListeners , function ( listenersForType , notificationType ) {
96
- fns . forEach ( listenersForType , function ( listenerEntry , i ) {
97
- if ( listenerEntry . id === listenerId ) {
98
- indexToRemove = i ;
99
- typeToRemove = notificationType ;
100
+ NotificationCenter . prototype . removeNotificationListener = function ( listenerId ) {
101
+ try {
102
+ var indexToRemove ;
103
+ var typeToRemove ;
104
+ fns . forOwn ( this . __notificationListeners , function ( listenersForType , notificationType ) {
105
+ fns . forEach ( listenersForType , function ( listenerEntry , i ) {
106
+ if ( listenerEntry . id === listenerId ) {
107
+ indexToRemove = i ;
108
+ typeToRemove = notificationType ;
109
+ return false ;
110
+ }
111
+ } ) ;
112
+ if ( indexToRemove !== undefined && typeToRemove !== undefined ) {
100
113
return false ;
101
114
}
102
115
} ) ;
116
+
103
117
if ( indexToRemove !== undefined && typeToRemove !== undefined ) {
104
- return false ;
118
+ this . __notificationListeners [ typeToRemove ] . splice ( indexToRemove , 1 ) ;
119
+ return true ;
105
120
}
106
- } ) ;
107
-
108
- if ( indexToRemove !== undefined && typeToRemove !== undefined ) {
109
- this . __notificationListeners [ typeToRemove ] . splice ( indexToRemove , 1 ) ;
110
- return true ;
121
+ } catch ( e ) {
122
+ this . logger . log ( LOG_LEVEL . ERROR , e . message ) ;
123
+ this . errorHandler . handleError ( e ) ;
111
124
}
112
-
113
125
return false ;
114
126
} ;
115
127
116
128
/**
117
129
* Removes all previously added notification listeners, for all notification types
118
130
*/
119
- NotificationCenter . prototype . clearAllNotificationListeners = function ( ) {
120
- fns . forOwn ( enums . NOTIFICATION_TYPES , function ( notificationTypeEnum ) {
121
- this . __notificationListeners [ notificationTypeEnum ] = [ ] ;
122
- } . bind ( this ) ) ;
131
+ NotificationCenter . prototype . clearAllNotificationListeners = function ( ) {
132
+ try {
133
+ fns . forOwn ( enums . NOTIFICATION_TYPES , function ( notificationTypeEnum ) {
134
+ this . __notificationListeners [ notificationTypeEnum ] = [ ] ;
135
+ } . bind ( this ) ) ;
136
+ } catch ( e ) {
137
+ this . logger . log ( LOG_LEVEL . ERROR , e . message ) ;
138
+ this . errorHandler . handleError ( e ) ;
139
+ }
123
140
} ;
124
141
125
142
/**
126
143
* Remove all previously added notification listeners for the argument type
127
144
* @param {string } notificationType One of enums.NOTIFICATION_TYPES
128
145
*/
129
- NotificationCenter . prototype . clearNotificationListeners = function ( notificationType ) {
130
- this . __notificationListeners [ notificationType ] = [ ] ;
146
+ NotificationCenter . prototype . clearNotificationListeners = function ( notificationType ) {
147
+ try {
148
+ this . __notificationListeners [ notificationType ] = [ ] ;
149
+ } catch ( e ) {
150
+ this . logger . log ( LOG_LEVEL . ERROR , e . message ) ;
151
+ this . errorHandler . handleError ( e ) ;
152
+ }
131
153
} ;
132
154
133
155
/**
@@ -136,15 +158,20 @@ NotificationCenter.prototype.clearNotificationListeners = function(notificationT
136
158
* @param {string } notificationType One of enums.NOTIFICATION_TYPES
137
159
* @param {Object } notificationData Will be passed to callbacks called
138
160
*/
139
- NotificationCenter . prototype . sendNotifications = function ( notificationType , notificationData ) {
140
- fns . forEach ( this . __notificationListeners [ notificationType ] , function ( listenerEntry ) {
141
- var callback = listenerEntry . callback ;
142
- try {
143
- callback ( notificationData ) ;
144
- } catch ( ex ) {
145
- this . logger . log ( LOG_LEVEL . ERROR , sprintf ( LOG_MESSAGES . NOTIFICATION_LISTENER_EXCEPTION , MODULE_NAME , notificationType , ex . message ) ) ;
146
- }
147
- } . bind ( this ) ) ;
161
+ NotificationCenter . prototype . sendNotifications = function ( notificationType , notificationData ) {
162
+ try {
163
+ fns . forEach ( this . __notificationListeners [ notificationType ] , function ( listenerEntry ) {
164
+ var callback = listenerEntry . callback ;
165
+ try {
166
+ callback ( notificationData ) ;
167
+ } catch ( ex ) {
168
+ this . logger . log ( LOG_LEVEL . ERROR , sprintf ( LOG_MESSAGES . NOTIFICATION_LISTENER_EXCEPTION , MODULE_NAME , notificationType , ex . message ) ) ;
169
+ }
170
+ } . bind ( this ) ) ;
171
+ } catch ( e ) {
172
+ this . logger . log ( LOG_LEVEL . ERROR , e . message ) ;
173
+ this . errorHandler . handleError ( e ) ;
174
+ }
148
175
} ;
149
176
150
177
module . exports = {
0 commit comments