From d50697a6f148b59648b09ba9827e44dc69b46b36 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Tue, 17 Sep 2024 13:08:45 -0700 Subject: [PATCH] Fixed flush policy over-flush potential; Added example policy aggregator. --- Examples/tasks/UncleFlushPolicy.swift | 60 +++++++++++++++++++++++++++ Sources/Segment/Analytics.swift | 23 +++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Examples/tasks/UncleFlushPolicy.swift diff --git a/Examples/tasks/UncleFlushPolicy.swift b/Examples/tasks/UncleFlushPolicy.swift new file mode 100644 index 00000000..92c5f1ab --- /dev/null +++ b/Examples/tasks/UncleFlushPolicy.swift @@ -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() } + } +} diff --git a/Sources/Segment/Analytics.swift b/Sources/Segment/Analytics.swift index 500d1380..99d0c83a 100644 --- a/Sources/Segment/Analytics.swift +++ b/Sources/Segment/Analytics.swift @@ -99,7 +99,7 @@ public class Analytics { _ = timeline.process(incomingEvent: event, enrichments: enrichments) - let flushPolicies = configuration.values.flushPolicies + /*let flushPolicies = configuration.values.flushPolicies for policy in flushPolicies { policy.updateState(event: event) @@ -107,6 +107,27 @@ public class Analytics { flush() policy.reset() } + }*/ + + let flushPolicies = configuration.values.flushPolicies + + var shouldFlush = false + // if any policy says to flush, make note of that + for policy in flushPolicies { + policy.updateState(event: event) + if policy.shouldFlush() { + shouldFlush = true + // we don't need to updateState on any others since we're gonna reset it below. + break + } + } + // if we were told to flush do it. + if shouldFlush { + // reset all the policies if one decided to flush. + flushPolicies.forEach { + $0.reset() + } + flush() } }