|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// ``ObservableObject`` values nested within an ``ObservableObject`` object |
| 4 | +/// will only have their changes published by the parent ``ObservableObject`` |
| 5 | +/// if marked with this marker protocol. This avoids uncertainty around which |
| 6 | +/// properties will or will not have their changes published by the parent. |
| 7 | +/// For clarity reasons, you shouldn't conform your own types to this protocol. |
| 8 | +/// Instead, apply the ``Published`` property wrapper when needed. |
| 9 | +/// |
| 10 | +/// ```swift |
| 11 | +/// // The following example highlights why the marker protocol exists. |
| 12 | +/// |
| 13 | +/// class MyNestedState: ObservableObject { |
| 14 | +/// @Published var count = 0 |
| 15 | +/// } |
| 16 | +/// |
| 17 | +/// class MyState: ObservableObject { |
| 18 | +/// // Without the marker protocol mechanism in place, `nested` would get |
| 19 | +/// // published as well as `index`. However, that would not be possible to |
| 20 | +/// // know without looking at the definition to check if `MyNestedState` |
| 21 | +/// // is `ObservableObject`. Because of the marker protocol, it is required |
| 22 | +/// // that both properties are annotated with `@Published` (which conforms |
| 23 | +/// // to the marker protocol). |
| 24 | +/// var nested = MyNestedState() |
| 25 | +/// @Published var index = 0 |
| 26 | +/// } |
| 27 | +/// ``` |
| 28 | +/// |
| 29 | +public protocol PublishedMarkerProtocol {} |
| 30 | + |
| 31 | +/// A wrapper which publishes a change whenever the wrapped value is set. If |
| 32 | +/// the wrapped value is ``ObservableObject``, its `didChange` publisher will |
| 33 | +/// also be forwarded to the wrapper's publisher. |
| 34 | +/// |
| 35 | +/// A compile time warning is emitted if the wrapper is applied to a class |
| 36 | +/// which isn't ``ObservableObject`` because this is considered undesired |
| 37 | +/// behaviour. Only replacing the value with a new instance of the class would |
| 38 | +/// cause a change to be published; changing the class' properties would not. |
| 39 | +/// The warning will show up as a deprecation, but it isn't (as you could guess |
| 40 | +/// from the accompanying message). |
| 41 | +@propertyWrapper |
| 42 | +public final class Published<Value>: ObservableObject, PublishedMarkerProtocol { |
| 43 | + /// A handle that can be used to cancel the link to the previous upstream publisher. |
| 44 | + private var upstreamLinkCancellable: Cancellable? |
| 45 | + |
| 46 | + /// A binding to the inner value. |
| 47 | + public var projectedValue: Binding<Value> { |
| 48 | + Binding( |
| 49 | + get: { |
| 50 | + self.wrappedValue |
| 51 | + }, |
| 52 | + set: { newValue in |
| 53 | + self.wrappedValue = newValue |
| 54 | + } |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + /// The underlying wrapped value. |
| 59 | + public var wrappedValue: Value { |
| 60 | + didSet { |
| 61 | + valueDidChange() |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// A publisher that publishes any observable changes made to |
| 66 | + /// ``Published/wrappedValue``. |
| 67 | + public let didChange = Publisher().tag(with: "Published") |
| 68 | + |
| 69 | + /// Creates a publishing wrapper around a value type or |
| 70 | + /// ``ObservableObject`` class. |
| 71 | + public init(wrappedValue: Value) { |
| 72 | + self.wrappedValue = wrappedValue |
| 73 | + valueDidChange(publish: false) |
| 74 | + } |
| 75 | + |
| 76 | + /// Creates a publishing wrapper around a value type or ``ObservableObject`` |
| 77 | + /// class. |
| 78 | + public init(wrappedValue: Value) where Value: AnyObject, Value: ObservableObject { |
| 79 | + // This initializer exists to redirect valid classes away from the initializer which |
| 80 | + // contains a compile time warning (through deprecation). |
| 81 | + self.wrappedValue = wrappedValue |
| 82 | + valueDidChange(publish: false) |
| 83 | + } |
| 84 | + |
| 85 | + /// Creates a wrapper around a non-ObservableObject class. Setting |
| 86 | + /// ``Published/wrappedValue`` to a new instance of the class is the only |
| 87 | + /// change that will get published. This is hardly ever intentional, so |
| 88 | + /// this initializer variant contains a deprecation warning to warn |
| 89 | + /// developers (but does nothing functionally different). |
| 90 | + @available( |
| 91 | + *, deprecated, |
| 92 | + message: "A class must conform to ObservableObject to be Published" |
| 93 | + ) |
| 94 | + public init(wrappedValue: Value) where Value: AnyObject { |
| 95 | + self.wrappedValue = wrappedValue |
| 96 | + valueDidChange(publish: false) |
| 97 | + } |
| 98 | + |
| 99 | + /// Handles changing a value. If `publish` is `false` the change won't be |
| 100 | + /// published, but if the wrapped value is ``ObservableObject`` the new |
| 101 | + /// upstream publisher will still get relinked. |
| 102 | + public func valueDidChange(publish: Bool = true) { |
| 103 | + if publish { |
| 104 | + didChange.send() |
| 105 | + } |
| 106 | + |
| 107 | + if let upstream = wrappedValue as? ObservableObject { |
| 108 | + upstreamLinkCancellable?.cancel() |
| 109 | + upstreamLinkCancellable = didChange.link(toUpstream: upstream.didChange) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +extension Published: Codable where Value: Codable { |
| 115 | + public convenience init(from decoder: Decoder) throws { |
| 116 | + self.init(wrappedValue: try Value(from: decoder)) |
| 117 | + } |
| 118 | + |
| 119 | + public func encode(to encoder: Encoder) throws { |
| 120 | + try wrappedValue.encode(to: encoder) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +@available(*, deprecated, message: "Replace Observed with Published") |
| 125 | +public typealias Observed = Published |
0 commit comments