-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add flush policy architecture * feat: add flush policy tests and interval flush policy * remove flush logic in segmentDestination and clean up tests * fix: re-add enterForeground lifecycle method * fix: move policies to project, fix memory leak * add lifecycle extensions * fix macOS and watchOS extensions --------- Co-authored-by: Alan Charles <[email protected]> Co-authored-by: Brandon Sneed <[email protected]>
- Loading branch information
1 parent
01fa6d3
commit d941847
Showing
15 changed files
with
457 additions
and
64 deletions.
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
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
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
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
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
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
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
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
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
46 changes: 46 additions & 0 deletions
46
Sources/Segment/Utilities/Policies/CountBasedFlushPolicy.swift
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,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 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.