Skip to content

Fix 232ms UI hang in call duration timer #836

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions Sources/StreamVideo/CallState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class CallState: ObservableObject {
@Published public internal(set) var startsAt: Date?
@Published public internal(set) var startedAt: Date? {
didSet {
_cachedStartedAt = startedAt
setupDurationTimer()
}
}
Expand Down Expand Up @@ -153,7 +154,9 @@ public class CallState: ObservableObject {
}

private var localCallSettingsUpdate = false
private var durationTimer: Foundation.Timer?
private var durationTimer: DispatchSourceTimer?
/// Cache to avoid @Published access
private var _cachedStartedAt: Date?

/// We mark this one as `nonisolated` to allow us to initialise a state instance without isolation.
/// That's a safe operation because `MainActor` is only required to ensure that all `@Published`
Expand Down Expand Up @@ -497,33 +500,37 @@ public class CallState: ObservableObject {

private func setupDurationTimer() {
resetTimer()
durationTimer = Foundation.Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { [weak self] timer in
guard let self else {
timer.invalidate()
return

// Create timer on background queue
let timer = DispatchSource.makeTimerSource(queue: .global(qos: .utility))
timer.schedule(deadline: .now() + 1, repeating: 1.0)

timer.setEventHandler { [weak self] in
guard let self = self else { return }

// Calculate duration using cached value
let duration: TimeInterval
if let startedAt = self._cachedStartedAt {
duration = Date().timeIntervalSince(startedAt)
} else {
duration = 0
}
Task {
await MainActor.run {
self.updateDuration()
}

// Update on main thread
DispatchQueue.main.async {
self.update(duration: duration)
}
})
}

timer.resume()
self.durationTimer = timer
}

private func resetTimer() {
durationTimer?.invalidate()
durationTimer?.cancel()
durationTimer = nil
}

@objc private func updateDuration() {
guard let startedAt else {
update(duration: 0)
return
}
let timeInterval = Date().timeIntervalSince(startedAt)
update(duration: timeInterval)
}

private func update(duration: TimeInterval) {
if duration != self.duration {
self.duration = duration
Expand Down