Skip to content
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

SwiftTimerQueue #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ timer = SwiftCountDownTimer(interval: .fromSeconds(0.1), times: 10) { timer , le
timer.start()
~~~

### timer queue

~~~swift
timer = SwiftTimerQueue.init(interval: . seconds(1))
for i in 0..<50{
timer.addQueue {
label.text = "\(i)"
}
}
~~~


## Installation
Expand Down
53 changes: 53 additions & 0 deletions Sources/SwiftTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,56 @@ public extension DispatchTimeInterval {
return .milliseconds(Int(seconds * 1000))
}
}


public class SwiftTimerQueue {

private let internalTimer: SwiftTimer
private var workQueue:[DispatchWorkItem]


///The Handler will be called after interval you specified
///Calling again in the interval cancels the previous call
public init(interval: DispatchTimeInterval, queue: DispatchQueue = .main) {
self.workQueue = []
self.internalTimer = SwiftTimer.init(interval: interval, repeats: true, queue: queue) { _ in
}
self.internalTimer.rescheduleHandler {[weak self] swiftTimer in
if let strongSelf = self,let command = strongSelf.workQueue.first,!command.isCancelled {
queue.async(execute: command)
}
}
}

func addQueue(_ handler: @escaping () -> Void ) {
let item = DispatchWorkItem {[weak self] in
guard let weak = self else{
return
}
if weak.workQueue.count > 0{
weak.workQueue.removeFirst()
}
handler();
};
workQueue.append(item)
}

func clean() {
for item in workQueue{
item.cancel()
}
workQueue = []
}

func flowCount()->Int {
return workQueue.count
}

public func start() {
self.internalTimer.start()
}

public func suspend() {
self.internalTimer.suspend()
}
}
21 changes: 21 additions & 0 deletions SwiftTimer iOSTests/SwiftTimer_iOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,25 @@ class SwiftTimer_iOSTests: XCTestCase {
self.waitForExpectations(timeout: 1.01, handler: nil)

}

func testTimerQueue() {

let expectation = self.expectation(description: "test timer queue")

let label = UILabel()

let timer = SwiftTimerQueue.init(interval: .seconds(1))
for i in 0..<50{
timer.addQueue {
label.text = "\(i)"
}
}
timer.addQueue {
expectation.fulfill()
}
timer.start()

self.waitForExpectations(timeout: 51, handler: nil)

}
}