Skip to content

Commit

Permalink
Merge pull request #19 from Claeysson/async
Browse files Browse the repository at this point in the history
Removed semaphore.wait() and introduced a timer pause boolean.
  • Loading branch information
cristipufu authored Jun 14, 2024
2 parents 02f791e + 3a4bce8 commit d876770
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Sources/Aptabase/AptabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AptabaseClient {
private let dispatcher: EventDispatcher
private let env: EnvironmentInfo
private let flushInterval: Double
private var pauseFlushTimer: Bool = false

init(appKey: String, baseUrl: String, env: EnvironmentInfo, options: InitOptions?) {
flushInterval = options?.flushInterval ?? (env.isDebug ? 2.0 : 60.0)
Expand Down Expand Up @@ -46,14 +47,17 @@ class AptabaseClient {
public func startPolling() {
stopPolling()

flushTimer = Timer.scheduledTimer(timeInterval: flushInterval, target: self, selector: #selector(flushSync), userInfo: nil, repeats: true)
flushTimer = Timer.scheduledTimer(timeInterval: flushInterval, target: self, selector: #selector(timerFlushSync), userInfo: nil, repeats: true)
}

public func stopPolling() {
flushTimer?.invalidate()
flushTimer = nil

flushSync()

Task {
await flush()
}

}

public func flush() async {
Expand All @@ -66,12 +70,13 @@ class AptabaseClient {
return String(epochInSeconds * 100000000 + random)
}

@objc private func flushSync() {
let semaphore = DispatchSemaphore(value: 0)
Task {
await self.flush()
semaphore.signal()
@objc private func timerFlushSync() {
if !pauseFlushTimer {
Task {
self.pauseFlushTimer = true
await self.flush()
self.pauseFlushTimer = false
}
}
semaphore.wait()
}
}

0 comments on commit d876770

Please sign in to comment.