@@ -24,16 +24,22 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
2424
2525 static let sharedInstance = DefaultEventDispatcher ( )
2626
27- // the max failure count. there is no backoff timer.
28- static let MAX_FAILURE_COUNT = 3
29-
3027 // default timerInterval
3128 var timerInterval : TimeInterval
3229 // default batchSize.
3330 // attempt to send events in batches with batchSize number of events combined
3431 var batchSize : Int
3532 var maxQueueSize : Int
3633
34+ public struct DefaultValues {
35+ static public let batchSize = 10
36+ static public let timeInterval : TimeInterval = 60 // secs
37+ static public let maxQueueSize = 10000
38+ static let maxFailureCount = 3
39+ }
40+
41+ // the max failure count. there is no backoff timer.
42+
3743 lazy var logger = OPTLoggerFactory . getLogger ( )
3844 var backingStore : DataStoreType
3945 var backingStoreName : String
@@ -45,12 +51,10 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
4551 // timer as a atomic property.
4652 var timer : AtomicProperty < Timer > = AtomicProperty < Timer > ( )
4753
48- public struct DefaultValues {
49- static public let batchSize = 10
50- static public let timeInterval : TimeInterval = 60 // secs
51- static public let maxQueueSize = 10000
52- }
54+ var observerProjectId : NSObjectProtocol ?
55+ var observerRevision : NSObjectProtocol ?
5356
57+
5458 public init ( batchSize: Int = DefaultValues . batchSize,
5559 backingStore: DataStoreType = . file,
5660 dataStoreName: String = " OPTEventQueue " ,
@@ -88,21 +92,11 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
8892 deinit {
8993 stopTimer ( )
9094
95+ removeProjectChangeNotificationObservers ( )
96+
9197 unsubscribe ( )
9298 }
9399
94- func addProjectChangeNotificationObservers( ) {
95- NotificationCenter . default. addObserver ( forName: . didReceiveOptimizelyProjectIdChange, object: nil , queue: nil ) { ( notif) in
96- self . logger. d ( " Event flush triggered by datafile projectId change " )
97- self . flushEvents ( )
98- }
99-
100- NotificationCenter . default. addObserver ( forName: . didReceiveOptimizelyRevisionChange, object: nil , queue: nil ) { ( notif) in
101- self . logger. d ( " Event flush triggered by datafile revision change " )
102- self . flushEvents ( )
103- }
104- }
105-
106100 open func dispatchEvent( event: EventForDispatch , completionHandler: DispatchCompletionHandler ? ) {
107101 guard dataStore. count < maxQueueSize else {
108102 let error = OptimizelyError . eventDispatchFailed ( " EventQueue is full " )
@@ -156,7 +150,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
156150
157151 // we've exhuasted our failure count. Give up and try the next time a event
158152 // is queued or someone calls flush.
159- if failureCount > DefaultEventDispatcher . MAX_FAILURE_COUNT {
153+ if failureCount > DefaultValues . maxFailureCount {
160154 self . logger. e ( . eventSendRetyFailed( failureCount) )
161155 break
162156 }
@@ -252,3 +246,30 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
252246 timer. property = nil
253247 }
254248}
249+
250+ // MARK: - Notification Observers
251+
252+ extension DefaultEventDispatcher {
253+
254+ func addProjectChangeNotificationObservers( ) {
255+ observerProjectId = NotificationCenter . default. addObserver ( forName: . didReceiveOptimizelyProjectIdChange, object: nil , queue: nil ) { [ weak self] ( notif) in
256+ self ? . logger. d ( " Event flush triggered by datafile projectId change " )
257+ self ? . flushEvents ( )
258+ }
259+
260+ observerRevision = NotificationCenter . default. addObserver ( forName: . didReceiveOptimizelyRevisionChange, object: nil , queue: nil ) { [ weak self] ( notif) in
261+ self ? . logger. d ( " Event flush triggered by datafile revision change " )
262+ self ? . flushEvents ( )
263+ }
264+ }
265+
266+ func removeProjectChangeNotificationObservers( ) {
267+ if let observer = observerProjectId {
268+ NotificationCenter . default. removeObserver ( observer, name: . didReceiveOptimizelyProjectIdChange, object: nil )
269+ }
270+ if let observer = observerRevision {
271+ NotificationCenter . default. removeObserver ( observer, name: . didReceiveOptimizelyRevisionChange, object: nil )
272+ }
273+ }
274+
275+ }
0 commit comments