1717import Foundation
1818
1919public class DefaultNotificationCenter : OPTNotificationCenter {
20- public var notificationId : Int = 1
21- var notificationListeners = [ Int: ( Int, GenericListener) ] ( )
20+ public var notificationId : Int {
21+ get {
22+ return notifications. notificationId
23+ }
24+ set {
25+ // don't do it.
26+ }
27+ }
28+
29+ class Notifications {
30+ var notificationId : Int = 1
31+ var notificationListeners = [ Int: ( Int, GenericListener) ] ( )
32+ func incrementNotificationId( ) -> Int {
33+ let returnValue = notificationId
34+ notificationId += 1
35+ return returnValue
36+ }
37+ }
38+
39+ private var _listeners : AtomicProperty < Notifications > = AtomicProperty < Notifications > ( )
40+
41+ var notifications : Notifications {
42+ get {
43+ return _listeners. property!
44+ }
45+ set {
46+ _listeners. property = newValue;
47+ }
48+ }
2249
2350 var observerLogEvent : NSObjectProtocol ?
2451
2552 required public init ( ) {
2653 addInternalNotificationListners ( )
54+ notifications = Notifications ( )
2755 }
2856
2957 deinit {
3058 removeInternalNotificationListners ( )
3159 }
3260
33- internal func incrementNotificationId( ) -> Int {
34- let returnValue = notificationId
35- notificationId += 1
36- return returnValue
37- }
38-
3961 public func addGenericNotificationListener( notificationType: Int , listener: @escaping GenericListener ) -> Int ? {
40- notificationListeners [ notificationId] = ( notificationType, listener)
4162
42- return incrementNotificationId ( )
63+ var id = 0
64+ _listeners. performAtomic { ( notifications) in
65+ notifications. notificationListeners [ notifications. notificationId] = ( notificationType, listener)
66+
67+ id = notifications. incrementNotificationId ( )
68+ }
69+ return id
4370 }
4471
4572 public func addActivateNotificationListener( activateListener: @escaping ( OptimizelyExperimentData , String , OptimizelyAttributes ? , OptimizelyVariationData , [ String : Any ] ) -> Void ) -> Int ? {
46- notificationListeners [ notificationId ] = ( NotificationType . activate . rawValue , { ( args: Any... ) in
73+ let listener = { ( args: Any... ) in
4774 guard let myArgs = args [ 0 ] as? [ Any ? ] else {
4875 return
4976 }
@@ -62,13 +89,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
6289
6390 activateListener ( experimentData, userId, attributes, variationData, event)
6491 }
65- } )
92+ }
6693
67- return incrementNotificationId ( )
94+ return addGenericNotificationListener ( notificationType : NotificationType . activate . rawValue , listener : listener )
6895 }
6996
7097 public func addTrackNotificationListener( trackListener: @escaping ( String , String , OptimizelyAttributes ? , [ String : Any ] ? , [ String : Any ] ) -> Void ) -> Int ? {
71- notificationListeners [ notificationId ] = ( NotificationType . track . rawValue , { ( args: Any... ) in
98+ let listener = { ( args: Any... ) in
7299 guard let myArgs = args [ 0 ] as? [ Any ? ] else {
73100 return
74101 }
@@ -82,13 +109,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
82109 let event = myArgs [ 4 ] as? [ String : Any ] {
83110 trackListener ( eventKey, userId, attributes, eventTags, event)
84111 }
85- } )
112+ }
86113
87- return incrementNotificationId ( )
114+ return addGenericNotificationListener ( notificationType : NotificationType . track . rawValue , listener : listener )
88115 }
89116
90117 public func addDecisionNotificationListener( decisionListener: @escaping ( String , String , OptimizelyAttributes ? , [ String : Any ] ) -> Void ) -> Int ? {
91- notificationListeners [ notificationId ] = ( NotificationType . decision . rawValue , { ( args: Any... ) in
118+ let listener = { ( args: Any... ) in
92119 guard let myArgs = args [ 0 ] as? [ Any ? ] else {
93120 return
94121 }
@@ -101,13 +128,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
101128 let decisionInfo = myArgs [ 3 ] as? [ String : Any ] {
102129 decisionListener ( type, userId, attributes, decisionInfo)
103130 }
104- } )
131+ }
105132
106- return incrementNotificationId ( )
133+ return addGenericNotificationListener ( notificationType : NotificationType . decision . rawValue , listener : listener )
107134 }
108135
109136 public func addDatafileChangeNotificationListener( datafileListener: @escaping DatafileChangeListener ) -> Int ? {
110- notificationListeners [ notificationId ] = ( NotificationType . datafileChange . rawValue , { ( args: Any... ) in
137+ let listener = { ( args: Any... ) in
111138 guard let myArgs = args [ 0 ] as? [ Any ? ] else {
112139 return
113140 }
@@ -117,13 +144,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
117144 if let data = myArgs [ 0 ] as? Data {
118145 datafileListener ( data)
119146 }
120- } )
147+ }
121148
122- return incrementNotificationId ( )
149+ return addGenericNotificationListener ( notificationType : NotificationType . datafileChange . rawValue , listener : listener )
123150 }
124151
125152 public func addLogEventNotificationListener( logEventListener: @escaping LogEventListener ) -> Int ? {
126- notificationListeners [ notificationId ] = ( NotificationType . logEvent . rawValue , { ( args: Any... ) in
153+ let listener = { ( args: Any... ) in
127154 guard let myArgs = args [ 0 ] as? [ Any ? ] else {
128155 return
129156 }
@@ -134,26 +161,37 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
134161 let event = myArgs [ 1 ] as? [ String : Any ] {
135162 logEventListener ( url, event)
136163 }
137- } )
164+ }
138165
139- return incrementNotificationId ( )
166+ return addGenericNotificationListener ( notificationType : NotificationType . logEvent . rawValue , listener : listener )
140167 }
141168
142169 public func removeNotificationListener( notificationId: Int ) {
143- self . notificationListeners. removeValue ( forKey: notificationId)
170+ _listeners. performAtomic { ( listeners) in
171+ listeners. notificationListeners. removeValue ( forKey: notificationId)
172+ }
144173 }
145174
146175 public func clearNotificationListeners( type: NotificationType ) {
147- self . notificationListeners = self . notificationListeners. filter ( { $1. 0 != type. rawValue} )
176+ _listeners. performAtomic { ( listeners) in
177+ listeners. notificationListeners = listeners. notificationListeners. filter ( { $1. 0 != type. rawValue} )
178+ }
148179 }
149180
150181 public func clearAllNotificationListeners( ) {
151- self . notificationListeners. removeAll ( )
182+ _listeners. performAtomic { ( listeners) in
183+ listeners. notificationListeners. removeAll ( )
184+ }
152185 }
153186
154187 public func sendNotifications( type: Int , args: [ Any ? ] ) {
155- for values in notificationListeners. values where values. 0 == type {
156- values. 1 ( args)
188+ var selected = [ GenericListener] ( )
189+ _listeners. performAtomic { ( listeners) in
190+ selected = listeners. notificationListeners. values. filter { $0. 0 == type } . map { $0. 1 }
191+ }
192+
193+ for listener in selected {
194+ listener ( args)
157195 }
158196 }
159197
0 commit comments