Skip to content

Commit ac69d6c

Browse files
committed
[PlaygroundSupport] Cleaned up a few things in PlaygroundPage.swift.
1 parent 993610f commit ac69d6c

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

PlaygroundSupport/PlaygroundSupport/PlaygroundPage.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -12,7 +12,7 @@
1212

1313
#if os(iOS) || os(tvOS)
1414
import UIKit
15-
#elseif os(OSX)
15+
#elseif os(macOS)
1616
import AppKit
1717
#endif
1818

@@ -50,7 +50,7 @@ public final class PlaygroundPage {
5050
///
5151
public var needsIndefiniteExecution: Bool = false {
5252
didSet {
53-
NotificationCenter.default.post(name: "PlaygroundPageNeedsIndefiniteExecutionDidChangeNotification" as NSString as NSNotification.Name, object: self, userInfo: [ "PlaygroundPageNeedsIndefiniteExecution" : needsIndefiniteExecution as AnyObject])
53+
NotificationCenter.default.post(name: .playgroundPageNeedsIndefiniteExecutionDidChange, object: self, userInfo: ["PlaygroundPageNeedsIndefiniteExecution": needsIndefiniteExecution])
5454
}
5555
}
5656

@@ -59,7 +59,7 @@ public final class PlaygroundPage {
5959
/// This method does not return, as Xcode will kill the process hosting playground execution when this method is called.
6060
public func finishExecution() -> Never {
6161
// Send a message to Xcode requesting that we be killed.
62-
NotificationCenter.default.post(name: "PlaygroundPageFinishExecutionNotification" as NSString as NSNotification.Name, object: self, userInfo: nil)
62+
NotificationCenter.default.post(name: .playgroundPageIsReadyToFinishExecution, object: self, userInfo: nil)
6363

6464
// Sleep for a while to let Xcode kill us.
6565
for _ in 1...10 {
@@ -78,6 +78,9 @@ public final class PlaygroundPage {
7878
/// - note: This is nil by default.
7979
public var liveView: PlaygroundLiveViewable? = nil {
8080
didSet {
81+
// Don't do anything if we just went from nil to nil.
82+
guard !(liveView == nil && oldValue == nil) else { return }
83+
8184
if liveView != nil {
8285
// Setting a live view enables implies a need for indefinite execution.
8386
needsIndefiniteExecution = true
@@ -88,21 +91,16 @@ public final class PlaygroundPage {
8891
if let liveView = liveView {
8992
switch liveView.playgroundLiveViewRepresentation {
9093
case .viewController(let viewController):
91-
userInfo = ["PlaygroundPageLiveViewController" : viewController]
94+
userInfo = ["PlaygroundPageLiveViewController": viewController]
9295
case .view(let view):
93-
userInfo = ["PlaygroundPageLiveView" : view]
96+
userInfo = ["PlaygroundPageLiveView": view]
9497
}
9598
}
9699
else {
97-
if oldValue == nil {
98-
// Don't send a notification if we just went from nil to nil.
99-
return
100-
}
101-
102100
userInfo = [:]
103101
}
104102

105-
NotificationCenter.default.post(name: "PlaygroundPageLiveViewDidChangeNotification" as NSString as NSNotification.Name, object: self, userInfo: userInfo)
103+
NotificationCenter.default.post(name: .playgroundPageLiveViewDidChange, object: self, userInfo: userInfo)
106104
}
107105
}
108106
}
@@ -120,7 +118,7 @@ public enum PlaygroundLiveViewRepresentation {
120118
/// - note: This view controller must be the root of a view controller hierarchy (i.e. it has no parent view controller), and its view must *not* have a superview.
121119
case viewController(UIViewController)
122120

123-
#elseif os(OSX)
121+
#elseif os(macOS)
124122
/// A view which will be displayed as the live view.
125123
///
126124
/// - note: This view must be the root of a view hierarchy (i.e. it must not have a superview), and it must *not* be owned by a view controller.
@@ -137,7 +135,7 @@ public enum PlaygroundLiveViewRepresentation {
137135
/// A protocol for types which can be displayed as the live view for a playground page.
138136
///
139137
/// On iOS and tvOS, `UIView` and `UIViewController` conform to this protocol.
140-
/// Likewise, on OS X, `NSView` and `NSViewController` conform to this protocol.
138+
/// Likewise, on macOS, `NSView` and `NSViewController` conform to this protocol.
141139
///
142140
/// Implement this protocol if your custom type should be usable as the "live view" for a playground page.
143141
public protocol PlaygroundLiveViewable {
@@ -165,7 +163,7 @@ extension UIViewController: PlaygroundLiveViewable {
165163
}
166164
}
167165
}
168-
#elseif os(OSX)
166+
#elseif os(macOS)
169167
extension NSView: PlaygroundLiveViewable {
170168
public var playgroundLiveViewRepresentation: PlaygroundLiveViewRepresentation {
171169
get {
@@ -182,3 +180,15 @@ extension NSViewController: PlaygroundLiveViewable {
182180
}
183181
}
184182
#endif
183+
184+
/// A collection of Notification.Name values used internally by PlaygroundPage.
185+
private extension Notification.Name {
186+
/// Indicates that `PlaygroundPage.needsIndefiniteExecution` did change.
187+
static let playgroundPageNeedsIndefiniteExecutionDidChange = Notification.Name(rawValue: "PlaygroundPageNeedsIndefiniteExecutionDidChangeNotification")
188+
189+
/// Indicates that `PlaygroundPage.finishExecution()` was called.
190+
static let playgroundPageIsReadyToFinishExecution = Notification.Name(rawValue: "PlaygroundPageFinishExecutionNotification")
191+
192+
/// Indicates that `PlaygroundPage.liveView` did change.
193+
static let playgroundPageLiveViewDidChange = Notification.Name(rawValue: "PlaygroundPageLiveViewDidChangeNotification")
194+
}

0 commit comments

Comments
 (0)