|
11 | 11 | @propertyWrapper
|
12 | 12 | public struct Published<Value> {
|
13 | 13 |
|
14 |
| - private var value: Value |
15 |
| - private var publisher: Publisher? |
| 14 | + private let publisher: Publisher |
16 | 15 | private let willChangeSubject = PassthroughSubject<Void, Never>()
|
17 | 16 |
|
18 | 17 | public init(wrappedValue: Value) {
|
19 |
| - value = wrappedValue |
| 18 | + publisher = Publisher(wrappedValue) |
20 | 19 | }
|
21 | 20 |
|
22 | 21 | /// A publisher for properties used with the `@Published` attribute.
|
23 | 22 | public struct Publisher: SignalProtocol {
|
24 | 23 | public typealias Element = Value
|
25 | 24 | public typealias Error = Never
|
26 | 25 |
|
27 |
| - fileprivate let didChangeSubject: ReplayOneSubject<Value, Never> |
| 26 | + fileprivate let property: Property<Value> |
28 | 27 |
|
29 | 28 | public func observe(with observer: @escaping (Signal<Value, Never>.Event) -> Void) -> Disposable {
|
30 |
| - self.didChangeSubject.observe(with: observer) |
| 29 | + self.property.observe(with: observer) |
31 | 30 | }
|
32 | 31 |
|
33 | 32 | fileprivate init(_ output: Element) {
|
34 |
| - self.didChangeSubject = ReplayOneSubject() |
35 |
| - self.didChangeSubject.send(output) |
| 33 | + self.property = Property(output) |
36 | 34 | }
|
37 | 35 | }
|
38 | 36 |
|
39 | 37 | public var wrappedValue: Value {
|
40 |
| - get { self.value } |
41 |
| - set { |
| 38 | + get { self.publisher.property.value } |
| 39 | + nonmutating set { |
42 | 40 | self.willChangeSubject.send()
|
43 |
| - self.value = newValue |
44 |
| - self.publisher?.didChangeSubject.send(newValue) |
| 41 | + self.publisher.property.value = newValue |
45 | 42 | }
|
46 | 43 | }
|
47 | 44 |
|
48 | 45 | public var projectedValue: Publisher {
|
49 |
| - mutating get { |
50 |
| - if let publisher = publisher { |
51 |
| - return publisher |
52 |
| - } |
53 |
| - let publisher = Publisher(value) |
54 |
| - self.publisher = publisher |
55 |
| - return publisher |
56 |
| - } |
| 46 | + get { publisher } |
57 | 47 | }
|
58 | 48 | }
|
59 | 49 |
|
|
0 commit comments