17
17
import Foundation
18
18
19
19
public 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
+ }
22
49
23
50
var observerLogEvent : NSObjectProtocol ?
24
51
25
52
required public init ( ) {
26
53
addInternalNotificationListners ( )
54
+ notifications = Notifications ( )
27
55
}
28
56
29
57
deinit {
30
58
removeInternalNotificationListners ( )
31
59
}
32
60
33
- internal func incrementNotificationId( ) -> Int {
34
- let returnValue = notificationId
35
- notificationId += 1
36
- return returnValue
37
- }
38
-
39
61
public func addGenericNotificationListener( notificationType: Int , listener: @escaping GenericListener ) -> Int ? {
40
- notificationListeners [ notificationId] = ( notificationType, listener)
41
62
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
43
70
}
44
71
45
72
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
47
74
guard let myArgs = args [ 0 ] as? [ Any ? ] else {
48
75
return
49
76
}
@@ -62,13 +89,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
62
89
63
90
activateListener ( experimentData, userId, attributes, variationData, event)
64
91
}
65
- } )
92
+ }
66
93
67
- return incrementNotificationId ( )
94
+ return addGenericNotificationListener ( notificationType : NotificationType . activate . rawValue , listener : listener )
68
95
}
69
96
70
97
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
72
99
guard let myArgs = args [ 0 ] as? [ Any ? ] else {
73
100
return
74
101
}
@@ -82,13 +109,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
82
109
let event = myArgs [ 4 ] as? [ String : Any ] {
83
110
trackListener ( eventKey, userId, attributes, eventTags, event)
84
111
}
85
- } )
112
+ }
86
113
87
- return incrementNotificationId ( )
114
+ return addGenericNotificationListener ( notificationType : NotificationType . track . rawValue , listener : listener )
88
115
}
89
116
90
117
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
92
119
guard let myArgs = args [ 0 ] as? [ Any ? ] else {
93
120
return
94
121
}
@@ -101,13 +128,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
101
128
let decisionInfo = myArgs [ 3 ] as? [ String : Any ] {
102
129
decisionListener ( type, userId, attributes, decisionInfo)
103
130
}
104
- } )
131
+ }
105
132
106
- return incrementNotificationId ( )
133
+ return addGenericNotificationListener ( notificationType : NotificationType . decision . rawValue , listener : listener )
107
134
}
108
135
109
136
public func addDatafileChangeNotificationListener( datafileListener: @escaping DatafileChangeListener ) -> Int ? {
110
- notificationListeners [ notificationId ] = ( NotificationType . datafileChange . rawValue , { ( args: Any... ) in
137
+ let listener = { ( args: Any... ) in
111
138
guard let myArgs = args [ 0 ] as? [ Any ? ] else {
112
139
return
113
140
}
@@ -117,13 +144,13 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
117
144
if let data = myArgs [ 0 ] as? Data {
118
145
datafileListener ( data)
119
146
}
120
- } )
147
+ }
121
148
122
- return incrementNotificationId ( )
149
+ return addGenericNotificationListener ( notificationType : NotificationType . datafileChange . rawValue , listener : listener )
123
150
}
124
151
125
152
public func addLogEventNotificationListener( logEventListener: @escaping LogEventListener ) -> Int ? {
126
- notificationListeners [ notificationId ] = ( NotificationType . logEvent . rawValue , { ( args: Any... ) in
153
+ let listener = { ( args: Any... ) in
127
154
guard let myArgs = args [ 0 ] as? [ Any ? ] else {
128
155
return
129
156
}
@@ -134,26 +161,37 @@ public class DefaultNotificationCenter: OPTNotificationCenter {
134
161
let event = myArgs [ 1 ] as? [ String : Any ] {
135
162
logEventListener ( url, event)
136
163
}
137
- } )
164
+ }
138
165
139
- return incrementNotificationId ( )
166
+ return addGenericNotificationListener ( notificationType : NotificationType . logEvent . rawValue , listener : listener )
140
167
}
141
168
142
169
public func removeNotificationListener( notificationId: Int ) {
143
- self . notificationListeners. removeValue ( forKey: notificationId)
170
+ _listeners. performAtomic { ( listeners) in
171
+ listeners. notificationListeners. removeValue ( forKey: notificationId)
172
+ }
144
173
}
145
174
146
175
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
+ }
148
179
}
149
180
150
181
public func clearAllNotificationListeners( ) {
151
- self . notificationListeners. removeAll ( )
182
+ _listeners. performAtomic { ( listeners) in
183
+ listeners. notificationListeners. removeAll ( )
184
+ }
152
185
}
153
186
154
187
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)
157
195
}
158
196
}
159
197
0 commit comments