-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from iglance/release/v2.0.6
Release/v2.0.6
- Loading branch information
Showing
20 changed files
with
238 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (C) 2020 D0miH <https://github.com/D0miH> & Contributors <https://github.com/iglance/iGlance/graphs/contributors> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import Foundation | ||
|
||
|
||
/** | ||
* This class is a timer that is reaping a given event handler every time interval. | ||
* | ||
* This class is taken from https://gist.github.com/danielgalasko/1da90276f23ea24cb3467c33d2c05768#file-repeatingtimer-swift. | ||
* Huge thanks to Daniel Galasko <https://github.com/danielgalasko> for providing this code. | ||
*/ | ||
class RepeatingTimer { | ||
let timeInterval: TimeInterval | ||
|
||
init(timeInterval: TimeInterval) { | ||
self.timeInterval = timeInterval | ||
} | ||
|
||
private lazy var timer: DispatchSourceTimer = { | ||
let timer = DispatchSource.makeTimerSource() | ||
timer.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval) | ||
timer.setEventHandler { [weak self] in | ||
self?.eventHandler?() | ||
} | ||
return timer | ||
}() | ||
|
||
var eventHandler: (() -> Void)? | ||
|
||
private enum State { | ||
case suspended | ||
case resumed | ||
} | ||
|
||
private var state: State = .suspended | ||
|
||
deinit { | ||
timer.setEventHandler {} | ||
timer.cancel() | ||
/* | ||
If the timer is suspended, calling cancel without resuming | ||
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902 | ||
*/ | ||
resume() | ||
eventHandler = nil | ||
} | ||
|
||
func resume() { | ||
if state == .resumed { | ||
return | ||
} | ||
state = .resumed | ||
timer.resume() | ||
} | ||
|
||
func suspend() { | ||
if state == .suspended { | ||
return | ||
} | ||
state = .suspended | ||
timer.suspend() | ||
} | ||
} |
Oops, something went wrong.