Skip to content

Commit 546c7d0

Browse files
Reorder update signature to State, Action, Environment (#14)
This PR changes the order of update function arguments: - Changes update function signature from `update(State, Environment, Action)` to `update(State, Action, Environment)` - Changes Store from `Store<State, Environment, Action>` to `Store<State, Action, Environment>` Why? - This order of arguments is used by two other Elm-like libraries: [Swift Composable Architecture](https://github.com/pointfreeco/swift-composable-architecture) and [Redux-like state container](https://swiftwithmajid.com/2019/09/18/redux-like-state-container-in-swiftui/). - Context arguments like environment often seem to be passed last in Swift. E.g. [`UIViewRepresentable.Context`](https://developer.apple.com/documentation/swiftui/uiviewrepresentable/updateuiview(_:context:)) is passed last to methods in SwiftUI. - State and Action are always used, but Environment might be nil, or an empty struct. You don't always need environment. It is optional. - The order reads better in the signature, since state and action are always paired together in update function and `Update` struct.
1 parent f21ef24 commit 546c7d0

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ struct AppState: Equatable {
3333
/// State update function
3434
static func update(
3535
state: AppState,
36-
environment: AppEnvironment,
37-
action: AppAction
36+
action: AppAction,
37+
environment: AppEnvironment
3838
) -> Update<AppState, AppAction> {
3939
switch action {
4040
case .increment:
@@ -77,7 +77,7 @@ A `Store` is a source of truth for application state. It's an [ObservableObject]
7777
Store exposes a single [`@Published`](https://developer.apple.com/documentation/combine/published) property, `state`, which represents your application state. `state` is read-only, and cannot be updated directly. Instead, like Elm or Redux, all `state` changes happen through a single `update` function, with the signature:
7878

7979
```
80-
(State, Environment, Action) -> Update<State, Action>
80+
(State, Action, Environment) -> Update<State, Action>
8181
```
8282

8383
The `Update` returned is a small struct that contains a new state, plus any optional effects and animations associated with the state transition (more about that in a bit).
@@ -171,8 +171,8 @@ You can subscribe to an effects publisher by returning it as part of an Update:
171171
```swift
172172
func update(
173173
state: State,
174-
environment: Environment,
175-
action: Action
174+
action: Action,
175+
environment: Environment
176176
) -> Update<State, Action> {
177177
switch action {
178178
// ...
@@ -196,8 +196,8 @@ Use `Update.animation` to set an explicit [Animation](https://developer.apple.co
196196
```swift
197197
func update(
198198
state: State,
199-
environment: Environment,
200-
action: Action
199+
action: Action,
200+
environment: Environment
201201
) -> Update<State, Action> {
202202
switch action {
203203
// ...

Sources/ObservableStore/ObservableStore.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ where State: Equatable {
102102
/// See https://guide.elm-lang.org/architecture/
103103
/// and https://guide.elm-lang.org/webapps/structure.html
104104
/// for more about this approach.
105-
public final class Store<State, Environment, Action>: ObservableObject
105+
public final class Store<State, Action, Environment>: ObservableObject
106106
where State: Equatable {
107107
/// Stores cancellables by ID
108108
private(set) var cancellables: [UUID: AnyCancellable] = [:]
@@ -112,8 +112,8 @@ where State: Equatable {
112112
/// Update function for state
113113
public var update: (
114114
State,
115-
Environment,
116-
Action
115+
Action,
116+
Environment
117117
) -> Update<State, Action>
118118
/// Environment, which typically holds references to outside information,
119119
/// such as API methods.
@@ -134,8 +134,8 @@ where State: Equatable {
134134
public init(
135135
update: @escaping (
136136
State,
137-
Environment,
138-
Action
137+
Action,
138+
Environment
139139
) -> Update<State, Action>,
140140
state: State,
141141
environment: Environment
@@ -232,7 +232,7 @@ where State: Equatable {
232232
/// `.receive(on: DispatchQueue.main)`).
233233
public func send(_ action: Action) {
234234
// Generate next state and effect
235-
let next = update(self.state, self.environment, action)
235+
let next = update(self.state, action, self.environment)
236236
// Set `state` if changed.
237237
//
238238
// Mutating state (a `@Published` property) will fire `objectWillChange`

Tests/ObservableStoreTests/ObservableStoreTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ final class ObservableStoreTests: XCTestCase {
2929
/// State update function
3030
static func update(
3131
state: Self,
32-
environment: Environment,
33-
action: Action
32+
action: Action,
33+
environment: Environment
3434
) -> Update<Self, Action> {
3535
switch action {
3636
case .increment:
@@ -214,8 +214,8 @@ final class ObservableStoreTests: XCTestCase {
214214
/// Update function for Fx tests (below)
215215
static func update(
216216
state: Self,
217-
environment: Environment,
218-
action: Action
217+
action: Action,
218+
environment: Environment
219219
) -> Update<Self, Action> {
220220
switch action {
221221
case .setTitle(let title):

0 commit comments

Comments
 (0)