Skip to content

Commit db0412a

Browse files
committed
Refactor
1 parent b3ff873 commit db0412a

File tree

16 files changed

+133
-41
lines changed

16 files changed

+133
-41
lines changed

Sources/SwiftUIX/Intermodular/Helpers/AppKit/_AppKitOrUIKitHostingPopover.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ open class NSHostingPopover<Content: View>: _AnyAppKitOrUIKitHostingPopover, NSP
361361
} else {
362362
let contentViewController = _contentViewController
363363

364-
self.objectWillChange.send()
364+
self._objectWillChange_send()
365365

366366
let content = contentViewController.mainView.content
367367

Sources/SwiftUIX/Intermodular/Helpers/Combine/AnyObservableValue.swift

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) Vatsal Manot
33
//
44

5+
import _SwiftUIX
56
import Combine
67
import Swift
78
import SwiftUI
@@ -10,6 +11,24 @@ import SwiftUI
1011
@dynamicMemberLookup
1112
@_documentation(visibility: internal)
1213
public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, ObservableObject {
14+
public struct Configuration {
15+
public var deferUpdates: Bool
16+
17+
public init(
18+
deferUpdates: Bool?
19+
) {
20+
self.deferUpdates = deferUpdates ?? false
21+
}
22+
23+
public init() {
24+
self.init(
25+
deferUpdates: nil
26+
)
27+
}
28+
}
29+
30+
public var configuration = Configuration()
31+
1332
public var wrappedValue: Value {
1433
get {
1534
fatalError() // abstract
@@ -18,10 +37,10 @@ public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, Observabl
1837
}
1938
}
2039

21-
internal init() {
22-
40+
init(configuration: Configuration) {
41+
self.configuration = configuration
2342
}
24-
43+
2544
public subscript<Subject>(
2645
dynamicMember keyPath: WritableKeyPath<Value, Subject>
2746
) -> AnyObservableValue<Subject> {
@@ -30,7 +49,7 @@ public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, Observabl
3049

3150
@_disfavoredOverload
3251
public subscript<Subject>(dynamicMember keyPath: WritableKeyPath<Value, Subject>) -> Binding<Subject> {
33-
return .init(
52+
return Binding<Subject>(
3453
get: { self.wrappedValue[keyPath: keyPath] },
3554
set: { self.wrappedValue[keyPath: keyPath] = $0 }
3655
)
@@ -51,16 +70,21 @@ enum ObservableValues {
5170
get {
5271
root
5372
} set {
54-
objectWillChange.send()
55-
73+
_objectWillChange_send(deferred: configuration.deferUpdates)
74+
5675
root = newValue
5776

5877
_objectDidChange.send()
5978
}
6079
}
6180

62-
public init(root: Root) {
81+
public init(
82+
root: Root,
83+
configuration: AnyObservableValue<Root>.Configuration = .init()
84+
) {
6385
self.root = root
86+
87+
super.init(configuration: configuration)
6488
}
6589
}
6690

@@ -74,21 +98,29 @@ enum ObservableValues {
7498
get {
7599
root.wrappedValue[keyPath: keyPath]
76100
} set {
77-
objectWillChange.send()
78-
101+
_objectWillChange_send(deferred: configuration.deferUpdates)
102+
79103
root.wrappedValue[keyPath: keyPath] = newValue
80104
}
81105
}
82106

83-
public init(root: AnyObservableValue<Root>, keyPath: WritableKeyPath<Root, Value>) {
107+
public init(
108+
root: AnyObservableValue<Root>,
109+
keyPath: WritableKeyPath<Root, Value>,
110+
configuration: AnyObservableValue<Value>.Configuration = .init()
111+
) {
84112
self.root = root
85113
self.keyPath = keyPath
86114
self.subscription = nil
87115

88-
super.init()
116+
super.init(configuration: configuration)
89117

90-
subscription = root.objectWillChange.sink(receiveValue: { _ in
91-
self.objectWillChange.send()
118+
subscription = root.objectWillChange.sink(receiveValue: { [weak self] _ in
119+
guard let `self` = self else {
120+
return
121+
}
122+
123+
self._objectWillChange_send(deferred: self.configuration.deferUpdates)
92124
})
93125
}
94126
}
@@ -104,21 +136,29 @@ enum ObservableValues {
104136
get {
105137
root[keyPath: keyPath]
106138
} set {
107-
objectWillChange.send()
139+
_objectWillChange_send(deferred: configuration.deferUpdates)
108140

109141
root[keyPath: keyPath] = newValue
110142
}
111143
}
112144

113-
public init(root: Root, keyPath: ReferenceWritableKeyPath<Root, Value>) {
145+
public init(
146+
root: Root,
147+
keyPath: ReferenceWritableKeyPath<Root, Value>,
148+
configuration: AnyObservableValue<Value>.Configuration = .init()
149+
) {
114150
self.root = root
115151
self.keyPath = keyPath
116152
self.subscription = nil
117153

118-
super.init()
154+
super.init(configuration: configuration)
119155

120-
subscription = root.objectWillChange.sink(receiveValue: { _ in
121-
self.objectWillChange.send()
156+
subscription = root.objectWillChange.sink(receiveValue: { [weak self] _ in
157+
guard let `self` = self else {
158+
return
159+
}
160+
161+
self._objectWillChange_send(deferred: self.configuration.deferUpdates)
122162
})
123163
}
124164
}

Sources/SwiftUIX/Intermodular/Helpers/Combine/_ObservableObjectBox.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public final class _ObservableObjectMutableBox<Value, WrappedValue>: _AnyObserva
5656
}
5757
}
5858

59-
objectWillChange.send()
59+
_objectWillChange_send()
6060
} didSet {
6161
if _equate(oldValue, base), baseSubscription != nil {
6262
return
@@ -279,7 +279,7 @@ public final class _ObservableObjectMutableBox<Value, WrappedValue>: _AnyObserva
279279
return
280280
}
281281

282-
`self`.objectWillChange.send()
282+
`self`._objectWillChange_send()
283283
})
284284
}
285285
}

Sources/SwiftUIX/Intermodular/Helpers/Swift/ReferenceBox.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) Vatsal Manot
33
//
44

5+
import _SwiftUIX
56
import Combine
67
import Swift
78
import SwiftUI
@@ -259,7 +260,7 @@ public final class _SwiftUIX_ObservableWeakReferenceBox<T: AnyObject>: Observabl
259260
return
260261
}
261262

262-
objectWillChange.send()
263+
_objectWillChange_send()
263264
}
264265
}
265266

