Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Nov 20, 2024
1 parent b3ff873 commit db0412a
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ open class NSHostingPopover<Content: View>: _AnyAppKitOrUIKitHostingPopover, NSP
} else {
let contentViewController = _contentViewController

self.objectWillChange.send()
self._objectWillChange_send()

let content = contentViewController.mainView.content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Vatsal Manot
//

import _SwiftUIX
import Combine
import Swift
import SwiftUI
Expand All @@ -10,6 +11,24 @@ import SwiftUI
@dynamicMemberLookup
@_documentation(visibility: internal)
public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, ObservableObject {
public struct Configuration {
public var deferUpdates: Bool

public init(
deferUpdates: Bool?
) {
self.deferUpdates = deferUpdates ?? false
}

public init() {
self.init(
deferUpdates: nil
)
}
}

public var configuration = Configuration()

public var wrappedValue: Value {
get {
fatalError() // abstract
Expand All @@ -18,10 +37,10 @@ public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, Observabl
}
}

internal init() {

init(configuration: Configuration) {
self.configuration = configuration
}

public subscript<Subject>(
dynamicMember keyPath: WritableKeyPath<Value, Subject>
) -> AnyObservableValue<Subject> {
Expand All @@ -30,7 +49,7 @@ public class AnyObservableValue<Value>: _SwiftUIX_AnyIndirectValueBox, Observabl

@_disfavoredOverload
public subscript<Subject>(dynamicMember keyPath: WritableKeyPath<Value, Subject>) -> Binding<Subject> {
return .init(
return Binding<Subject>(
get: { self.wrappedValue[keyPath: keyPath] },
set: { self.wrappedValue[keyPath: keyPath] = $0 }
)
Expand All @@ -51,16 +70,21 @@ enum ObservableValues {
get {
root
} set {
objectWillChange.send()
_objectWillChange_send(deferred: configuration.deferUpdates)

root = newValue

_objectDidChange.send()
}
}

public init(root: Root) {
public init(
root: Root,
configuration: AnyObservableValue<Root>.Configuration = .init()
) {
self.root = root

super.init(configuration: configuration)
}
}

Expand All @@ -74,21 +98,29 @@ enum ObservableValues {
get {
root.wrappedValue[keyPath: keyPath]
} set {
objectWillChange.send()
_objectWillChange_send(deferred: configuration.deferUpdates)

root.wrappedValue[keyPath: keyPath] = newValue
}
}

public init(root: AnyObservableValue<Root>, keyPath: WritableKeyPath<Root, Value>) {
public init(
root: AnyObservableValue<Root>,
keyPath: WritableKeyPath<Root, Value>,
configuration: AnyObservableValue<Value>.Configuration = .init()
) {
self.root = root
self.keyPath = keyPath
self.subscription = nil

super.init()
super.init(configuration: configuration)

subscription = root.objectWillChange.sink(receiveValue: { _ in
self.objectWillChange.send()
subscription = root.objectWillChange.sink(receiveValue: { [weak self] _ in
guard let `self` = self else {
return
}

self._objectWillChange_send(deferred: self.configuration.deferUpdates)
})
}
}
Expand All @@ -104,21 +136,29 @@ enum ObservableValues {
get {
root[keyPath: keyPath]
} set {
objectWillChange.send()
_objectWillChange_send(deferred: configuration.deferUpdates)

root[keyPath: keyPath] = newValue
}
}

public init(root: Root, keyPath: ReferenceWritableKeyPath<Root, Value>) {
public init(
root: Root,
keyPath: ReferenceWritableKeyPath<Root, Value>,
configuration: AnyObservableValue<Value>.Configuration = .init()
) {
self.root = root
self.keyPath = keyPath
self.subscription = nil

super.init()
super.init(configuration: configuration)

subscription = root.objectWillChange.sink(receiveValue: { _ in
self.objectWillChange.send()
subscription = root.objectWillChange.sink(receiveValue: { [weak self] _ in
guard let `self` = self else {
return
}

self._objectWillChange_send(deferred: self.configuration.deferUpdates)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class _ObservableObjectMutableBox<Value, WrappedValue>: _AnyObserva
}
}

objectWillChange.send()
_objectWillChange_send()
} didSet {
if _equate(oldValue, base), baseSubscription != nil {
return
Expand Down Expand Up @@ -279,7 +279,7 @@ public final class _ObservableObjectMutableBox<Value, WrappedValue>: _AnyObserva
return
}

`self`.objectWillChange.send()
`self`._objectWillChange_send()
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Vatsal Manot
//

import _SwiftUIX
import Combine
import Swift
import SwiftUI
Expand Down Expand Up @@ -259,7 +260,7 @@ public final class _SwiftUIX_ObservableWeakReferenceBox<T: AnyObject>: Observabl
return
}

objectWillChange.send()
_objectWillChange_send()
}
}

Expand Down Expand Up @@ -295,7 +296,7 @@ public final class _SwiftUIX_ObservableWeakReferenceBox<T: AnyObject>: Observabl
public final class _SwiftUIX_WeakObservableReferenceBox<Value: AnyObject>: ObservableObject {
public weak var value: Value? {
didSet {
objectWillChange.send()
_objectWillChange_send()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

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

if paginationState != nil {
// _pageUpdateDriver.objectWillChange.send() // FIXME: This does not perform well.
// _pageUpdateDriver._objectWillChange_send() // FIXME: This does not perform well.
}

if activePageTransitionProgress == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import Combine
import Swift
import SwiftUI

public struct ObservedValueConfiguration<Value> {
public var deferUpdates: Bool = false

public init() {

}
}

@dynamicMemberLookup
@propertyWrapper
@_documentation(visibility: internal)
public struct ObservedValue<Value>: DynamicProperty {
public var configuration = ObservedValueConfiguration<Value>()

@PersistentObject var base: AnyObservableValue<Value>

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

public var projectedValue: ObservedValue<Value> {
self
get {
self
} set {
self = newValue
}
}

public var binding: Binding<Value> {
.init(
Binding<Value>(
get: { self.wrappedValue },
set: { self.wrappedValue = $0 }
)
Expand All @@ -34,14 +48,16 @@ public struct ObservedValue<Value>: DynamicProperty {
public subscript<Subject>(
dynamicMember keyPath: WritableKeyPath<Value, Subject>
) -> ObservedValue<Subject> {
.init(base[dynamicMember: keyPath])
ObservedValue<Subject>(base[dynamicMember: keyPath])
}
}

// MARK: - API

extension ObservedValue {
public init(_ base: @autoclosure @escaping () -> AnyObservableValue<Value>) {
public init(
_ base: @autoclosure @escaping () -> AnyObservableValue<Value>
) {
self._base = .init(wrappedValue: base())
}

Expand All @@ -54,9 +70,18 @@ extension ObservedValue {

public init<Root: ObservableObject>(
_ keyPath: ReferenceWritableKeyPath<Root, Value>,
on root: Root
on root: Root,
deferUpdates: Bool? = nil
) {
self.init(ObservableValues.ObjectMember(root: root, keyPath: keyPath))
self.init(
ObservableValues.ObjectMember(
root: root,
keyPath: keyPath,
configuration: .init(
deferUpdates: deferUpdates
)
)
)
}

public static func constant(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct PersistentObject<Value>: DynamicProperty {
} nonmutating set {
_ = foo

observedObjectContainer.objectWillChange.send()
observedObjectContainer._objectWillChange_send()

objectContainer.__unsafe_opaque_base = newValue
observedObjectContainer.__unsafe_opaque_base = objectContainer.__unsafe_opaque_base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Vatsal Manot
//

import _SwiftUIX
import Combine
import Dispatch
import Swift
Expand All @@ -15,7 +16,7 @@ public struct TimerState: DynamicProperty {
private class ValueBox: ObservableObject {
var value: Int {
willSet {
objectWillChange.send()
_objectWillChange_send()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ extension UserStorage {
do {
if configuration.deferUpdates {
Task(priority: .userInitiated) { @MainActor in
objectWillChange.send()
_objectWillChange_send()
}
} else {
objectWillChange.send()
_objectWillChange_send()
}

storedValue = newValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct ViewStorage<Value>: Identifiable, DynamicProperty {
fileprivate init(_ value: Value) {
self.value = value

super.init()
super.init(configuration: AnyObservableValue.Configuration())
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftUIX/Intramodular/Keyboard/Keyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class Keyboard: ObservableObject {
self.keyboardWillShowSubscription = notificationCenter
.publisher(for: UIResponder.keyboardWillShowNotification)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { _ in self.objectWillChange.send() })
.sink(receiveValue: { _ in self._objectWillChange_send() })

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

self.keyboardDidHideSubscription = notificationCenter
.publisher(for: UIResponder.keyboardDidHideNotification)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ extension _PlatformTableCellView {
contentHostingViewCoordinator.stateFlags.insert(.payloadDidJustUpdate)

DispatchQueue.main.async {
// self.contentHostingViewCoordinator.objectWillChange.send()
// self.contentHostingViewCoordinator._objectWillChange_send()
self.contentHostingViewCoordinator.stateFlags.remove(.payloadDidJustUpdate)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class EmptyObservableObject: ObservableObject, Hashable {
}

public func notify() {
objectWillChange.send()
_objectWillChange_send()
}

public static func == (lhs: EmptyObservableObject, rhs: EmptyObservableObject) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUIX/Intramodular/Screen/Screen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Screen: ObservableObject {
object: nil,
queue: .main,
using: { [weak self] notification in
self?.objectWillChange.send()
self?._objectWillChange_send()
}
)
#endif
Expand Down
Loading

0 comments on commit db0412a

Please sign in to comment.