Skip to content

Commit

Permalink
Rename ObservationToken to ObserveToken. (#208)
Browse files Browse the repository at this point in the history
* Rename ObservationToken to ObserveToken.

* deprecation
  • Loading branch information
mbrandonw authored Aug 19, 2024
1 parent 339ba3e commit 150eb9f
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SwiftUI, UIKit, AppKit, and even non-Apple platforms.
### Observing changes to state

- ``observe(isolation:_:)-93yzu``
- ``ObservationToken``
- ``ObserveToken``

### Creating and sharing state

Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftNavigation/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// MARK: - Deprecated after 2.1.0

@available(*, deprecated, renamed: "ObserveToken")
public typealias ObservationToken = ObserveToken

// MARK: - Deprecated after 2.0.0

extension AlertState {
@available(*, deprecated, message: "Use 'init(title:actions:message:)' instead.")
public init(
Expand Down
18 changes: 9 additions & 9 deletions Sources/SwiftNavigation/Observe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import ConcurrencyExtras
public func observe(
isolation: (any Actor)? = #isolation,
@_inheritActorContext _ apply: @escaping @Sendable () -> Void
) -> ObservationToken {
) -> ObserveToken {
observe(isolation: isolation) { _ in apply() }
}

Expand All @@ -74,7 +74,7 @@ import ConcurrencyExtras
public func observe(
isolation: (any Actor)? = #isolation,
@_inheritActorContext _ apply: @escaping @Sendable (_ transaction: UITransaction) -> Void
) -> ObservationToken {
) -> ObserveToken {
let actor = ActorProxy(base: isolation)
return observe(
apply,
Expand Down Expand Up @@ -110,8 +110,8 @@ public func observe(
) -> Void = {
Task(operation: $1)
}
) -> ObservationToken {
let token = ObservationToken()
) -> ObserveToken {
let token = ObserveToken()
onChange(
{ [weak token] transaction in
guard
Expand Down Expand Up @@ -144,12 +144,12 @@ private func onChange(
///
/// When this token is deallocated it cancels the observation it was associated with. Store this
/// token in another object to keep the observation alive. You can do with this with a set of
/// ``ObservationToken``s and the ``store(in:)-1hsqo`` method:
/// ``ObserveToken``s and the ``store(in:)-1hsqo`` method:
///
/// ```swift
/// class Coordinator {
/// let model = Model()
/// var tokens: Set<ObservationToken> = []
/// var tokens: Set<ObserveToken> = []
///
/// func start() {
/// observe { [weak self] in
Expand All @@ -159,7 +159,7 @@ private func onChange(
/// }
/// }
/// ```
public final class ObservationToken: @unchecked Sendable, HashableObject {
public final class ObserveToken: @unchecked Sendable, HashableObject {
fileprivate let _isCancelled = LockIsolated(false)
public var onCancel: @Sendable () -> Void

Expand Down Expand Up @@ -191,14 +191,14 @@ public final class ObservationToken: @unchecked Sendable, HashableObject {
/// Stores this observation token instance in the specified collection.
///
/// - Parameter collection: The collection in which to store this observation token.
public func store(in collection: inout some RangeReplaceableCollection<ObservationToken>) {
public func store(in collection: inout some RangeReplaceableCollection<ObserveToken>) {
collection.append(self)
}

/// Stores this observation token instance in the specified set.
///
/// - Parameter set: The set in which to store this observation token.
public func store(in set: inout Set<ObservationToken>) {
public func store(in set: inout Set<ObserveToken>) {
set.insert(self)
}
}
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UIColorWell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// when the selected color changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(selectedColor: UIBinding<UIColor?>) -> ObservationToken {
public func bind(selectedColor: UIBinding<UIColor?>) -> ObserveToken {
bind(selectedColor, to: \.selectedColor, for: .valueChanged)
}
}
Expand Down
22 changes: 11 additions & 11 deletions Sources/UIKitNavigation/Bindings/UIControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
_ binding: UIBinding<Value>,
to keyPath: ReferenceWritableKeyPath<Self, Value>,
for event: UIControl.Event
) -> ObservationToken {
) -> ObserveToken {
bind(binding, to: keyPath, for: event) { control, newValue, _ in
control[keyPath: keyPath] = newValue
}
Expand All @@ -45,7 +45,7 @@
to keyPath: KeyPath<Self, Value>,
for event: UIControl.Event,
set: @escaping (_ control: Self, _ newValue: Value, _ transaction: UITransaction) -> Void
) -> ObservationToken {
) -> ObserveToken {
unbind(keyPath)
let action = UIAction { [weak self] _ in
guard let self else { return }
Expand Down Expand Up @@ -73,35 +73,35 @@
binding.wrappedValue = control[keyPath: $uncheckedKeyPath.wrappedValue]
}
}
let observationToken = ObservationToken { [weak self] in
let observeToken = ObserveToken { [weak self] in
MainActor._assumeIsolated {
self?.removeAction(action, for: .allEvents)
}
token.cancel()
observation.invalidate()
}
observationTokens[keyPath] = observationToken
return observationToken
observeTokens[keyPath] = observeToken
return observeToken
}

public func unbind<Value>(_ keyPath: KeyPath<Self, Value>) {
observationTokens[keyPath]?.cancel()
observationTokens[keyPath] = nil
observeTokens[keyPath]?.cancel()
observeTokens[keyPath] = nil
}

var observationTokens: [AnyKeyPath: ObservationToken] {
var observeTokens: [AnyKeyPath: ObserveToken] {
get {
objc_getAssociatedObject(self, observationTokensKey) as? [AnyKeyPath: ObservationToken]
objc_getAssociatedObject(self, observeTokensKey) as? [AnyKeyPath: ObserveToken]
?? [:]
}
set {
objc_setAssociatedObject(
self, observationTokensKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC
self, observeTokensKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
}

@MainActor
private let observationTokensKey = malloc(1)!
private let observeTokensKey = malloc(1)!
#endif
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UIDatePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// selected date changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(date: UIBinding<Date>) -> ObservationToken {
public func bind(date: UIBinding<Date>) -> ObserveToken {
bind(date, to: \.date, for: .valueChanged)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UIPageControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// the current page changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(currentPage: UIBinding<Int>) -> ObservationToken {
public func bind(currentPage: UIBinding<Int>) -> ObserveToken {
bind(currentPage, to: \.currentPage, for: .valueChanged)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UISegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) -> ObservationToken {
) -> ObserveToken {
let fileID = HashableStaticString(rawValue: fileID)
let filePath = HashableStaticString(rawValue: filePath)
return bind(
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UISlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// value changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(value: UIBinding<Float>) -> ObservationToken {
public func bind(value: UIBinding<Float>) -> ObserveToken {
bind(value, to: \.value, for: .valueChanged)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UIStepper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// value changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(value: UIBinding<Double>) -> ObservationToken {
public func bind(value: UIBinding<Double>) -> ObserveToken {
bind(value, to: \.value, for: .valueChanged)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UIKitNavigation/Bindings/UISwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// state changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(isOn: UIBinding<Bool>) -> ObservationToken {
public func bind(isOn: UIBinding<Bool>) -> ObserveToken {
bind(isOn, to: \.isOn, for: .valueChanged) { control, isOn, transaction in
control.setOn(isOn, animated: !transaction.uiKit.disablesAnimations)
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/UIKitNavigation/Bindings/UITabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) -> ObservationToken {
) -> ObserveToken {
let token = observe { [weak self] in
guard let self else { return }
guard let identifier = selectedTab.wrappedValue else {
Expand Down Expand Up @@ -41,25 +41,25 @@
selectedTab.wrappedValue = controller.selectedTab?.identifier
}
}
let observationToken = ObservationToken {
let observeToken = ObserveToken {
token.cancel()
observation.invalidate()
}
self.observationToken = observationToken
return observationToken
self.observeToken = observeToken
return observeToken
}

private var observationToken: ObservationToken? {
private var observeToken: ObserveToken? {
get {
objc_getAssociatedObject(self, Self.observationTokenKey) as? ObservationToken
objc_getAssociatedObject(self, Self.observeTokenKey) as? ObserveToken
}
set {
objc_setAssociatedObject(
self, Self.observationTokenKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC
self, Self.observeTokenKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}

private static let observationTokenKey = malloc(1)!
private static let observeTokenKey = malloc(1)!
}
#endif
22 changes: 11 additions & 11 deletions Sources/UIKitNavigation/Bindings/UITextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/// changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(text: UIBinding<String>) -> ObservationToken {
public func bind(text: UIBinding<String>) -> ObserveToken {
bind(UIBinding(text), to: \.text, for: .editingChanged)
}

Expand All @@ -43,7 +43,7 @@
/// the attributed text changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(attributedText: UIBinding<NSAttributedString>) -> ObservationToken {
public func bind(attributedText: UIBinding<NSAttributedString>) -> ObserveToken {
bind(UIBinding(attributedText), to: \.attributedText, for: .editingChanged)
}

Expand All @@ -53,7 +53,7 @@
/// the selected text range changes.
/// - Returns: A cancel token.
@discardableResult
public func bind(selection: UIBinding<UITextSelection?>) -> ObservationToken {
public func bind(selection: UIBinding<UITextSelection?>) -> ObserveToken {
let editingChangedAction = UIAction { [weak self] _ in
guard let self else { return }
selection.wrappedValue = self.textSelection
Expand All @@ -70,16 +70,16 @@
selection.wrappedValue = control.textSelection
}
}
let observationToken = ObservationToken { [weak self] in
let observeToken = ObserveToken { [weak self] in
MainActor._assumeIsolated {
self?.removeAction(editingChangedAction, for: [.editingChanged, .editingDidBegin])
self?.removeAction(editingDidEndAction, for: .editingDidEnd)
}
token.cancel()
observation.invalidate()
}
observationTokens[\UITextField.selectedTextRange] = observationToken
return observationToken
observeTokens[\UITextField.selectedTextRange] = observeToken
return observeToken
}

fileprivate var textSelection: UITextSelection? {
Expand Down Expand Up @@ -194,7 +194,7 @@
@discardableResult
public func bind<Value: Hashable>(
focus: UIBinding<Value?>, equals value: Value
) -> ObservationToken {
) -> ObserveToken {
self.focusToken?.cancel()
let editingDidBeginAction = UIAction { _ in focus.wrappedValue = value }
let editingDidEndAction = UIAction { _ in
Expand All @@ -214,7 +214,7 @@
break
}
}
let outerToken = ObservationToken { [weak self] in
let outerToken = ObserveToken { [weak self] in
MainActor._assumeIsolated {
self?.removeAction(editingDidBeginAction, for: .editingDidBegin)
self?.removeAction(editingDidEndAction, for: [.editingDidEnd, .editingDidEndOnExit])
Expand Down Expand Up @@ -276,12 +276,12 @@
/// automatically dismisses focus.
/// - Returns: A cancel token.
@discardableResult
public func bind(focus condition: UIBinding<Bool>) -> ObservationToken {
public func bind(focus condition: UIBinding<Bool>) -> ObserveToken {
bind(focus: condition.toOptionalUnit, equals: Bool.Unit())
}

private var focusToken: ObservationToken? {
get { objc_getAssociatedObject(self, Self.focusTokenKey) as? ObservationToken }
private var focusToken: ObserveToken? {
get { objc_getAssociatedObject(self, Self.focusTokenKey) as? ObserveToken }
set {
objc_setAssociatedObject(
self, Self.focusTokenKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC
Expand Down
Loading

0 comments on commit 150eb9f

Please sign in to comment.