-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed flush policy over-flush potential; Added example policy aggrega…
…tor. (#362)
- Loading branch information
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters