Skip to content

Commit 84de7db

Browse files
committed
Deprecate conf.Q, add default dispatcher setter methods
1 parent a8f2e9b commit 84de7db

1 file changed

Lines changed: 65 additions & 9 deletions

File tree

Sources/Configuration.swift

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
import Dispatch
22

3-
/**
4-
PromiseKit’s configurable parameters.
3+
// PromiseKit’s configurable parameters.
54

6-
Do not change these after any Promise machinery executes as the configuration object is not thread-safe.
5+
public struct NoValue: Dispatcher {
6+
public init() {}
7+
public func dispatch(_ body: @escaping () -> Void) {
8+
fatalError("NoValue dispatcher should never actually be used as a dispatcher")
9+
}
10+
}
711

8-
We would like it to be, but sadly `Swift` does not expose `dispatch_once` et al. which is what we used to use in order to make the configuration immutable once first used.
9-
*/
1012
public struct PMKConfiguration {
13+
14+
public var requireChainConfirmation = true
15+
1116
/// Backward compatibility: the default Dispatcher to which handlers dispatch, represented as DispatchQueues.
17+
@available(*, deprecated, message: "Use conf.setDefaultDispatchers(body:tail:) to set default dispatchers in PromiseKit 7+")
1218
public var Q: (map: DispatchQueue?, return: DispatchQueue?) {
1319
get {
14-
let convertedMap = D.map is CurrentThreadDispatcher ? nil : D.map as? DispatchQueue
15-
let convertedReturn = D.return is CurrentThreadDispatcher ? nil : D.return as? DispatchQueue
20+
let convertedMap = _D.body is CurrentThreadDispatcher ? nil : _D.body as? DispatchQueue
21+
let convertedReturn = _D.tail is CurrentThreadDispatcher ? nil : _D.tail as? DispatchQueue
1622
return (map: convertedMap, return: convertedReturn)
1723
}
18-
set { D = (map: newValue.map ?? CurrentThreadDispatcher(), return: newValue.return ?? CurrentThreadDispatcher()) }
24+
set {
25+
verifyDUnread()
26+
_D = (body: newValue.map ?? CurrentThreadDispatcher(), tail: newValue.return ?? CurrentThreadDispatcher())
27+
}
1928
}
2029

2130
/// The default Dispatchers to which promise handlers dispatch
22-
public var D: (map: Dispatcher, return: Dispatcher) = (map: DispatchQueue.main, return: DispatchQueue.main)
31+
private static let defaultBodyDispatcher = DispatchQueue.main
32+
private static let defaultTailDispatcher = DispatchQueue.main
33+
34+
internal var _D: (body: Dispatcher, tail: Dispatcher) = (body: DispatchQueue.main, tail: DispatchQueue.main)
35+
internal var D: (body: Dispatcher, tail: Dispatcher) {
36+
mutating get { dRead = true; return _D }
37+
set { _D = newValue }
38+
}
39+
40+
private var dRead = false
41+
internal var testMode = false
2342

2443
/// The default catch-policy for all `catch` and `resolve`
2544
public var catchPolicy = CatchPolicy.allErrorsExceptCancellation
@@ -30,6 +49,43 @@ public struct PMKConfiguration {
3049
public var logHandler: (LogEvent) -> () = { event in
3150
print(event.asString())
3251
}
52+
53+
private func verifyDUnread() {
54+
if dRead && !testMode {
55+
conf.logHandler(.defaultDispatchersReset)
56+
}
57+
}
58+
59+
mutating public func setDefaultDispatchers(body: Dispatcher = NoValue(), tail: Dispatcher = NoValue()) {
60+
verifyDUnread()
61+
if !(body is NoValue) { _D.body = body }
62+
if !(tail is NoValue) { _D.tail = tail }
63+
}
64+
65+
fileprivate func determineDispatcher(_ dispatcher: DispatchQueue?, default: Dispatcher) -> Dispatcher? {
66+
switch dispatcher {
67+
case nil:
68+
return CurrentThreadDispatcher()
69+
case DispatchQueue.unspecified:
70+
return nil // Do nothing
71+
case DispatchQueue.default:
72+
return `default`
73+
case DispatchQueue.chain:
74+
fatalError("PromiseKit: .chain is not meaningful in the context of setDefaultDispatchers")
75+
default:
76+
return dispatcher!
77+
}
78+
}
79+
80+
mutating public func setDefaultDispatchers(body: DispatchQueue? = .unspecified, tail: DispatchQueue? = .unspecified) {
81+
verifyDUnread()
82+
if let newBody = determineDispatcher(body, default: PMKConfiguration.defaultBodyDispatcher) {
83+
_D.body = newBody
84+
}
85+
if let newTail = determineDispatcher(tail, default: PMKConfiguration.defaultTailDispatcher) {
86+
_D.tail = newTail
87+
}
88+
}
3389
}
3490

3591
/// Modify this as soon as possible in your application’s lifetime

0 commit comments

Comments
 (0)