|
| 1 | +// |
| 2 | +// PropertyPublisherDeclBuilder.swift |
| 3 | +// Relay |
| 4 | +// |
| 5 | +// Created by Kamil Strzelecki on 12/01/2025. |
| 6 | +// Copyright © 2025 Kamil Strzelecki. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftSyntaxMacros |
| 10 | + |
| 11 | +internal struct PropertyPublisherDeclBuilder: ClassDeclBuilder, MemberBuilding { |
| 12 | + |
| 13 | + let declaration: ClassDeclSyntax |
| 14 | + let properties: PropertiesList |
| 15 | + let trimmedSuperclassType: TypeSyntax? |
| 16 | + let preferredGlobalActorIsolation: GlobalActorIsolation? |
| 17 | + |
| 18 | + func build() -> [DeclSyntax] { |
| 19 | + [ |
| 20 | + """ |
| 21 | + \(inheritedGlobalActorIsolation)\(inheritedAccessControlLevelAllowingOpen)\(inheritedFinalModifier)\ |
| 22 | + class PropertyPublisher: \(inheritanceClause()) { |
| 23 | +
|
| 24 | + private final unowned let object: \(trimmedType) |
| 25 | +
|
| 26 | + \(objectWillChangeDidChangePublishers()) |
| 27 | +
|
| 28 | + \(initializer()) |
| 29 | +
|
| 30 | + \(deinitializer()) |
| 31 | +
|
| 32 | + \(storedPropertiesPublishers().formatted()) |
| 33 | +
|
| 34 | + \(computedPropertiesPublishers().formatted()) |
| 35 | +
|
| 36 | + \(memoizedPropertiesPublishers().formatted()) |
| 37 | + } |
| 38 | + """ |
| 39 | + ] |
| 40 | + } |
| 41 | + |
| 42 | + private func inheritanceClause() -> TypeSyntax { |
| 43 | + if let trimmedSuperclassType { |
| 44 | + "\(trimmedSuperclassType).PropertyPublisher" |
| 45 | + } else { |
| 46 | + "Relay.AnyPropertyPublisher" |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private func objectWillChangeDidChangePublishers() -> MemberBlockItemListSyntax { |
| 51 | + let notation = CamelCaseNotation(string: trimmedType.description) |
| 52 | + let prefix = notation.joined(as: .lowerCamelCase) |
| 53 | + |
| 54 | + return """ |
| 55 | + \(inheritedAccessControlLevel)final var \ |
| 56 | + \(raw: prefix)WillChange: some Publisher<\(trimmedType), Never> { |
| 57 | + willChange.map { [unowned object] _ in |
| 58 | + object |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + \(inheritedAccessControlLevel)final var \ |
| 63 | + \(raw: prefix)DidChange: some Publisher<\(trimmedType), Never> { |
| 64 | + didChange.map { [unowned object] _ in |
| 65 | + object |
| 66 | + } |
| 67 | + } |
| 68 | + """ |
| 69 | + } |
| 70 | + |
| 71 | + private func initializer() -> MemberBlockItemListSyntax { |
| 72 | + """ |
| 73 | + \(inheritedAccessControlLevel)init(object: \(trimmedType)) { |
| 74 | + self.object = object |
| 75 | + super.init(object: object) |
| 76 | + } |
| 77 | + """ |
| 78 | + } |
| 79 | + |
| 80 | + private func deinitializer() -> MemberBlockItemListSyntax { |
| 81 | + """ |
| 82 | + \(inheritedGlobalActorIsolation)deinit { |
| 83 | + \(storedPropertiesSubjectsFinishCalls().formatted()) |
| 84 | + } |
| 85 | + """ |
| 86 | + } |
| 87 | + |
| 88 | + @CodeBlockItemListBuilder |
| 89 | + private func storedPropertiesSubjectsFinishCalls() -> CodeBlockItemListSyntax { |
| 90 | + for property in properties.all where property.isStoredPublisherTracked { |
| 91 | + let call = storedPropertySubjectFinishCall(for: property) |
| 92 | + if let ifConfigCall = property.underlying.applyingEnclosingIfConfig(to: call) { |
| 93 | + ifConfigCall |
| 94 | + } else { |
| 95 | + call |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private func storedPropertySubjectFinishCall(for property: Property) -> CodeBlockItemListSyntax { |
| 101 | + "_\(property.trimmedName).send(completion: .finished)" |
| 102 | + } |
| 103 | + |
| 104 | + @MemberBlockItemListBuilder |
| 105 | + private func storedPropertiesPublishers() -> MemberBlockItemListSyntax { |
| 106 | + for property in properties.all where property.isStoredPublisherTracked { |
| 107 | + let publisher = storedPropertyPublisher(for: property) |
| 108 | + if let ifConfigPublisher = property.underlying.applyingEnclosingIfConfig(to: publisher) { |
| 109 | + ifConfigPublisher |
| 110 | + } else { |
| 111 | + publisher |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private func storedPropertyPublisher(for property: Property) -> MemberBlockItemListSyntax { |
| 117 | + // Stored properties cannot be made potentially unavailable |
| 118 | + let accessControlLevel = AccessControlLevel.forSibling(of: property.underlying) |
| 119 | + let name = property.trimmedName |
| 120 | + let type = property.inferredType |
| 121 | + |
| 122 | + return """ |
| 123 | + fileprivate final let _\(name) = PassthroughSubject<\(type), Never>() |
| 124 | + \(accessControlLevel)final var \(name): some Publisher<\(type), Never> { |
| 125 | + _storedPropertyPublisher(_\(name), for: \\.\(name), object: object) |
| 126 | + } |
| 127 | + """ |
| 128 | + } |
| 129 | + |
| 130 | + @MemberBlockItemListBuilder |
| 131 | + private func computedPropertiesPublishers() -> MemberBlockItemListSyntax { |
| 132 | + for property in properties.all where property.isComputedPublisherTracked { |
| 133 | + let publisher = computedPropertyPublisher(for: property) |
| 134 | + if let ifConfigPublisher = property.underlying.applyingEnclosingIfConfig(to: publisher) { |
| 135 | + ifConfigPublisher |
| 136 | + } else { |
| 137 | + publisher |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private func computedPropertyPublisher(for property: Property) -> MemberBlockItemListSyntax { |
| 143 | + let accessControlLevel = AccessControlLevel.forSibling(of: property.underlying) |
| 144 | + let availability = property.availability?.trimmed.withTrailingNewline |
| 145 | + let name = property.trimmedName |
| 146 | + let type = property.inferredType |
| 147 | + |
| 148 | + return """ |
| 149 | + \(availability)\(accessControlLevel)final var \(name): some Publisher<\(type), Never> { |
| 150 | + _computedPropertyPublisher(for: \\.\(name), object: object) |
| 151 | + } |
| 152 | + """ |
| 153 | + } |
| 154 | + |
| 155 | + @MemberBlockItemListBuilder |
| 156 | + private func memoizedPropertiesPublishers() -> MemberBlockItemListSyntax { |
| 157 | + for member in declaration.memberBlock.members { |
| 158 | + if let extractionResult = MemoizedMacro.extract(from: member.decl) { |
| 159 | + let declaration = extractionResult.declaration |
| 160 | + |
| 161 | + if !declaration.attributes.contains(like: PublisherIgnoredMacro.attribute) { |
| 162 | + let publisher = memoizedPropertyPublisher(for: extractionResult) |
| 163 | + if let ifConfigPublisher = declaration.applyingEnclosingIfConfig(to: publisher) { |
| 164 | + ifConfigPublisher |
| 165 | + } else { |
| 166 | + publisher |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private func memoizedPropertyPublisher( |
| 174 | + for extractionResult: MemoizedMacro.ExtractionResult |
| 175 | + ) -> MemberBlockItemListSyntax { |
| 176 | + let accessControlLevel = extractionResult.preferredAccessControlLevel?.inheritedBySibling() |
| 177 | + let availability = extractionResult.declaration.availability?.trimmed.withTrailingNewline |
| 178 | + let name = extractionResult.propertyName |
| 179 | + let type = extractionResult.trimmedReturnType |
| 180 | + |
| 181 | + return """ |
| 182 | + \(availability)\(accessControlLevel)final var \(raw: name): some Publisher<\(type), Never> { |
| 183 | + _computedPropertyPublisher(for: \\.\(raw: name), object: object) |
| 184 | + } |
| 185 | + """ |
| 186 | + } |
| 187 | +} |
0 commit comments