Skip to content

Commit b64f68d

Browse files
committed
Refactor StepWrapper and introduce StepSystem for observer pattern. Remove unused StepSteps struct and update MutateID method. Enhance StepsMacro to notify observers on selection changes.</message>
1 parent 992f1c8 commit b64f68d

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

Sources/VDFlow/MutateID.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct MutateID: Comparable, Hashable, Codable, Sendable {
1515
try (mutationDate ?? 0).encode(to: encoder)
1616
}
1717

18-
public mutating func _update() {
18+
mutating func update() {
1919
mutationDate = DispatchTime.now().uptimeNanoseconds
2020
}
2121

Sources/VDFlow/StepSystem.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
public protocol StepsObserver: Sendable {
4+
5+
func stepWillChange<Parent: StepsCollection, Value>(to newValue: Parent.AllSteps, in type: Parent.Type, with value: Value)
6+
func stepDidChange<Parent: StepsCollection, Value>(to newValue: Parent.AllSteps, in type: Parent.Type, with value: Value)
7+
}
8+
9+
public struct NoopStepsObserver: StepsObserver {
10+
11+
public init() {}
12+
13+
public func stepWillChange<Parent: StepsCollection, Value>(to newValue: Parent.AllSteps, in type: Parent.Type, with value: Value) {}
14+
public func stepDidChange<Parent: StepsCollection, Value>(to newValue: Parent.AllSteps, in type: Parent.Type, with value: Value) {}
15+
}
16+
17+
public enum StepSystem {
18+
19+
public static var observer: StepsObserver {
20+
get { lock.withLock { _observer } }
21+
set { lock.withLock { _observer = newValue } }
22+
}
23+
private static let lock = NSRecursiveLock()
24+
private static var _observer: StepsObserver = NoopStepsObserver()
25+
}

Sources/VDFlow/StepWrapper.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ public extension StepsCollection {
55
typealias StepID<Value> = VDFlow.StepWrapper<Self, Value>
66
}
77

8+
public protocol StepWrapperType {
9+
10+
mutating func select()
11+
}
12+
813
@propertyWrapper
9-
public struct StepWrapper<Parent: StepsCollection, Value>: Identifiable {
14+
public struct StepWrapper<Parent: StepsCollection, Value>: Identifiable, StepWrapperType {
1015

1116
public let id: Parent.AllSteps
12-
public var _mutateID = MutateID()
17+
private var mutateID = MutateID()
1318
public var wrappedValue: Value
1419
public var projectedValue: StepWrapper {
1520
get { self }
@@ -25,7 +30,9 @@ public struct StepWrapper<Parent: StepsCollection, Value>: Identifiable {
2530
}
2631

2732
public mutating func select() {
28-
_mutateID._update()
33+
StepSystem.observer.stepWillChange(to: id, in: Parent.self, with: wrappedValue)
34+
mutateID.update()
35+
StepSystem.observer.stepDidChange(to: id, in: Parent.self, with: wrappedValue)
2936
}
3037

3138
public mutating func select(with value: Value) {
@@ -35,7 +42,7 @@ public struct StepWrapper<Parent: StepsCollection, Value>: Identifiable {
3542

3643
public var _lastMutateID: (Parent.AllSteps, MutateID)? {
3744
let lastNested = (wrappedValue as? any StepsCollection)?._lastMutateID?.optional
38-
let this = _mutateID.optional
45+
let this = mutateID.optional
3946
return [lastNested, this]
4047
.compactMap { $0 }
4148
.sorted { $0 > $1 }

Sources/VDFlow/macros.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,3 @@ public macro Steps() = #externalMacro(module: "VDFlowMacros", type: "StepsMacro"
99
@attached(accessor, names: named(didSet))
1010
public macro Step() = #externalMacro(module: "VDFlowMacros", type: "StepsMacro")
1111
#endif
12-
13-
@Steps
14-
public struct StepSteps {
15-
16-
public var value: Int = SomeInt()
17-
public var int = Int()
18-
public var none
19-
}
20-
21-
public func SomeInt() -> Int { 0 }

Sources/VDFlowMacros/StepsMacro.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
100100
101101
public var selected: Self {
102102
get { self }
103-
set { self = newValue }
103+
set {
104+
StepSystem.observer.stepWillChange(to: newValue, in: Self.self, with: ())
105+
self = newValue
106+
StepSystem.observer.stepDidChange(to: newValue, in: Self.self, with: ())
107+
}
104108
}
105109
"""]
106110
}
@@ -315,22 +319,23 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
315319
public var selected: \(raw: stepsType) {
316320
get { if let result = lastMutateStepID { return result.0 } else { return \(raw: defaultValue) } }
317321
set {
318-
guard let keyPath = Self._mutateIDs[newValue] else {
322+
guard let selection = Self._selections[newValue] else {
319323
\(raw: hasDeselected ? "_deselectedMutateID._update()" : "")
320324
return
321325
}
322-
self[keyPath: keyPath]._update()
326+
selection(&self)
323327
}
324328
}
325329
"""
326330
result.append(selected)
327331

328332
let isSelected: DeclSyntax =
329333
"""
330-
public static func isSelected<T>(_ keyPath: WritableKeyPath<Self, StepID<T>>) -> Bool {
334+
public func isSelected<T>(_ keyPath: WritableKeyPath<Self, StepID<T>>) -> Bool {
331335
selected == self[keyPath: keyPath].id
332336
}
333337
"""
338+
result.append(isSelected)
334339

335340
let typealiasDecl: DeclSyntax = "public typealias AllSteps = \(raw: stepsType)"
336341
result.append(typealiasDecl)
@@ -345,8 +350,8 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
345350

346351
let mutateIDs: DeclSyntax =
347352
"""
348-
private static var _mutateIDs: [AllSteps: WritableKeyPath<Self, MutateID>] {
349-
[\(raw: cases.map { ".\($0): \\.$\($0)._mutateID" }.joined(separator: ", "))]
353+
private static var _selections: [AllSteps: (inout Self) -> Void] {
354+
[\(raw: cases.map { ".\($0): { $0.$\($0).select() }" }.joined(separator: ", "))]
350355
}
351356
"""
352357
result.append(mutateIDs)

0 commit comments

Comments
 (0)