Skip to content

Commit 8b45e26

Browse files
authored
[Fix]Remove TimerStorage (#873)
1 parent 9ffe235 commit 8b45e26

File tree

17 files changed

+68
-173
lines changed

17 files changed

+68
-173
lines changed

.swiftlint.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ excluded:
1212

1313
# Custom Rules
1414
custom_rules:
15-
discourage_timer_publish:
16-
name: "Discouraged Timer.publish usage"
17-
regex: '\bTimer\s*(\.\s*)?publish\b'
18-
message: "Avoid using Timer.publish. User the TimerStorage instead."
19-
severity: error
2015
discourage_task_init:
2116
name: "Discouraged Task { usage"
2217
regex: '\bTask\s*\{'

Sources/StreamVideo/CallKit/CallKitService.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ open class CallKitService: NSObject, CXProviderDelegate, @unchecked Sendable {
1313

1414
@Injected(\.callCache) private var callCache
1515
@Injected(\.uuidFactory) private var uuidFactory
16-
@Injected(\.timers) private var timers
1716
@Injected(\.currentDevice) private var currentDevice
1817
private let disposableBag = DisposableBag()
1918

@@ -613,8 +612,10 @@ open class CallKitService: NSObject, CXProviderDelegate, @unchecked Sendable {
613612
/// - Parameter callState: The state of the call.
614613
open func setUpRingingTimer(for callState: GetCallResponse) {
615614
let timeout = TimeInterval(callState.call.settings.ring.autoCancelTimeoutMs / 1000)
616-
ringingTimerCancellable = timers
617-
.timer(for: timeout)
615+
ringingTimerCancellable = Foundation
616+
.Timer
617+
.publish(every: timeout, on: .main, in: .default)
618+
.autoconnect()
618619
.sink { [weak self] _ in
619620
log.debug(
620621
"Detected ringing timeout, hanging up...",

Sources/StreamVideo/CallState.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public struct PermissionRequest: @unchecked Sendable, Identifiable {
3333
public class CallState: ObservableObject {
3434

3535
@Injected(\.streamVideo) var streamVideo
36-
@Injected(\.timers) var timers
3736

3837
/// The id of the current session.
3938
/// When a call is started, a unique session identifier is assigned to the user in the call.
@@ -507,8 +506,10 @@ public class CallState: ObservableObject {
507506

508507
private func setupDurationTimer() {
509508
resetTimer()
510-
durationCancellable = timers
511-
.timer(for: 1.0)
509+
durationCancellable = Foundation
510+
.Timer
511+
.publish(every: 1.0, on: .main, in: .default)
512+
.autoconnect()
512513
.receive(on: DispatchQueue.main)
513514
.compactMap { [weak self] _ in
514515
if let startedAt = self?.startedAt {

Sources/StreamVideo/StreamVideo.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public typealias UserTokenUpdater = @Sendable(UserToken) -> Void
1515
public class StreamVideo: ObservableObject, @unchecked Sendable {
1616

1717
@Injected(\.callCache) private var callCache
18-
@Injected(\.timers) private var timers
1918
@Injected(\.screenProperties) private var screenProperties
2019

2120
private enum DisposableKey: String { case ringEventReceived }
@@ -532,8 +531,10 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
532531
do {
533532
var cancellable: AnyCancellable?
534533
log.debug("Listening for WS connection")
535-
_ = try await timers
536-
.timer(for: 0.1)
534+
_ = try await Foundation
535+
.Timer
536+
.publish(every: 0.1, on: .main, in: .default)
537+
.autoconnect()
537538
.filter { [weak webSocketClient] _ in webSocketClient?.connectionState.isConnected == true }
538539
.nextValue(timeout: 30) { cancellable = $0 }
539540
cancellable?.cancel()
@@ -585,8 +586,10 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
585586

586587
var cancellable: AnyCancellable?
587588
do {
588-
let result = try await timers
589-
.timer(for: 0.1)
589+
let result = try await Foundation
590+
.Timer
591+
.publish(every: 0.1, on: .main, in: .default)
592+
.autoconnect()
590593
.log(.debug) { _ in "Waiting for connection id" }
591594
.compactMap { [weak self] _ in self?.loadConnectionIdFromHealthcheck() }
592595
.nextValue(timeout: 5) { cancellable = $0 }

Sources/StreamVideo/Utils/AudioSession/AudioRecorder/StreamCallAudioRecorder.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ open class StreamCallAudioRecorder: @unchecked Sendable {
1616

1717
@Injected(\.activeCallProvider) private var activeCallProvider
1818
@Injected(\.activeCallAudioSession) private var activeCallAudioSession
19-
@Injected(\.timers) private var timers
2019

2120
/// The builder used to create the AVAudioRecorder instance.
2221
let audioRecorderBuilder: AVAudioRecorderBuilder
@@ -119,8 +118,10 @@ open class StreamCallAudioRecorder: @unchecked Sendable {
119118

120119
updateMetersTimerCancellable?.cancel()
121120
disposableBag.remove("update-meters")
122-
updateMetersTimerCancellable = timers
123-
.timer(for: ScreenPropertiesAdapter.currentValue.refreshRate)
121+
updateMetersTimerCancellable = Foundation
122+
.Timer
123+
.publish(every: ScreenPropertiesAdapter.currentValue.refreshRate, on: .main, in: .default)
124+
.autoconnect()
124125
.sinkTask(storeIn: disposableBag, identifier: "update-meters") { [weak self, audioRecorder] _ in
125126
audioRecorder.updateMeters()
126127
self?._metersPublisher.send(audioRecorder.averagePower(forChannel: 0))

Sources/StreamVideo/Utils/OrderedCapacityQueue/OrderedCapacityQueue.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Foundation
88
/// A thread-safe queue that maintains a fixed capacity and removes elements after
99
/// a specified time interval.
1010
final class OrderedCapacityQueue<Element> {
11-
@Injected(\.timers) private var timers
1211

1312
private let queue = UnfairQueue()
1413

@@ -50,8 +49,10 @@ final class OrderedCapacityQueue<Element> {
5049
init(capacity: Int, removalTime: TimeInterval) {
5150
self.capacity = capacity
5251
self.removalTime = removalTime
53-
removalTimerCancellable = timers
54-
.timer(for: ScreenPropertiesAdapter.currentValue.refreshRate)
52+
removalTimerCancellable = Foundation
53+
.Timer
54+
.publish(every: ScreenPropertiesAdapter.currentValue.refreshRate, on: .main, in: .default)
55+
.autoconnect()
5556
.receive(on: DispatchQueue.global(qos: .userInteractive))
5657
.sink { [weak self] _ in self?.removeItemsIfRequired() }
5758
}
@@ -87,8 +88,10 @@ final class OrderedCapacityQueue<Element> {
8788
/// should be enabled.
8889
private func toggleRemovalObservation(_ isEnabled: Bool) {
8990
if isEnabled, removalTimerCancellable == nil {
90-
removalTimerCancellable = timers
91-
.timer(for: ScreenPropertiesAdapter.currentValue.refreshRate)
91+
removalTimerCancellable = Foundation
92+
.Timer
93+
.publish(every: ScreenPropertiesAdapter.currentValue.refreshRate, on: .main, in: .default)
94+
.autoconnect()
9295
.sink { [weak self] _ in self?.removeItemsIfRequired() }
9396
} else if !isEnabled, removalTimerCancellable != nil {
9497
removalTimerCancellable?.cancel()

Sources/StreamVideo/Utils/Timers/TimerStorage.swift

Lines changed: 0 additions & 115 deletions
This file was deleted.

Sources/StreamVideo/WebRTC/v2/StateMachine/Stages/WebRTCCoordinator+Disconnected.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ extension WebRTCCoordinator.StateMachine.Stage {
3030
@unchecked Sendable
3131
{
3232
@Injected(\.internetConnectionObserver) private var internetConnectionObserver
33-
@Injected(\.timers) private var timers
3433

3534
private var internetObservationCancellable: AnyCancellable?
3635
private var timeInStageCancellable: AnyCancellable?
@@ -128,8 +127,10 @@ extension WebRTCCoordinator.StateMachine.Stage {
128127
/// We add a small delay of 100ms in oder to ensure that the internet connection state
129128
/// has been updated, so that when we start observing it will receive the latest and
130129
/// updated value.
131-
_ = try? await timers
132-
.timer(for: ScreenPropertiesAdapter.currentValue.refreshRate)
130+
_ = try? await Foundation
131+
.Timer
132+
.publish(every: ScreenPropertiesAdapter.currentValue.refreshRate, on: .main, in: .default)
133+
.autoconnect()
133134
.nextValue { cancellable = $0 }
134135
cancellable?.cancel()
135136
cancellable = nil
@@ -217,8 +218,10 @@ extension WebRTCCoordinator.StateMachine.Stage {
217218
guard context.disconnectionTimeout > 0 else {
218219
return
219220
}
220-
timeInStageCancellable = timers
221-
.timer(for: context.disconnectionTimeout)
221+
timeInStageCancellable = Foundation
222+
.Timer
223+
.publish(every: context.disconnectionTimeout, on: .main, in: .default)
224+
.autoconnect()
222225
.sink { [weak self] _ in self?.didTimeInStageExpired() }
223226
}
224227

Sources/StreamVideo/WebRTC/v2/Stats/Collector/WebRTCStatsCollector.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import StreamWebRTC
1818
/// will start periodic collection upon setting it.
1919
final class WebRTCStatsCollector: WebRTCStatsCollecting, @unchecked Sendable {
2020

21-
@Injected(\.timers) private var timers
22-
2321
/// The most recent `CallStatsReport` generated from collected stats.
2422
///
2523
/// Observers can subscribe to this publisher to receive updates.
@@ -78,8 +76,10 @@ final class WebRTCStatsCollector: WebRTCStatsCollecting, @unchecked Sendable {
7876
}
7977

8078
collectionCancellable?.cancel()
81-
collectionCancellable = timers
82-
.timer(for: interval)
79+
collectionCancellable = Foundation
80+
.Timer
81+
.publish(every: interval, on: .main, in: .default)
82+
.autoconnect()
8383
.receive(on: DispatchQueue.global(qos: .utility))
8484
.log(.debug, subsystems: .webRTC) { _ in "Will collect stats." }
8585
.sink { [weak self] _ in self?.collectStats() }

Sources/StreamVideo/WebRTC/v2/Stats/Reporter/WebRTCStatsReporter.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import StreamWebRTC
1414
final class WebRTCStatsReporter: WebRTCStatsReporting, @unchecked Sendable {
1515

1616
@Injected(\.thermalStateObserver) private var thermalStateObserver
17-
@Injected(\.timers) private var timers
1817

1918
struct Input: @unchecked Sendable {
2019
var sessionID: String
@@ -105,7 +104,10 @@ final class WebRTCStatsReporter: WebRTCStatsReporting, @unchecked Sendable {
105104
return
106105
}
107106

108-
deliveryCancellable = timers.timer(for: interval)
107+
deliveryCancellable = Foundation
108+
.Timer
109+
.publish(every: interval, on: .main, in: .default)
110+
.autoconnect()
109111
.compactMap { [weak self] _ in self?.provider() }
110112
.sink { [weak self] in self?.deliverStats($0) }
111113
}

0 commit comments

Comments
 (0)