Skip to content

Commit 1cf4016

Browse files
author
Vladislav Alekseev
committed
Add Timer library
1 parent 3ec5bef commit 1cf4016

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
generate:
2+
swift package generate-xcodeproj --xcconfig-overrides Package.xcconfig --enable-code-coverage
3+
.PHONY: generate
4+
5+
open: generate
6+
open *.xcodeproj
7+
.PHONY: open
8+
9+
clean:
10+
rm -rf .build/
11+
.PHONY: clean
12+
13+
build:
14+
swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.13" --static-swift-stdlib
15+
16+
run:
17+
swift run -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.13" --static-swift-stdlib
18+
19+
test:
20+
swift test -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.13" --parallel

Package.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// swift-tools-version:4.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "AvitoSharedUtils",
7+
products: [
8+
.library(
9+
name: "Timer",
10+
targets: [
11+
"Timer"
12+
]
13+
)
14+
],
15+
dependencies: [],
16+
targets: [
17+
.target(
18+
// MARK: Timer
19+
name: "Timer",
20+
dependencies: [
21+
]
22+
),
23+
]
24+
)

Package.xcconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MACOSX_DEPLOYMENT_TARGET=10.13
2+
SUPPORTED_PLATFORMS=macosx

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
ios-shared-utils
1+
# Avito Shared Utils
2+
3+
Swift package that provides some basic shareable utility classes.
4+
5+
## Developing
6+
7+
- `make open` to generate Xcode project and open it in Xcode
8+
- `make test` to run all unit tests using SPM
9+
- `make build` to build all products using SPM
10+
11+
## Timer
12+
13+
A GCD-based timer.
14+
15+
```swift
16+
let timer = DispatchBasedTimer.startedTimer(repeating: .seconds(1), leeway: .seconds(1)) { [weak self] timer in
17+
guard let strongSelf = self else {
18+
timer.stop()
19+
return
20+
}
21+
strongSelf.handleTimerTick()
22+
}
23+
```
24+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Dispatch
2+
import Foundation
3+
4+
public final class DispatchBasedTimer {
5+
private var timer: DispatchSourceTimer?
6+
private let queue = DispatchQueue(label: "DispatchBasedTimer.queue")
7+
private let deadline: DispatchTime
8+
private let repeating: DispatchTimeInterval
9+
private let leeway: DispatchTimeInterval
10+
11+
public init(deadline: DispatchTime = .now(), repeating: DispatchTimeInterval, leeway: DispatchTimeInterval) {
12+
self.deadline = deadline
13+
self.repeating = repeating
14+
self.leeway = leeway
15+
}
16+
17+
public static func startedTimer(
18+
deadline: DispatchTime = .now(),
19+
repeating: DispatchTimeInterval,
20+
leeway: DispatchTimeInterval,
21+
handler: @escaping (DispatchBasedTimer) -> ())
22+
-> DispatchBasedTimer
23+
{
24+
let timer = DispatchBasedTimer(deadline: deadline, repeating: repeating, leeway: leeway)
25+
timer.start(handler: handler)
26+
return timer
27+
}
28+
29+
deinit {
30+
stop()
31+
}
32+
33+
public func start(handler: @escaping (DispatchBasedTimer) -> ()) {
34+
stop()
35+
36+
let timer = DispatchSource.makeTimerSource(queue: queue)
37+
timer.schedule(deadline: deadline, repeating: repeating, leeway: leeway)
38+
timer.setEventHandler(handler: { handler(self) })
39+
timer.resume()
40+
self.timer = timer
41+
}
42+
43+
public func stop() {
44+
timer?.cancel()
45+
timer?.setEventHandler(handler: nil)
46+
}
47+
48+
public func pause() {
49+
timer?.suspend()
50+
}
51+
52+
public func resume() {
53+
timer?.resume()
54+
}
55+
}

0 commit comments

Comments
 (0)