-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathDisplayLink.swift
44 lines (36 loc) · 1.07 KB
/
DisplayLink.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// DisplayLink.swift
// UIKit
//
// Created by Geordie Jay on 25.05.17.
// Copyright © 2017 flowkey. All rights reserved.
//
open class DisplayLink {
static var activeDisplayLinks: Set<DisplayLink> = []
public init() {}
public var isPaused = true {
didSet { updateActiveDisplayLinks() }
}
// You'd have to call displayLink.callback() yourself to crash the program by this being nil
public var callback: (() -> Void)! {
didSet { updateActiveDisplayLinks() }
}
private func updateActiveDisplayLinks() {
if isPaused || callback == nil {
DisplayLink.activeDisplayLinks.remove(self)
} else {
DisplayLink.activeDisplayLinks.insert(self)
}
}
public func invalidate() {
callback = nil
}
}
extension DisplayLink: Hashable {
nonisolated public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self).hashValue)
}
nonisolated public static func ==(lhs: DisplayLink, rhs: DisplayLink) -> Bool {
return lhs === rhs
}
}