Skip to content

Commit

Permalink
Add _modify to @perceptible
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed Jun 25, 2024
1 parent 0fb2e78 commit de05abb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Perception/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public macro Perceptible() =
#externalMacro(module: "PerceptionMacros", type: "PerceptibleMacro")

@attached(accessor, names: named(init), named(get), named(set))
@attached(accessor, names: named(init), named(get), named(set), named(_modify))
@attached(peer, names: prefixed(_))
public macro PerceptionTracked() =
#externalMacro(module: "PerceptionMacros", type: "PerceptionTrackedMacro")
Expand Down
12 changes: 11 additions & 1 deletion Sources/PerceptionMacros/PerceptibleMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,17 @@ public struct PerceptionTrackedMacro: AccessorMacro {
}
"""

return [initAccessor, getAccessor, setAccessor]
let modifyAccessor: AccessorDeclSyntax =
"""
_modify {
access(keyPath: \\.\(identifier))
\(raw: PerceptibleMacro.registrarVariableName).willSet(self, keyPath: \\.\(identifier))
defer { \(raw: PerceptibleMacro.registrarVariableName).didSet(self, keyPath: \\.\(identifier)) }
yield &_\(identifier)
}
"""

return [initAccessor, getAccessor, setAccessor, modifyAccessor]
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/PerceptionMacrosTests/PerceptionMacrosTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
_count = newValue
}
}
_modify {
access(keyPath: \.count)
_$perceptionRegistrar.willSet(self, keyPath: \.count)
defer {
_$perceptionRegistrar.didSet(self, keyPath: \.count)
}
yield &_count
}
}
private let _$perceptionRegistrar = Perception.PerceptionRegistrar()
Expand Down

0 comments on commit de05abb

Please sign in to comment.