-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathUncleFlushPolicy.swift
60 lines (48 loc) · 1.52 KB
/
UncleFlushPolicy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// UncleFlushPolicy.swift
// Segment
//
// Created by Brandon Sneed on 9/17/24.
//
import Foundation
public class UncleFlushPolicy: FlushPolicy {
public weak var analytics: Analytics?
internal var basePolicies: [FlushPolicy] = [CountBasedFlushPolicy(), IntervalBasedFlushPolicy(), /* .. add your own here .. */]
public init() {
/*
or add your own here ...
```
self.basePolicies.append(MyCrazyUnclesOtherPolicy(onThanksgiving: true)
```
*/
}
private func shouldWeREALLYFlush() -> Bool {
// do some meaningful calculation or check here.
// Ol Unc's was right i guess since we're gonna do what he says.
return true
}
public func configure(analytics: Analytics) {
self.analytics = analytics
basePolicies.forEach { $0.configure(analytics: analytics) }
}
public func shouldFlush() -> Bool {
guard let a = analytics else {
return false
}
var shouldFlush = false
for policy in basePolicies {
shouldFlush = policy.shouldFlush() || shouldFlush
}
if shouldFlush {
// ask the know it all ...
shouldFlush = shouldWeREALLYFlush()
}
return shouldFlush
}
public func updateState(event: RawEvent) {
basePolicies.forEach { $0.updateState(event: event) }
}
public func reset() {
basePolicies.forEach { $0.reset() }
}
}