Skip to content

Commit 193bfc4

Browse files
committedFeb 7, 2017
Allow commands to know they've expired.
1 parent e244b3e commit 193bfc4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
 

‎Sources/Reactor.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public protocol Event {}
1818
public protocol AnyCommand {
1919
func _execute(state: Any, core: Any)
2020
func _canExecute(state: Any) -> Bool
21+
func _didExpire(state: Any, core: Any)
2122
}
2223

2324
extension AnyCommand {
@@ -28,6 +29,7 @@ public protocol Command: AnyCommand {
2829
associatedtype StateType: State
2930
func execute(state: StateType, core: Core<StateType>)
3031
func canExecute(state: StateType) -> Bool
32+
func didExpire(state: StateType, core: Core<StateType>)
3133
}
3234

3335
extension Command {
@@ -36,6 +38,10 @@ extension Command {
3638
return true
3739
}
3840

41+
public func didExpire(state: StateType, core: Core<StateType>) {
42+
// do nothing by default
43+
}
44+
3945
public func _canExecute(state: Any) -> Bool {
4046
if let state = state as? StateType {
4147
return canExecute(state: state)
@@ -44,6 +50,12 @@ extension Command {
4450
}
4551
}
4652

53+
public func _didExpire(state: Any, core: Any) {
54+
if let state = state as? StateType, let core = core as? Core<StateType> {
55+
didExpire(state: state, core: core)
56+
}
57+
}
58+
4759
public func _execute(state: Any, core: Any) {
4860
if let state = state as? StateType, let core = core as? Core<StateType> {
4961
execute(state: state, core: core)
@@ -196,7 +208,10 @@ public class Core<StateType: State> {
196208
}
197209
let now = Date()
198210
let expired = self.commands.enumerated().filter { $1.expiresAt < now }
199-
expired.forEach { self.commands.remove(at: $0.offset) }
211+
expired.forEach {
212+
self.commands.remove(at: $0.offset)
213+
$1.command._didExpire(state: state, core: self)
214+
}
200215
self.middlewares.forEach { $0.middleware._process(event: event, state: state) }
201216
}
202217
}

0 commit comments

Comments
 (0)