Skip to content

Commit a50a942

Browse files
committed
Convert RunLoopMode to RunLoop.Mode as with Darwin Foundation
1 parent 12ee2c8 commit a50a942

9 files changed

+58
-65
lines changed

Foundation/FileHandle.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -330,31 +330,31 @@ public let NSFileHandleNotificationDataItem: String = "NSFileHandleNotificationD
330330
public let NSFileHandleNotificationFileHandleItem: String = "NSFileHandleNotificationFileHandleItem"
331331

332332
extension FileHandle {
333-
open func readInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
333+
open func readInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
334334
NSUnimplemented()
335335
}
336336

337337
open func readInBackgroundAndNotify() {
338338
NSUnimplemented()
339339
}
340340

341-
open func readToEndOfFileInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
341+
open func readToEndOfFileInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
342342
NSUnimplemented()
343343
}
344344

345345
open func readToEndOfFileInBackgroundAndNotify() {
346346
NSUnimplemented()
347347
}
348348

349-
open func acceptConnectionInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
349+
open func acceptConnectionInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
350350
NSUnimplemented()
351351
}
352352

353353
open func acceptConnectionInBackgroundAndNotify() {
354354
NSUnimplemented()
355355
}
356356

357-
open func waitForDataInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
357+
open func waitForDataInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
358358
NSUnimplemented()
359359
}
360360

