Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Aug 22, 2024
1 parent 6d0d1d0 commit f2eb2d5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
19 changes: 1 addition & 18 deletions Sources/SwiftNavigation/Observe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,7 @@ private func onChange(
apply(.current)
} onChange: {
task(.current) {
onChange(apply) { transaction, operation in
var perform: @Sendable () -> Void = {
task(transaction, operation)
}
for value in transaction.storage.keys {
@Sendable func open<K: PerformKey>(_: K.Type, operation: @escaping @Sendable () -> Void) {
K.perform(transaction: transaction) {
operation()
}
}
if let type = value.keyType as? any PerformKey.Type {
perform = { [perform] in
open(type, operation: perform)
}
}
}
perform()
}
onChange(apply, task: task)
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions Sources/SwiftNavigation/UITransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public func withUITransaction<R, V>(
public struct UITransaction: Sendable {
@TaskLocal package static var current = Self()

var storage: [Key: any Sendable] = [:]
private var storage: [Key: any Sendable] = [:]

/// Creates a transaction.
public init() {}
Expand Down Expand Up @@ -68,7 +68,7 @@ public struct UITransaction: Sendable {
storage.isEmpty
}

struct Key: Hashable {
private struct Key: Hashable {
let keyType: Any.Type
init<K: UITransactionKey>(_ keyType: K.Type) {
self.keyType = keyType
Expand All @@ -93,9 +93,18 @@ public protocol UITransactionKey {
static var defaultValue: Value { get }
}

public protocol PerformKey: UITransactionKey, Sendable {
static func perform(
transaction: UITransaction,
operation: @escaping @Sendable () -> Void
)

extension UITransaction {
public var perform: @Sendable (UITransaction, @Sendable () -> Void) -> Void {
get {
self[PerformTransactionKey.self]
}
set {
self[PerformTransactionKey.self] = newValue
}
}
}

private enum PerformTransactionKey: UITransactionKey {
static let defaultValue: @Sendable (UITransaction, @Sendable () -> Void) -> Void = { $1() }
}
31 changes: 30 additions & 1 deletion Sources/UIKitNavigation/Observe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,36 @@
}
} task: { transaction, work in
DispatchQueue.main.async {
withUITransaction(transaction, work)
withUITransaction(transaction) {
MainActor._assumeIsolated {
#if os(watchOS)
apply(transaction)
#else
if transaction.uiKit.disablesAnimations {
UIView.performWithoutAnimation { work() }
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
} else if let animation = transaction.uiKit.animation {
return animation.perform(
{ work() },
completion: transaction.uiKit.animationCompletions.isEmpty
? nil
: {
for completion in transaction.uiKit.animationCompletions {
completion($0)
}
}
)
} else {
work()
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
}
#endif
}
}
}
}
tokens.append(token)
Expand Down
62 changes: 27 additions & 35 deletions Sources/UIKitNavigation/UITransaction.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if canImport(UIKit) && !os(watchOS)

import UIKit
import SwiftNavigation

extension UITransaction {
/// Creates a transaction and assigns its animation property.
Expand All @@ -17,50 +16,43 @@ import SwiftNavigation
get { self[UIKitKey.self] }
set {
self[UIKitKey.self] = newValue
// self.perform = { transaction, work in
//
// }
}
}

private enum UIKitKey: UITransactionKey, PerformKey {
static let defaultValue = UIKit()

public static func perform(
transaction: UITransaction,
operation: @escaping @Sendable () -> Void
) {
MainActor._assumeIsolated {
self.perform = { transaction, work in
MainActor._assumeIsolated {
#if os(watchOS)
//apply(transaction)
//apply(transaction)
#else
if transaction.uiKit.disablesAnimations {
UIView.performWithoutAnimation { operation() }
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
} else if let animation = transaction.uiKit.animation {
return animation.perform(
{ operation() },
completion: transaction.uiKit.animationCompletions.isEmpty
? nil
: {
for completion in transaction.uiKit.animationCompletions {
completion($0)
if transaction.uiKit.disablesAnimations {
UIView.performWithoutAnimation { work() }
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
} else if let animation = transaction.uiKit.animation {
return animation.perform(
{ work() },
completion: transaction.uiKit.animationCompletions.isEmpty
? nil
: {
for completion in transaction.uiKit.animationCompletions {
completion($0)
}
}
)
} else {
work()
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
)
} else {
operation()
for completion in transaction.uiKit.animationCompletions {
completion(true)
}
}
#endif
}
}
}
}

private enum UIKitKey: UITransactionKey {
static let defaultValue = UIKit()
}

/// UIKit-specific data associated with a ``UITransaction``.
public struct UIKit: Sendable {
/// The animation, if any, associated with the current state change.
Expand Down

0 comments on commit f2eb2d5

Please sign in to comment.