@@ -18,6 +18,7 @@ public protocol Event {}
18
18
public protocol AnyCommand {
19
19
func _execute( state: Any , core: Any )
20
20
func _canExecute( state: Any ) -> Bool
21
+ func _didExpire( state: Any , core: Any )
21
22
}
22
23
23
24
extension AnyCommand {
@@ -28,6 +29,7 @@ public protocol Command: AnyCommand {
28
29
associatedtype StateType : State
29
30
func execute( state: StateType , core: Core < StateType > )
30
31
func canExecute( state: StateType ) -> Bool
32
+ func didExpire( state: StateType , core: Core < StateType > )
31
33
}
32
34
33
35
extension Command {
@@ -36,6 +38,10 @@ extension Command {
36
38
return true
37
39
}
38
40
41
+ public func didExpire( state: StateType , core: Core < StateType > ) {
42
+ // do nothing by default
43
+ }
44
+
39
45
public func _canExecute( state: Any ) -> Bool {
40
46
if let state = state as? StateType {
41
47
return canExecute ( state: state)
@@ -44,6 +50,12 @@ extension Command {
44
50
}
45
51
}
46
52
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
+
47
59
public func _execute( state: Any , core: Any ) {
48
60
if let state = state as? StateType , let core = core as? Core < StateType > {
49
61
execute ( state: state, core: core)
@@ -196,7 +208,10 @@ public class Core<StateType: State> {
196
208
}
197
209
let now = Date ( )
198
210
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
+ }
200
215
self . middlewares. forEach { $0. middleware. _process ( event: event, state: state) }
201
216
}
202
217
}
0 commit comments