Skip to content

[5.0] Convert RunLoopMode to RunLoop.Mode as with Darwin Foundation #1854

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

Merged
Merged
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
8 changes: 4 additions & 4 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,31 +330,31 @@ public let NSFileHandleNotificationDataItem: String = "NSFileHandleNotificationD
public let NSFileHandleNotificationFileHandleItem: String = "NSFileHandleNotificationFileHandleItem"

extension FileHandle {
open func readInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
open func readInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
NSUnimplemented()
}

open func readInBackgroundAndNotify() {
NSUnimplemented()
}

open func readToEndOfFileInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
open func readToEndOfFileInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
NSUnimplemented()
}

open func readToEndOfFileInBackgroundAndNotify() {
NSUnimplemented()
}

open func acceptConnectionInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
open func acceptConnectionInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
NSUnimplemented()
}

open func acceptConnectionInBackgroundAndNotify() {
NSUnimplemented()
}

open func waitForDataInBackgroundAndNotify(forModes modes: [RunLoopMode]?) {
open func waitForDataInBackgroundAndNotify(forModes modes: [RunLoop.Mode]?) {
NSUnimplemented()
}

Expand Down
8 changes: 4 additions & 4 deletions Foundation/NotificationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension NotificationQueue {
open class NotificationQueue: NSObject {

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

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

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

private func notify(_ currentMode: RunLoopMode?, notificationList: inout NSNotificationList) {
private func notify(_ currentMode: RunLoop.Mode?, notificationList: inout NSNotificationList) {
for (idx, (notification, modes)) in notificationList.enumerated().reversed() {
if currentMode == nil || modes.contains(currentMode!) {
self.notificationCenter.post(notification)
Expand Down
4 changes: 2 additions & 2 deletions Foundation/Port.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ open class Port : NSObject, NSCopying, NSCoding {
// to setup monitoring of the port when added to a run loop,
// and stop monitoring if needed when removed;
// These methods should not be called directly!
open func schedule(in runLoop: RunLoop, forMode mode: RunLoopMode) {
open func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

open func remove(from runLoop: RunLoop, forMode mode: RunLoopMode) {
open func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ open class Process: NSObject {

repeat {

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

self.runLoop = nil
}
Expand Down
59 changes: 26 additions & 33 deletions Foundation/RunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,29 @@ import CoreFoundation
internal let kCFRunLoopAllActivities = CFRunLoopActivity.allActivities.rawValue
#endif

public struct RunLoopMode : RawRepresentable, Equatable, Hashable {
public private(set) var rawValue: String

public init(_ rawValue: String) {
self.rawValue = rawValue
}

public init(rawValue: String) {
self.rawValue = rawValue
}

public var hashValue: Int {
return rawValue.hashValue
}
extension RunLoop {
public struct Mode : RawRepresentable, Equatable, Hashable {
public private(set) var rawValue: String

public init(_ rawValue: String) {
self.rawValue = rawValue
}

public static func ==(_ lhs: RunLoopMode, _ rhs: RunLoopMode) -> Bool {
return lhs.rawValue == rhs.rawValue
public init(rawValue: String) {
self.rawValue = rawValue
}
}
}


extension RunLoopMode {
public static let defaultRunLoopMode = RunLoopMode("kCFRunLoopDefaultMode")
public static let commonModes = RunLoopMode("kCFRunLoopCommonModes")
extension RunLoop.Mode {
public static let `default`: RunLoop.Mode = RunLoop.Mode("kCFRunLoopDefaultMode")
public static let common: RunLoop.Mode = RunLoop.Mode("kCFRunLoopCommonModes")

// Use this instead of .rawValue._cfObject; this will allow CFRunLoop to use pointer equality internally.
fileprivate var _cfStringUniquingKnown: CFString {
if self == .defaultRunLoopMode {
if self == .default {
return kCFRunLoopDefaultMode
} else if self == .commonModes {
} else if self == .common {
return kCFRunLoopCommonModes
} else {
return rawValue._cfObject
Expand Down Expand Up @@ -79,9 +72,9 @@ open class RunLoop: NSObject {
return _CFRunLoopGet2(CFRunLoopGetMain()) as! RunLoop
}

open var currentMode: RunLoopMode? {
open var currentMode: RunLoop.Mode? {
if let mode = CFRunLoopCopyCurrentMode(_cfRunLoop) {
return RunLoopMode(mode._swiftObject)
return RunLoop.Mode(mode._swiftObject)
} else {
return nil
}
Expand All @@ -91,19 +84,19 @@ open class RunLoop: NSObject {
return _cfRunLoop
}

open func add(_ timer: Timer, forMode mode: RunLoopMode) {
open func add(_ timer: Timer, forMode mode: RunLoop.Mode) {
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode._cfStringUniquingKnown)
}

open func add(_ aPort: Port, forMode mode: RunLoopMode) {
open func add(_ aPort: Port, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

open func remove(_ aPort: Port, forMode mode: RunLoopMode) {
open func remove(_ aPort: Port, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

open func limitDate(forMode mode: RunLoopMode) -> Date? {
open func limitDate(forMode mode: RunLoop.Mode) -> Date? {
if _cfRunLoop !== CFRunLoopGetCurrent() {
return nil
}
Expand Down Expand Up @@ -135,14 +128,14 @@ open class RunLoop: NSObject {
extension RunLoop {

public func run() {
while run(mode: .defaultRunLoopMode, before: Date.distantFuture) { }
while run(mode: .default, before: Date.distantFuture) { }
}

public func run(until limitDate: Date) {
while run(mode: .defaultRunLoopMode, before: limitDate) && limitDate.timeIntervalSinceReferenceDate > CFAbsoluteTimeGetCurrent() { }
while run(mode: .default, before: limitDate) && limitDate.timeIntervalSinceReferenceDate > CFAbsoluteTimeGetCurrent() { }
}

public func run(mode: RunLoopMode, before limitDate: Date) -> Bool {
public func run(mode: RunLoop.Mode, before limitDate: Date) -> Bool {
if _cfRunLoop !== CFRunLoopGetCurrent() {
return false
}
Expand All @@ -157,11 +150,11 @@ extension RunLoop {
return true
}

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

public func perform(_ block: @escaping () -> Void) {
perform(inModes: [.defaultRunLoopMode], block: block)
perform(inModes: [.default], block: block)
}
}
4 changes: 2 additions & 2 deletions Foundation/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ open class Stream: NSObject {

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

open func schedule(in aRunLoop: RunLoop, forMode mode: RunLoopMode) {
open func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

open func remove(from aRunLoop: RunLoop, forMode mode: RunLoopMode) {
open func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSUnimplemented()
}

Expand Down
12 changes: 6 additions & 6 deletions TestFoundation/TestNotificationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ FIXME SR-4280 timeouts in TestNSNotificationQueue tests
return
}

// post 2 notifications for the RunLoopMode.defaultRunLoopMode mode
// post 2 notifications for the RunLoop.Mode.default mode
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [runLoopMode])
queue.enqueue(notification, postingStyle: .now)
// here we post notification for the RunLoopMode.commonModes. It shouldn't have any affect, because the timer is scheduled in RunLoopMode.defaultRunLoopMode.
// here we post notification for the RunLoop.Mode.common. It shouldn't have any affect, because the timer is scheduled in RunLoop.Mode.default.
// 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.
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [.commonModes])
queue.enqueue(notification, postingStyle: .now, coalesceMask: [], forModes: [.common])
}
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
let _ = runLoop.run(mode: .defaultRunLoopMode, before: endDate)
runLoop.add(dummyTimer, forMode: .default)
let _ = runLoop.run(mode: .default, before: endDate)
XCTAssertEqual(numberOfCalls, 2)
NotificationCenter.default.removeObserver(obs)
}
Expand Down Expand Up @@ -219,7 +219,7 @@ FIXME SR-4280 timeouts in TestNSNotificationQueue tests
let dummyTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { _ in
e.fulfill()
}
RunLoop.current.add(dummyTimer, forMode: .defaultRunLoopMode)
RunLoop.current.add(dummyTimer, forMode: .default)
waitForExpectations(timeout: 0.1)
}

Expand Down
20 changes: 10 additions & 10 deletions TestFoundation/TestRunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class TestRunLoop : XCTestCase {
}

func test_constants() {
XCTAssertEqual(RunLoopMode.commonModes.rawValue, "kCFRunLoopCommonModes",
"\(RunLoopMode.commonModes.rawValue) is not equal to kCFRunLoopCommonModes")
XCTAssertEqual(RunLoop.Mode.common.rawValue, "kCFRunLoopCommonModes",
"\(RunLoop.Mode.common.rawValue) is not equal to kCFRunLoopCommonModes")

XCTAssertEqual(RunLoopMode.defaultRunLoopMode.rawValue, "kCFRunLoopDefaultMode",
"\(RunLoopMode.defaultRunLoopMode.rawValue) is not equal to kCFRunLoopDefaultMode")
XCTAssertEqual(RunLoop.Mode.default.rawValue, "kCFRunLoopDefaultMode",
"\(RunLoop.Mode.default.rawValue) is not equal to kCFRunLoopDefaultMode")
}

func test_runLoopInit() {
Expand Down Expand Up @@ -57,10 +57,10 @@ class TestRunLoop : XCTestCase {
return
}

XCTAssertEqual(runLoopMode, RunLoopMode.defaultRunLoopMode)
XCTAssertEqual(runLoopMode, RunLoop.Mode.default)
}
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
let result = runLoop.run(mode: .defaultRunLoopMode, before: endDate)
runLoop.add(dummyTimer, forMode: .default)
let result = runLoop.run(mode: .default, before: endDate)

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

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

guard let timerTickInterval = runLoop.limitDate(forMode: .defaultRunLoopMode)?.timeIntervalSince1970 else {
guard let timerTickInterval = runLoop.limitDate(forMode: .default)?.timeIntervalSince1970 else {
return
}

Expand All @@ -88,7 +88,7 @@ class TestRunLoop : XCTestCase {
done.fulfill()
}

runLoop.add(timer, forMode: .commonModes)
runLoop.add(timer, forMode: .common)

waitForExpectations(timeout: 10)
}
Expand Down
6 changes: 3 additions & 3 deletions TestFoundation/TestTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TestTimer : XCTestCase {
}

let runLoop = RunLoop.current
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
runLoop.add(dummyTimer, forMode: .default)
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))

XCTAssertTrue(flag)
Expand All @@ -69,7 +69,7 @@ class TestTimer : XCTestCase {
}

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

XCTAssertEqual(flag, numberOfRepeats)
Expand All @@ -89,7 +89,7 @@ class TestTimer : XCTestCase {
}

let runLoop = RunLoop.current
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
runLoop.add(dummyTimer, forMode: .default)
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))

XCTAssertTrue(flag)
Expand Down