Foundation/NotificationQueue.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension NotificationQueue {
3030
open class NotificationQueue: NSObject {
3131

3232
internal typealias NotificationQueueList = NSMutableArray
33-
internal typealias NSNotificationListEntry = (Notification, [RunLoopMode]) // Notification ans list of modes the notification may be posted in.
33+
internal typealias NSNotificationListEntry = (Notification, [RunLoop.Mode]) // Notification ans list of modes the notification may be posted in.
3434
internal typealias NSNotificationList = [NSNotificationListEntry] // The list of notifications to post
3535

3636
internal let notificationCenter: NotificationCenter
@@ -80,8 +80,8 @@ open class NotificationQueue: NSObject {
8080
enqueue(notification, postingStyle: postingStyle, coalesceMask: [.onName, .onSender], forModes: nil)
8181
}
8282

83-
open func enqueue(_ notification: Notification, postingStyle: PostingStyle, coalesceMask: NotificationCoalescing, forModes modes: [RunLoopMode]?) {
84-
var runloopModes: [RunLoopMode] = [.defaultRunLoopMode]
83+
open func enqueue(_ notification: Notification, postingStyle: PostingStyle, coalesceMask: NotificationCoalescing, forModes modes: [RunLoop.Mode]?) {
84+
var runloopModes: [RunLoop.Mode] = [.default]
8585
if let modes = modes {
8686
runloopModes = modes
8787
}
@@ -140,7 +140,7 @@ open class NotificationQueue: NSObject {
140140
CFRunLoopRemoveObserver(RunLoop.current._cfRunLoop, observer, kCFRunLoopCommonModes)
141141
}
142142

143-
private func notify(_ currentMode: RunLoopMode?, notificationList: inout NSNotificationList) {
143+
private func notify(_ currentMode: RunLoop.Mode?, notificationList: inout NSNotificationList) {
144144
for (idx, (notification, modes)) in notificationList.enumerated().reversed() {
145145
if currentMode == nil || modes.contains(currentMode!) {
146146
self.notificationCenter.post(notification)

Foundation/Port.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ open class Port : NSObject, NSCopying, NSCoding {
5151
// to setup monitoring of the port when added to a run loop,
5252
// and stop monitoring if needed when removed;
5353
// These methods should not be called directly!
54-
open func schedule(in runLoop: RunLoop, forMode mode: RunLoopMode) {
54+
open func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) {
5555
NSUnimplemented()
5656
}
5757

58-
open func remove(from runLoop: RunLoop, forMode mode: RunLoopMode) {
58+
open func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) {
5959
NSUnimplemented()
6060
}
6161

Foundation/Process.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ open class Process: NSObject {
591591

592592
repeat {
593593

594-
} while( self.isRunning == true && RunLoop.current.run(mode: .defaultRunLoopMode, before: Date(timeIntervalSinceNow: 0.05)) )
594+
} while( self.isRunning == true && RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: 0.05)) )
595595

596596
self.runLoop = nil
597597
}

Foundation/RunLoop.swift

+26-33
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,29 @@ import CoreFoundation
1919
internal let kCFRunLoopAllActivities = CFRunLoopActivity.allActivities.rawValue
2020
#endif
2121

22-
public struct RunLoopMode : RawRepresentable, Equatable, Hashable {
23-
public private(set) var rawValue: String
24-
25-
public init(_ rawValue: String) {
26-
self.rawValue = rawValue
27-
}
28-
29-
public init(rawValue: String) {
30-
self.rawValue = rawValue
31-
}
32-
33-
public var hashValue: Int {
34-
return rawValue.hashValue
35-
}
22+
extension RunLoop {
23+
public struct Mode : RawRepresentable, Equatable, Hashable {
24+
public private(set) var rawValue: String
25+
26+
public init(_ rawValue: String) {
27+
self.rawValue = rawValue
28+
}
3629

37-
public static func ==(_ lhs: RunLoopMode, _ rhs: RunLoopMode) -> Bool {
38-
return lhs.rawValue == rhs.rawValue
30+
public init(rawValue: String) {
31+
self.rawValue = rawValue
32+
}
3933
}
4034
}
4135

42-
43-
extension RunLoopMode {
44-
public static let defaultRunLoopMode = RunLoopMode("kCFRunLoopDefaultMode")
45-
public static let commonModes = RunLoopMode("kCFRunLoopCommonModes")
36+
extension RunLoop.Mode {
37+
public static let `default`: RunLoop.Mode = RunLoop.Mode("kCFRunLoopDefaultMode")
38+
public static let common: RunLoop.Mode = RunLoop.Mode("kCFRunLoopCommonModes")
4639

4740
// Use this instead of .rawValue._cfObject; this will allow CFRunLoop to use pointer equality internally.
4841
fileprivate var _cfStringUniquingKnown: CFString {
49-
if self == .defaultRunLoopMode {
42+
if self == .default {
5043
return kCFRunLoopDefaultMode
51-
} else if self == .commonModes {
44+
} else if self == .common {
5245
return kCFRunLoopCommonModes
5346
} else {
5447
return rawValue._cfObject
@@ -79,9 +72,9 @@ open class RunLoop: NSObject {
7972
return _CFRunLoopGet2(CFRunLoopGetMain()) as! RunLoop
8073
}
8174

82-
open var currentMode: RunLoopMode? {
75+
open var currentMode: RunLoop.Mode? {
8376
if let mode = CFRunLoopCopyCurrentMode(_cfRunLoop) {
84-
return RunLoopMode(mode._swiftObject)
77+
return RunLoop.Mode(mode._swiftObject)
8578
} else {
8679
return nil
8780
}
@@ -91,19 +84,19 @@ open class RunLoop: NSObject {
9184
return _cfRunLoop
9285
}
9386

94-
open func add(_ timer: Timer, forMode mode: RunLoopMode) {
87+
open func add(_ timer: Timer, forMode mode: RunLoop.Mode) {
9588
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode._cfStringUniquingKnown)
9689
}
9790

98-
open func add(_ aPort: Port, forMode mode: RunLoopMode) {
91+
open func add(_ aPort: Port, forMode mode: RunLoop.Mode) {
9992
NSUnimplemented()
10093
}
10194

102-
open func remove(_ aPort: Port, forMode mode: RunLoopMode) {
95+
open func remove(_ aPort: Port, forMode mode: RunLoop.Mode) {
10396
NSUnimplemented()
10497
}
10598

106-
open func limitDate(forMode mode: RunLoopMode) -> Date? {
99+
open func limitDate(forMode mode: RunLoop.Mode) -> Date? {
107100
if _cfRunLoop !== CFRunLoopGetCurrent() {
108101
return nil
109102
}
@@ -135,14 +128,14 @@ open class RunLoop: NSObject {
135128
extension RunLoop {
136129

137130
public func run() {
138-
while run(mode: .defaultRunLoopMode, before: Date.distantFuture) { }
131+
while run(mode: .default, before: Date.distantFuture) { }
139132
}
140133

141134
public func run(until limitDate: Date) {
142-
while run(mode: .defaultRunLoopMode, before: limitDate) && limitDate.timeIntervalSinceReferenceDate > CFAbsoluteTimeGetCurrent() { }
135+
while run(mode: .default, before: limitDate) && limitDate.timeIntervalSinceReferenceDate > CFAbsoluteTimeGetCurrent() { }
143136
}
144137

145-
public func run(mode: RunLoopMode, before limitDate: Date) -> Bool {
138+
public func run(mode: RunLoop.Mode, before limitDate: Date) -> Bool {
146139
if _cfRunLoop !== CFRunLoopGetCurrent() {
147140
return false
148141
}
@@ -157,11 +150,11 @@ extension RunLoop {
157150
return true
158151
}
159152

160-
public func perform(inModes modes: [RunLoopMode], block: @escaping () -> Void) {
153+
public func perform(inModes modes: [RunLoop.Mode], block: @escaping () -> Void) {
161154
CFRunLoopPerformBlock(getCFRunLoop(), (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
162155
}
163156

164157
public func perform(_ block: @escaping () -> Void) {
165-
perform(inModes: [.defaultRunLoopMode], block: block)
158+
perform(inModes: [.default], block: block)
166159
}
167160
}

Foundation/Stream.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ open class Stream: NSObject {
9494

9595
// Re-enable once run loop is compiled on all platforms
9696

97-
open func schedule(in aRunLoop: RunLoop, forMode mode: RunLoopMode) {
97+
open func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
9898
NSUnimplemented()
9999
}
100100

101-
open func remove(from aRunLoop: RunLoop, forMode mode: RunLoopMode) {
101+
open func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
102102
NSUnimplemented()
103103
}
104104

TestFoundation/TestNotificationQueue.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ FIXME SR-4280 timeouts in TestNSNotificationQueue tests
103103
return
104104
}
105105

106-
// post 2 notifications for the RunLoopMode.defaultRunLoopMode mode
106+
// post 2 notifications for the RunLoop.Mode.default mode
107107
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [runLoopMode])
108108
queue.enqueue(notification, postingStyle: .now)
109-
// here we post notification for the RunLoopMode.commonModes. It shouldn't have any affect, because the timer is scheduled in RunLoopMode.defaultRunLoopMode.
109+
// here we post notification for the RunLoop.Mode.common. It shouldn't have any affect, because the timer is scheduled in RunLoop.Mode.default.
110110
// The notification queue will only post the notification to its notification center if the run loop is in one of the modes provided in the array.
111-
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [.commonModes])
111+
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [.common])
112112
}
113-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
114-
let _ = runLoop.run(mode: .defaultRunLoopMode, before: endDate)
113+
runLoop.add(dummyTimer, forMode: .default)
114+
let _ = runLoop.run(mode: .default, before: endDate)
115115
XCTAssertEqual(numberOfCalls, 2)
116116
NotificationCenter.default.removeObserver(obs)
117117
}
@@ -219,7 +219,7 @@ FIXME SR-4280 timeouts in TestNSNotificationQueue tests
219219
let dummyTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { _ in
220220
e.fulfill()
221221
}
222-
RunLoop.current.add(dummyTimer, forMode: .defaultRunLoopMode)
222+
RunLoop.current.add(dummyTimer, forMode: .default)
223223
waitForExpectations(timeout: 0.1)
224224
}
225225

TestFoundation/TestRunLoop.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class TestRunLoop : XCTestCase {
2020
}
2121

2222
func test_constants() {
23-
XCTAssertEqual(RunLoopMode.commonModes.rawValue, "kCFRunLoopCommonModes",
24-
"\(RunLoopMode.commonModes.rawValue) is not equal to kCFRunLoopCommonModes")
23+
XCTAssertEqual(RunLoop.Mode.common.rawValue, "kCFRunLoopCommonModes",
24+
"\(RunLoop.Mode.common.rawValue) is not equal to kCFRunLoopCommonModes")
2525

26-
XCTAssertEqual(RunLoopMode.defaultRunLoopMode.rawValue, "kCFRunLoopDefaultMode",
27-
"\(RunLoopMode.defaultRunLoopMode.rawValue) is not equal to kCFRunLoopDefaultMode")
26+
XCTAssertEqual(RunLoop.Mode.default.rawValue, "kCFRunLoopDefaultMode",
27+
"\(RunLoop.Mode.default.rawValue) is not equal to kCFRunLoopDefaultMode")
2828
}
2929

3030
func test_runLoopInit() {
@@ -57,10 +57,10 @@ class TestRunLoop : XCTestCase {
5757
return
5858
}
5959

60-
XCTAssertEqual(runLoopMode, RunLoopMode.defaultRunLoopMode)
60+
XCTAssertEqual(runLoopMode, RunLoop.Mode.default)
6161
}
62-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
63-
let result = runLoop.run(mode: .defaultRunLoopMode, before: endDate)
62+
runLoop.add(dummyTimer, forMode: .default)
63+
let result = runLoop.run(mode: .default, before: endDate)
6464

6565
XCTAssertFalse(result) // should be .Finished
6666
XCTAssertTrue(flag)
@@ -72,9 +72,9 @@ class TestRunLoop : XCTestCase {
7272
let expectedTimeInterval = Date(timeInterval: timeInterval, since: Date()).timeIntervalSince1970
7373

7474
let dummyTimer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false) { _ in }
75-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
75+
runLoop.add(dummyTimer, forMode: .default)
7676

77-
guard let timerTickInterval = runLoop.limitDate(forMode: .defaultRunLoopMode)?.timeIntervalSince1970 else {
77+
guard let timerTickInterval = runLoop.limitDate(forMode: .default)?.timeIntervalSince1970 else {
7878
return
7979
}
8080

@@ -88,7 +88,7 @@ class TestRunLoop : XCTestCase {
8888
done.fulfill()
8989
}
9090

91-
runLoop.add(timer, forMode: .commonModes)
91+
runLoop.add(timer, forMode: .common)
9292

9393
waitForExpectations(timeout: 10)
9494
}

TestFoundation/TestTimer.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TestTimer : XCTestCase {
4343
}
4444

4545
let runLoop = RunLoop.current
46-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
46+
runLoop.add(dummyTimer, forMode: .default)
4747
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))
4848

4949
XCTAssertTrue(flag)
@@ -69,7 +69,7 @@ class TestTimer : XCTestCase {
6969
}
7070

7171
let runLoop = RunLoop.current
72-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
72+
runLoop.add(dummyTimer, forMode: .default)
7373
runLoop.run(until: Date(timeIntervalSinceNow: interval * Double(numberOfRepeats + 1)))
7474

7575
XCTAssertEqual(flag, numberOfRepeats)
@@ -89,7 +89,7 @@ class TestTimer : XCTestCase {
8989
}
9090

9191
let runLoop = RunLoop.current
92-
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
92+
runLoop.add(dummyTimer, forMode: .default)
9393
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))
9494

9595
XCTAssertTrue(flag)

0 commit comments

Comments
 (0)