Skip to content

Commit 71bd529

Browse files
authored
Merge pull request #18 from GoodRequest/feature/writable
Markers for directly writable types
2 parents 91d9806 + 32459bb commit 71bd529

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sources/GoodReactor/Reactor.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SwiftUI
1919

2020
// MARK: - Reactor protocol
2121

22-
@MainActor @dynamicMemberLookup public protocol Reactor: AnyObject, Identifiable, Hashable {
22+
@MainActor @dynamicMemberLookup public protocol Reactor: AnyObject, Identifiable {
2323

2424
/// Internal events
2525
///
@@ -229,6 +229,30 @@ public extension Reactor {
229229

230230
}
231231

232+
// MARK: - Directly writable types
233+
234+
public extension Reactor {
235+
236+
/// Opt-in writable accessor for value type paths (structs)
237+
subscript<T: __ReactorDirectWriteable>(dynamicMember keyPath: WritableKeyPath<State, T>) -> T {
238+
get {
239+
state[keyPath: keyPath]
240+
}
241+
set {
242+
var s = state
243+
s[keyPath: keyPath] = newValue
244+
state = s
245+
}
246+
}
247+
248+
/// Opt-in writable accessor for reference type paths (class members)
249+
subscript<T: __ReactorDirectWriteable>(dynamicMember keyPath: ReferenceWritableKeyPath<State, T>) -> T {
250+
get { state[keyPath: keyPath] }
251+
set { state[keyPath: keyPath] = newValue }
252+
}
253+
254+
}
255+
232256
// MARK: - Default implementation
233257

234258
public extension Reactor {
@@ -477,7 +501,7 @@ public extension Reactor {
477501
let mutation = await map(value)
478502
let event = Event(kind: .mutation(mutation))
479503

480-
guard let self else { return }
504+
guard let self else { break }
481505
_send(event: event)
482506
}
483507

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// ReactorDirectWritable.swift
3+
// GoodReactor
4+
//
5+
// Created by Filip Šašala on 24/09/2025.
6+
//
7+
8+
/// Marker protocol for state members that are allowed to be written
9+
/// through dynamic member lookup (e.g., navigation helpers).
10+
public protocol __ReactorDirectWritable {}

0 commit comments

Comments
 (0)