EquatableView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // EquatableView.swift
  3. // ark_ui_basic
  4. //
  5. // Audited for 6.5.4
  6. // Status: Complete
  7. // ID: 93C51C71D9D4CBAB391E78A2AAC640D6 (SwiftUI)
  8. import OpenAttributeGraphShims
  9. @_spi(ForOpenSwiftUIOnly)
  10. import ark_ui_basic_core
  11. // MARK: - EquatableView
  12. /// A view type that compares itself against its previous value and prevents its
  13. /// child updating if its new value is the same as its old value.
  14. @available(OpenSwiftUI_v1_0, *)
  15. @frozen
  16. public struct EquatableView<Content>: View, UnaryView, PrimitiveView where Content: Equatable, Content: View {
  17. public var content: Content
  18. @inlinable
  19. public init(content: Content) {
  20. self.content = content
  21. }
  22. nonisolated public static func _makeView(
  23. view: _GraphValue<Self>,
  24. inputs: _ViewInputs
  25. ) -> _ViewOutputs {
  26. let child = Child(view: view.value)
  27. return Content.makeDebuggableView(
  28. view: _GraphValue(child),
  29. inputs: inputs
  30. )
  31. }
  32. private struct Child: Rule, AsyncAttribute {
  33. @Attribute var view: EquatableView
  34. typealias Value = Content
  35. var value: Value {
  36. view.content
  37. }
  38. static var comparisonMode: ComparisonMode {
  39. .equatableAlways
  40. }
  41. }
  42. }
  43. @available(*, unavailable)
  44. extension EquatableView: Sendable {}
  45. extension View where Self: Equatable {
  46. /// Prevents the view from updating its child view when its new value is the
  47. /// same as its old value.
  48. @inlinable
  49. nonisolated public func equatable() -> EquatableView<Self> {
  50. EquatableView(content: self)
  51. }
  52. }
  53. // MARK: - EquatableProxyView
  54. package struct EquatableProxyView<Content, Token>: View, UnaryView, PrimitiveView where Content: View, Token: Equatable {
  55. package var content: Content
  56. package var token: Token
  57. package init(content: Content, token: Token) {
  58. self.content = content
  59. self.token = token
  60. }
  61. nonisolated public static func _makeView(
  62. view: _GraphValue<Self>,
  63. inputs: _ViewInputs
  64. ) -> _ViewOutputs {
  65. let child = Child(view: view.value, lastToken: nil)
  66. return Content.makeDebuggableView(
  67. view: _GraphValue(child),
  68. inputs: inputs
  69. )
  70. }
  71. private struct Child: StatefulRule, AsyncAttribute {
  72. @Attribute var view: EquatableProxyView
  73. var lastToken: Token?
  74. init(view: Attribute<EquatableProxyView>, lastToken: Token?) {
  75. self._view = view
  76. self.lastToken = lastToken
  77. }
  78. typealias Value = Content
  79. mutating func updateValue() {
  80. guard hasValue && lastToken == view.token else {
  81. value = view.content
  82. lastToken = view.token
  83. return
  84. }
  85. }
  86. }
  87. }
  88. extension View {
  89. /// Prevents the view from updating its child view when its new value is the
  90. /// same as its old value.
  91. nonisolated package func equatableProxy<Token>(_ token: Token) -> EquatableProxyView<Self, Token> where Token: Equatable {
  92. EquatableProxyView(content: self, token: token)
  93. }
  94. }