Skip to content

Commit cda3ed4

Browse files
committed
Only call user enabled closure when ActionState.value changes
1 parent 1672883 commit cda3ed4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Sources/Action.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,26 @@ public final class Action<Input, Output, Error: Swift.Error> {
180180

181181
private struct ActionState {
182182
var isExecuting: Bool = false
183-
var value: Any
184-
private let userEnabled: (Any) -> Bool
183+
184+
var value: Any {
185+
didSet {
186+
userEnabled = userEnabledClosure(value)
187+
}
188+
}
189+
190+
private var userEnabled: Bool
191+
private let userEnabledClosure: (Any) -> Bool
185192

186193
init(value: Any, isEnabled: @escaping (Any) -> Bool) {
187194
self.value = value
188-
self.userEnabled = isEnabled
195+
self.userEnabled = isEnabled(value)
196+
self.userEnabledClosure = isEnabled
189197
}
190198

191199
/// Whether the action should be enabled for the given combination of user
192200
/// enabledness and executing status.
193201
fileprivate var isEnabled: Bool {
194-
return userEnabled(value) && !isExecuting
202+
return userEnabled && !isExecuting
195203
}
196204
}
197205

0 commit comments

Comments
 (0)