Skip to content

Commit 664fd02

Browse files
authored
Added docs (#2)
1 parent fcca7a3 commit 664fd02

6 files changed

Lines changed: 60 additions & 1 deletion

File tree

.spi.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets:
5+
- Publishable
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ``Publishable-module``
2+
3+
Observe changes to `Observable` types synchronously with `Combine`.
4+
5+
## Topics
6+
7+
### Making Types Publishable
8+
9+
- ``Publishable()``
10+
- ``Publishable-protocol``
11+
12+
### Getting Property Publishers
13+
14+
- ``AnyPropertyPublisher``

Sources/Publishable/PropertyPublisher/AnyPropertyPublisher.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88

99
import Combine
1010

11+
/// An object that exposes `Combine` publishers for ``willChange`` and ``didChange`` events.
12+
///
13+
/// Subclasses of this class are generated by the ``Publishable()`` macro,
14+
/// and provide publishers for all mutable instance properties of the type the macro is applied to.
15+
///
1116
open class AnyPropertyPublisher<Object: AnyObject> {
1217

1318
private let _willChange = PassthroughSubject<Object, Never>()
1419
private let _didChange = PassthroughSubject<Object, Never>()
1520

21+
/// Emits the `Object` **before** any of its stored properties are assigned a new value.
22+
///
1623
public var willChange: AnyPublisher<Object, Never> {
1724
_willChange.eraseToAnyPublisher()
1825
}
1926

27+
/// Emits the `Object` **after** any of its stored properties are assigned a new value.
28+
///
2029
public var didChange: AnyPublisher<Object, Never> {
2130
_didChange.eraseToAnyPublisher()
2231
}

Sources/Publishable/PropertyPublisher/Publishable.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
77
//
88

9+
import Observation
10+
11+
/// A macro that adds ``Publishable-protocol`` conformance to `Observable` types.
12+
///
13+
/// - Note: This macro works only with `final` classes to which the `Observable` or `SwiftData.Model` macro has been applied directly.
14+
///
15+
/// The `Publishable` macro adds a new ``Publishable/publisher`` property to your type,
16+
/// which exposes `Combine` publishers for all mutable instance properties - both stored and computed.
17+
///
18+
/// If a property’s type conforms to `Equatable`, its publisher automatically removes duplicate values.
19+
/// Just like the `Published` property wrapper, subscribing to any of the exposed publishers immediately emits the current value.
20+
///
21+
/// - Important: Swift Macros do not have access to full type information of expressions used in the code they’re applied to.
22+
/// Since working with `Combine` requires knowledge of concrete types, this macro attempts to infer the types of properties when they are not explicitly specified.
23+
/// However, this inference may fail in non-trivial cases. If the generated code fails to compile, explicitly specifying the type of the affected property should resolve the issue.
24+
///
925
@attached(
1026
member,
1127
names: named(_publisher),
@@ -22,9 +38,22 @@ public macro Publishable() = #externalMacro(
2238
type: "PublishableMacro"
2339
)
2440

25-
public protocol Publishable: AnyObject {
41+
/// A type that can be observed using both the `Observation` and `Combine` frameworks.
42+
///
43+
/// You don't need to declare conformance to this protocol yourself.
44+
/// It is generated automatically when you apply the ``Publishable()`` macro to your type.
45+
///
46+
public protocol Publishable: AnyObject, Observable {
2647

48+
/// A subclass of ``AnyPropertyPublisher`` generated by the ``Publishable()`` macro,
49+
/// containing publishers for all mutable instance properties of the type.
50+
///
2751
associatedtype PropertyPublisher: AnyPropertyPublisher<Self>
2852

53+
/// An instance that exposes `Combine` publishers for all mutable instance properties of the type.
54+
///
55+
/// - Important: Don't store this instance in an external property. Accessing it after the original object has been deallocated
56+
/// may result in a crash. Always access it directly through the object that exposes it.
57+
///
2958
var publisher: PropertyPublisher { get }
3059
}

Sources/Publishable/Registrars/PublishableObservationRegistrar.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Observation
1010

11+
@_documentation(visibility: private)
1112
public protocol PublishableObservationRegistrar {
1213

1314
associatedtype Object: Publishable, Observable

Sources/Publishable/Registrars/SwiftObservationRegistrar.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
import Observation
1010

11+
@_documentation(visibility: private) // swiftformat:disable:next all
1112
public typealias SwiftObservationRegistrar = ObservationRegistrar

0 commit comments

Comments
 (0)