@@ -295,7 +296,7 @@ public final class _SwiftUIX_ObservableWeakReferenceBox<T: AnyObject>: Observabl
295296
public final class _SwiftUIX_WeakObservableReferenceBox<Value: AnyObject>: ObservableObject {
296297
public weak var value: Value? {
297298
didSet {
298-
objectWillChange.send()
299+
_objectWillChange_send()
299300
}
300301
}
301302

Sources/SwiftUIX/Intermodular/Helpers/UIKit/UIHostingPageViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#if (os(iOS) && canImport(CoreTelephony)) || os(tvOS) || targetEnvironment(macCatalyst)
66

7+
import _SwiftUIX
78
import Swift
89
import SwiftUI
910
import UIKit
@@ -159,7 +160,7 @@ class UIHostingPageViewController<Page: View>: UIPageViewController, _opaque_UIH
159160
let activePageTransitionProgress = (scrollView.contentOffset.x - view.frame.size.width) / view.frame.size.width
160161

161162
if paginationState != nil {
162-
// _pageUpdateDriver.objectWillChange.send() // FIXME: This does not perform well.
163+
// _pageUpdateDriver._objectWillChange_send() // FIXME: This does not perform well.
163164
}
164165

165166
if activePageTransitionProgress == 0 {

Sources/SwiftUIX/Intramodular/Dynamic Properties/ObservedValue.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import Combine
66
import Swift
77
import SwiftUI
88

9+
public struct ObservedValueConfiguration<Value> {
10+
public var deferUpdates: Bool = false
11+
12+
public init() {
13+
14+
}
15+
}
16+
917
@dynamicMemberLookup
1018
@propertyWrapper
1119
@_documentation(visibility: internal)
1220
public struct ObservedValue<Value>: DynamicProperty {
21+
public var configuration = ObservedValueConfiguration<Value>()
22+
1323
@PersistentObject var base: AnyObservableValue<Value>
1424

1525
public var wrappedValue: Value {
@@ -21,11 +31,15 @@ public struct ObservedValue<Value>: DynamicProperty {
2131
}
2232

2333
public var projectedValue: ObservedValue<Value> {
24-
self
34+
get {
35+
self
36+
} set {
37+
self = newValue
38+
}
2539
}
2640

2741
public var binding: Binding<Value> {
28-
.init(
42+
Binding<Value>(
2943
get: { self.wrappedValue },
3044
set: { self.wrappedValue = $0 }
3145
)
@@ -34,14 +48,16 @@ public struct ObservedValue<Value>: DynamicProperty {
3448
public subscript<Subject>(
3549
dynamicMember keyPath: WritableKeyPath<Value, Subject>
3650
) -> ObservedValue<Subject> {
37-
.init(base[dynamicMember: keyPath])
51+
ObservedValue<Subject>(base[dynamicMember: keyPath])
3852
}
3953
}
4054

4155
// MARK: - API
4256

4357
extension ObservedValue {
44-
public init(_ base: @autoclosure @escaping () -> AnyObservableValue<Value>) {
58+
public init(
59+
_ base: @autoclosure @escaping () -> AnyObservableValue<Value>
60+
) {
4561
self._base = .init(wrappedValue: base())
4662
}
4763

@@ -54,9 +70,18 @@ extension ObservedValue {
5470

5571
public init<Root: ObservableObject>(
5672
_ keyPath: ReferenceWritableKeyPath<Root, Value>,
57-
on root: Root
73+
on root: Root,
74+
deferUpdates: Bool? = nil
5875
) {
59-
self.init(ObservableValues.ObjectMember(root: root, keyPath: keyPath))
76+
self.init(
77+
ObservableValues.ObjectMember(
78+
root: root,
79+
keyPath: keyPath,
80+
configuration: .init(
81+
deferUpdates: deferUpdates
82+
)
83+
)
84+
)
6085
}
6186

6287
public static func constant(

Sources/SwiftUIX/Intramodular/Dynamic Properties/PersistentObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct PersistentObject<Value>: DynamicProperty {
2929
} nonmutating set {
3030
_ = foo
3131

32-
observedObjectContainer.objectWillChange.send()
32+
observedObjectContainer._objectWillChange_send()
3333

3434
objectContainer.__unsafe_opaque_base = newValue
3535
observedObjectContainer.__unsafe_opaque_base = objectContainer.__unsafe_opaque_base

Sources/SwiftUIX/Intramodular/Dynamic Properties/TimerState.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) Vatsal Manot
33
//
44

5+
import _SwiftUIX
56
import Combine
67
import Dispatch
78
import Swift
@@ -15,7 +16,7 @@ public struct TimerState: DynamicProperty {
1516
private class ValueBox: ObservableObject {
1617
var value: Int {
1718
willSet {
18-
objectWillChange.send()
19+
_objectWillChange_send()
1920
}
2021
}
2122

Sources/SwiftUIX/Intramodular/Dynamic Properties/UserStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ extension UserStorage {
205205
do {
206206
if configuration.deferUpdates {
207207
Task(priority: .userInitiated) { @MainActor in
208-
objectWillChange.send()
208+
_objectWillChange_send()
209209
}
210210
} else {
211-
objectWillChange.send()
211+
_objectWillChange_send()
212212
}
213213

214214
storedValue = newValue

Sources/SwiftUIX/Intramodular/Dynamic Properties/ViewStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct ViewStorage<Value>: Identifiable, DynamicProperty {
2828
fileprivate init(_ value: Value) {
2929
self.value = value
3030

31-
super.init()
31+
super.init(configuration: AnyObservableValue.Configuration())
3232
}
3333
}
3434

Sources/SwiftUIX/Intramodular/Keyboard/Keyboard.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class Keyboard: ObservableObject {
5353
self.keyboardWillShowSubscription = notificationCenter
5454
.publisher(for: UIResponder.keyboardWillShowNotification)
5555
.receive(on: DispatchQueue.main)
56-
.sink(receiveValue: { _ in self.objectWillChange.send() })
56+
.sink(receiveValue: { _ in self._objectWillChange_send() })
5757

5858
self.keyboardDidShowSubscription = notificationCenter
5959
.publisher(for: UIResponder.keyboardDidShowNotification)
@@ -65,7 +65,7 @@ public final class Keyboard: ObservableObject {
6565
self.keyboardWillHideSubscription = notificationCenter
6666
.publisher(for: UIResponder.keyboardWillHideNotification)
6767
.receive(on: DispatchQueue.main)
68-
.sink(receiveValue: { _ in self.objectWillChange.send() })
68+
.sink(receiveValue: { _ in self._objectWillChange_send() })
6969

7070
self.keyboardDidHideSubscription = notificationCenter
7171
.publisher(for: UIResponder.keyboardDidHideNotification)

Sources/SwiftUIX/Intramodular/List/_PlatformTableViewCellView.ContentHostingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ extension _PlatformTableCellView {
175175
contentHostingViewCoordinator.stateFlags.insert(.payloadDidJustUpdate)
176176

177177
DispatchQueue.main.async {
178-
// self.contentHostingViewCoordinator.objectWillChange.send()
178+
// self.contentHostingViewCoordinator._objectWillChange_send()
179179
self.contentHostingViewCoordinator.stateFlags.remove(.payloadDidJustUpdate)
180180
}
181181
}

Sources/SwiftUIX/Intramodular/Miscellaneous/Utility Views/EmptyObservableObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class EmptyObservableObject: ObservableObject, Hashable {
1414
}
1515

1616
public func notify() {
17-
objectWillChange.send()
17+
_objectWillChange_send()
1818
}
1919

2020
public static func == (lhs: EmptyObservableObject, rhs: EmptyObservableObject) -> Bool {

Sources/SwiftUIX/Intramodular/Screen/Screen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class Screen: ObservableObject {
6868
object: nil,
6969
queue: .main,
7070
using: { [weak self] notification in
71-
self?.objectWillChange.send()
71+
self?._objectWillChange_send()
7272
}
7373
)
7474
#endif

0 commit comments

Comments
 (0)