-
Notifications
You must be signed in to change notification settings - Fork 96
Flush policy #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Flush policy #216
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4926fd6
feat: add flush policy architecture
alanjcharles c3f38ae
Persnippity
bsneed f425a8f
poligy
bsneed 4dd4e5c
add test file for flush policy
alanjcharles fda194d
Example tests
bsneed e54cbde
feat: add flush policy tests and interval flush policy
alanjcharles 9436e2a
remove flush logic in segmentDestination and clean up tests
alanjcharles f0f4198
fix: re-add enterForeground lifecycle method
alanjcharles 968645a
fix: move policies to project, fix memory leak
alanjcharles dd6b70e
merge main into flush-policy to get new test yml
alanjcharles 190d18c
add lifecycle extensions
alanjcharles a5d21c2
fix macOS and watchOS extensions
alanjcharles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift
This file contains hidden or 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,46 @@ | ||
// | ||
// CountBasedFlushPolicy.swift | ||
// | ||
// | ||
// Created by Alan Charles on 3/21/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public class CountBasedFlushPolicy: FlushPolicy { | ||
public weak var analytics: Analytics? | ||
internal var desiredCount: Int? | ||
internal var count: Int = 0 | ||
|
||
init() { } | ||
|
||
init(count: Int) { | ||
desiredCount = count | ||
} | ||
|
||
public func configure(analytics: Analytics) { | ||
self.analytics = analytics | ||
if let desiredCount = desiredCount { | ||
analytics.flushAt = desiredCount | ||
} | ||
} | ||
|
||
public func shouldFlush() -> Bool { | ||
guard let a = analytics else { | ||
return false | ||
} | ||
if a.configuration.values.flushAt > 0 && count >= a.configuration.values.flushAt { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
public func updateState(event: RawEvent) { | ||
count += 1 | ||
} | ||
|
||
public func reset() { | ||
count = 0 | ||
} | ||
} |
This file contains hidden or 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,65 @@ | ||
// | ||
// FlushPolicy.swift | ||
// | ||
// | ||
// Created by Alan Charles on 3/21/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol FlushPolicy: AnyObject { | ||
var analytics: Analytics? { get set } | ||
func configure(analytics: Analytics) -> Void | ||
func shouldFlush() -> Bool | ||
func updateState(event: RawEvent) -> Void | ||
func reset() -> Void | ||
} | ||
|
||
public extension Analytics { | ||
func add(flushPolicy: FlushPolicy) { | ||
guard let state: System = store.currentState() else { return } | ||
let config = state.configuration | ||
var policies = config.values.flushPolicies | ||
policies.append(flushPolicy) | ||
config.flushPolicies(policies) | ||
store.dispatch(action: System.UpdateConfigurationAction(configuration: config)) | ||
|
||
flushPolicy.configure(analytics: self) | ||
} | ||
|
||
func remove(flushPolicy: FlushPolicy) { | ||
guard let state: System = store.currentState() else { return } | ||
let config = state.configuration | ||
let policies = config.values.flushPolicies.filter { policy in | ||
return flushPolicy !== policy | ||
} | ||
config.flushPolicies(policies) | ||
store.dispatch(action: System.UpdateConfigurationAction(configuration: config)) | ||
} | ||
|
||
func remove<T: FlushPolicy>(flushPolicy: T.Type) { | ||
guard let state: System = store.currentState() else { return } | ||
let config = state.configuration | ||
let policies = config.values.flushPolicies.filter { policy in | ||
return !(policy is T) | ||
} | ||
config.flushPolicies(policies) | ||
store.dispatch(action: System.UpdateConfigurationAction(configuration: config)) | ||
} | ||
|
||
func removeAllFlushPolicies() { | ||
guard let state: System = store.currentState() else { return } | ||
let config = state.configuration | ||
config.flushPolicies([]) | ||
store.dispatch(action: System.UpdateConfigurationAction(configuration: config)) | ||
} | ||
|
||
func find<T: FlushPolicy>(flushPolicy: T.Type) -> FlushPolicy? { | ||
guard let state: System = store.currentState() else { return nil } | ||
let config = state.configuration | ||
let found = config.values.flushPolicies.filter { policy in | ||
return policy is T | ||
} | ||
return found.first | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.