-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathStateAccessor.swift
33 lines (30 loc) · 1.09 KB
/
StateAccessor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/// A wrapper around observable state that provides read and write access through unidirectional
/// channels.
///
/// This type serves as the primary channel of information in an ``ObservableModel``, by providing
/// read and write access to state through separate mechanisms.
///
/// To create an accessor, use ``Workflow/RenderContext/makeStateAccessor(state:)``. State writes
/// will flow through a workflow's state mutation sink.
///
/// This type can be embedded in an ``ObservableModel`` or used directly, for trivial workflows with
/// no custom actions.
public struct StateAccessor<State: ObservableState> {
let state: State
let sendValue: (@escaping (inout State) -> Void) -> Void
public init(
state: State,
sendValue: @escaping (@escaping (inout State) -> Void) -> Void
) {
self.state = state
self.sendValue = sendValue
}
}
extension StateAccessor: ObservableModel {
public var accessor: StateAccessor<State> { self }
}
extension StateAccessor: Identifiable where State: Identifiable {
public var id: State.ID {
state.id
}
}