Skip to content

Commit

Permalink
feat: LifecycleProducer events are emitted immediately (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored May 28, 2024
1 parent 873e6b7 commit accaaa3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public class Confidence: ConfidenceEventSender {
return
}
self.track(eventName: event.name, message: event.message)
if event.shouldFlush {
eventSenderEngine.flush()
}
}
.store(in: &cancellables)
}
Expand Down
18 changes: 13 additions & 5 deletions Sources/Confidence/ConfidenceAppLifecycleProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
.eraseToAnyPublisher()
}

private func track(eventName: String) {
private func track(eventName: String, shouldFlush: Bool) {
let previousBuild: String? = UserDefaults.standard.string(forKey: Self.userDefaultBuildNameKey)
let previousVersion: String? = UserDefaults.standard.string(forKey: Self.userDefaultVersionNameKey)

Expand All @@ -69,12 +69,20 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence

if eventName == Self.appLaunchedEventName {
if previousBuild == nil && previousVersion == nil {
events.send(Event(name: ConfidenceAppLifecycleProducer.appInstalledEventName, message: [:]))
events.send(Event(
name: ConfidenceAppLifecycleProducer.appInstalledEventName,
message: [:],
shouldFlush: shouldFlush)
)
} else if previousBuild != currentBuild || previousVersion != currentVersion {
events.send(Event(name: ConfidenceAppLifecycleProducer.appUpdatedEventName, message: [:]))
events.send(Event(
name: ConfidenceAppLifecycleProducer.appUpdatedEventName,
message: [:],
shouldFlush: shouldFlush)
)
}
}
events.send(Event(name: eventName, message: [:]))
events.send(Event(name: eventName, message: [:], shouldFlush: shouldFlush))

UserDefaults.standard.setValue(currentVersion, forKey: Self.userDefaultVersionNameKey)
UserDefaults.standard.setValue(currentBuild, forKey: Self.userDefaultBuildNameKey)
Expand Down Expand Up @@ -104,7 +112,7 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
case UIApplication.willEnterForegroundNotification:
updateContext(isForeground: true)
case UIApplication.didBecomeActiveNotification:
track(eventName: Self.appLaunchedEventName)
track(eventName: Self.appLaunchedEventName, shouldFlush: true)
default:
break
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Confidence/ConfidenceProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public protocol ConfidenceProducer {
public struct Event {
let name: String
let message: ConfidenceStruct
let shouldFlush: Bool

public init(name: String, message: ConfidenceStruct = [:]) {
public init(name: String, message: ConfidenceStruct = [:], shouldFlush: Bool = false) {
self.name = name
self.message = message
self.shouldFlush = shouldFlush
}
}

Expand Down

0 comments on commit accaaa3

Please sign in to comment.