@@ -53,33 +53,40 @@ extension ReactorBase {
5353
5454// MARK: - Cancellation Support
5555
56- public struct CancelId : Hashable {
57- let id : AnyHashable
56+ public struct CancelId : Hashable , Sendable {
57+ let id : UUID
5858 let mode : Mode
5959
60- public init ( id: AnyHashable , mode: Mode ) {
60+ public init ( id: UUID , mode: Mode ) {
6161 self . id = id
6262 self . mode = mode
6363 }
6464
65- public struct Mode : OptionSet , Hashable {
65+ public struct Mode : OptionSet , Hashable , Sendable {
6666 public let rawValue : Int
6767
6868 public init ( rawValue: Int ) {
6969 self . rawValue = rawValue
7070 }
7171
72- public static let lifecycle = Mode ( rawValue: 1 << 0 )
73- public static let inFlight = Mode ( rawValue: 1 << 1 )
72+ public static var lifecycle : Mode {
73+ Mode ( rawValue: 1 << 0 )
74+ }
75+
76+ public static var inFlight : Mode {
77+ Mode ( rawValue: 1 << 1 )
78+ }
7479 }
7580}
7681
77- struct TasksHolder {
78- @MainActor
79- static var tasks = [ TaskKey : Task < Void , Never > ] ( )
82+ @MainActor
83+ class TasksHolder {
84+ var tasks = [ TaskKey : Task < Void , Never > ] ( )
85+
86+ static var shared : TasksHolder = TasksHolder ( )
8087}
8188
82- struct TaskKey : Hashable {
89+ struct TaskKey : Hashable , Sendable {
8390 let reactorId : ObjectIdentifier
8491 let id : CancelId
8592
@@ -95,46 +102,46 @@ extension ReactorBase {
95102 let key = TaskKey ( reactor: self , id: id)
96103
97104 if id. mode. contains ( . inFlight) {
98- TasksHolder . tasks [ key] ? . cancel ( )
105+ TasksHolder . shared . tasks [ key] ? . cancel ( )
99106 }
100107
101108 let task = Task {
102109 await self . action ( action)
103110 }
104111
105- TasksHolder . tasks [ key] = task
112+ TasksHolder . shared . tasks [ key] = task
106113
107114 await task. value
108115
109116 if !task. isCancelled {
110- TasksHolder . tasks. removeValue ( forKey: key)
117+ TasksHolder . shared . tasks. removeValue ( forKey: key)
111118 }
112119 }
113120
114121 public func send( _ action: Action , id: CancelId ) {
115122 Task { await self . action ( action, id: id) }
116123 }
117124
118- public func lifecycleTask( _ action: @escaping @Sendable ( ) async -> Void ) {
125+ public func lifecycleTask( _ action: @escaping @Sendable ( ) async -> ( ) ) {
119126 Task { @MainActor in
120127 let key = TaskKey ( reactor: self , id: . init( id: UUID ( ) , mode: . lifecycle) )
121128
122129 let task = Task . detached {
123130 await action ( )
124- await MainActor . run { _ = TasksHolder . tasks. removeValue ( forKey: key) }
131+ await MainActor . run { _ = TasksHolder . shared . tasks. removeValue ( forKey: key) }
125132 }
126133
127- TasksHolder . tasks [ key] = task
134+ TasksHolder . shared . tasks [ key] = task
128135 }
129136 }
130137
131138 public func cancelLifecycleTasks( ) {
132139 Task { @MainActor in
133- let keys = TasksHolder . tasks. keys. filter { $0. id. mode. contains ( . lifecycle) && $0. reactorId == ObjectIdentifier ( self ) }
140+ let keys = TasksHolder . shared . tasks. keys. filter { $0. id. mode. contains ( . lifecycle) && $0. reactorId == ObjectIdentifier ( self ) }
134141
135142 for key in keys {
136- TasksHolder . tasks [ key] ? . cancel ( )
137- TasksHolder . tasks. removeValue ( forKey: key)
143+ TasksHolder . shared . tasks [ key] ? . cancel ( )
144+ TasksHolder . shared . tasks. removeValue ( forKey: key)
138145 }
139146 }
140147 }
0 commit comments