@@ -7,7 +7,11 @@ public struct Effect<Action>: Sendable {
77 enum Operation : Sendable {
88 case none
99 case publisher( AnyPublisher < Action , Never > )
10- case run( TaskPriority ? = nil , @Sendable ( _ send: Send < Action > ) async -> Void )
10+ case run(
11+ TaskPriority ? = nil ,
12+ _ name: String ? = nil ,
13+ @Sendable ( _ send: Send < Action > ) async -> Void
14+ )
1115 }
1216
1317 @usableFromInline
@@ -76,6 +80,7 @@ extension Effect {
7680 /// - Parameters:
7781 /// - priority: Priority of the underlying task. If `nil`, the priority will come from
7882 /// `Task.currentPriority`.
83+ /// - name: An optional name to associate with the task that runs this effect.
7984 /// - operation: The operation to execute.
8085 /// - handler: An error handler, invoked if the operation throws an error other than
8186 /// `CancellationError`.
@@ -86,6 +91,7 @@ extension Effect {
8691 /// - Returns: An effect wrapping the given asynchronous work.
8792 public static func run(
8893 priority: TaskPriority ? = nil ,
94+ name: String ? = nil ,
8995 operation: @escaping @Sendable ( _ send: Send < Action > ) async throws -> Void ,
9096 catch handler: ( @Sendable ( _ error: any Error , _ send: Send < Action > ) async -> Void ) ? = nil ,
9197 fileID: StaticString = #fileID,
@@ -95,7 +101,7 @@ extension Effect {
95101 ) -> Self {
96102 withEscapedDependencies { escaped in
97103 Self (
98- operation: . run( priority) { send in
104+ operation: . run( priority, name ) { send in
99105 await escaped. yield {
100106 do {
101107 try await operation ( send)
@@ -268,14 +274,14 @@ extension Effect {
268274 . eraseToAnyPublisher ( )
269275 )
270276 )
271- case let ( . run( lhsPriority, lhsOperation) , . run( rhsPriority, rhsOperation) ) :
277+ case let ( . run( lhsPriority, lhsName , lhsOperation) , . run( rhsPriority, rhsName , rhsOperation) ) :
272278 return Self (
273279 operation: . run { send in
274280 await withTaskGroup ( of: Void . self) { group in
275- group. addTask ( priority: lhsPriority) {
281+ group. addTask ( name : lhsName , priority: lhsPriority) {
276282 await lhsOperation ( send)
277283 }
278- group. addTask ( priority: rhsPriority) {
284+ group. addTask ( name : rhsName , priority: rhsPriority) {
279285 await rhsOperation ( send)
280286 }
281287 }
@@ -328,16 +334,16 @@ extension Effect {
328334 . eraseToAnyPublisher ( )
329335 )
330336 )
331- case let ( . run( lhsPriority, lhsOperation) , . run( rhsPriority, rhsOperation) ) :
337+ case let ( . run( lhsPriority, lhsName , lhsOperation) , . run( rhsPriority, rhsName , rhsOperation) ) :
332338 return Self (
333339 operation: . run { send in
334340 if let lhsPriority {
335- await Task ( priority: lhsPriority) { await lhsOperation ( send) } . cancellableValue
341+ await Task ( name : lhsName , priority: lhsPriority) { await lhsOperation ( send) } . cancellableValue
336342 } else {
337343 await lhsOperation ( send)
338344 }
339345 if let rhsPriority {
340- await Task ( priority: rhsPriority) { await rhsOperation ( send) } . cancellableValue
346+ await Task ( name : rhsName , priority: rhsPriority) { await rhsOperation ( send) } . cancellableValue
341347 } else {
342348 await rhsOperation ( send)
343349 }
@@ -372,10 +378,10 @@ extension Effect {
372378 . eraseToAnyPublisher ( )
373379 )
374380 )
375- case let . run( priority, operation) :
381+ case let . run( priority, name , operation) :
376382 return withEscapedDependencies { escaped in
377383 . init(
378- operation: . run( priority) { send in
384+ operation: . run( priority, name ) { send in
379385 await escaped. yield {
380386 await operation (
381387 Send { action in
@@ -389,3 +395,40 @@ extension Effect {
389395 }
390396 }
391397}
398+
399+ #if !swift(>=6.2)
400+ public extension Task {
401+ /// Backwards compatibility shim for named Task initializers introduced in Swift 6.2
402+ ///
403+ /// This extension provides the `name` parameter for Task initializers on Swift versions
404+ /// prior to 6.2.
405+ ///
406+ /// - Important: On Swift < 6.2, the `name` parameter is accepted but ignored.
407+ /// No naming functionality is provided - this is purely for API compatibility.
408+ ///
409+ /// Example usage:
410+ /// ```swift
411+ /// let task = Task(name: "DataLoader") {
412+ /// await loadUserData()
413+ /// }
414+ /// ```
415+ @discardableResult
416+ init (
417+ name: String ,
418+ priority: TaskPriority ? = nil ,
419+ operation: @escaping @Sendable ( ) async -> Success
420+ ) where Failure == Never {
421+ self . init ( priority: priority, operation: operation)
422+ }
423+
424+ /// Backwards compatibility shim for named throwing Task initializers
425+ @discardableResult
426+ init (
427+ name: String ,
428+ priority: TaskPriority ? = nil ,
429+ operation: @escaping @Sendable ( ) async throws -> Success
430+ ) where Failure == Error {
431+ self . init ( priority: priority, operation: operation)
432+ }
433+ }
434+ #endif
0 commit comments