Skip to content

Commit 52d709c

Browse files
committed
Add name() & conform to Printable.
1 parent aad1fc8 commit 52d709c

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class TaskConfiguration
6565
}
6666
}
6767

68-
public class Task<Progress, Value, Error>
68+
public class Task<Progress, Value, Error>: Printable
6969
{
7070
public typealias ErrorInfo = (error: Error?, isCancelled: Bool)
7171

@@ -103,6 +103,8 @@ public class Task<Progress, Value, Error>
103103
/// rejected/cancelled tuple info
104104
public internal(set) var errorInfo: ErrorInfo?
105105

106+
public var name: String = "DefaultTask"
107+
106108
public var state: TaskState
107109
{
108110
// return .Cancelled if .Rejected & errorInfo.isCancelled=true
@@ -117,6 +119,11 @@ public class Task<Progress, Value, Error>
117119
return self.machine.state
118120
}
119121

122+
public var description: String
123+
{
124+
return self.name
125+
}
126+
120127
///
121128
/// Creates a new task.
122129
///
@@ -343,6 +350,13 @@ public class Task<Progress, Value, Error>
343350
self._cancel(error: nil)
344351
}
345352

353+
/// Sets task name (method chainable)
354+
public func name(name: String) -> Self
355+
{
356+
self.name = name
357+
return self
358+
}
359+
346360
/// Returns new task that is retryable for `tryCount-1` times.
347361
/// `task.try(n)` is conceptually equal to `task.failure(clonedTask1).failure(clonedTask2)...` with n-1 failure-able.
348362
public func try(maxTryCount: Int) -> Task
@@ -354,7 +368,7 @@ public class Task<Progress, Value, Error>
354368

355369
if initClosure == nil { return self }
356370

357-
let newTask = Task { [weak self] machine, progress, fulfill, _reject, configure in
371+
return Task { [weak self] machine, progress, fulfill, _reject, configure in
358372

359373
var chainedTasks = [self!]
360374
var nextTask: Task = self!
@@ -394,9 +408,7 @@ public class Task<Progress, Value, Error>
394408
}
395409
}
396410

397-
}
398-
399-
return newTask
411+
}.name("\(self.name)-try(\(maxTryCount))")
400412
}
401413

402414
///
@@ -434,7 +446,7 @@ public class Task<Progress, Value, Error>
434446
///
435447
public func then<Progress2, Value2>(thenClosure: (Value?, ErrorInfo?) -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
436448
{
437-
let newTask = Task<Progress2, Value2, Error> { [weak self] machine, progress, fulfill, _reject, configure in
449+
return Task<Progress2, Value2, Error> { [weak self] machine, progress, fulfill, _reject, configure in
438450

439451
let bind = { [weak machine] (value: Value?, errorInfo: ErrorInfo?) -> Void in
440452
let innerTask = thenClosure(value, errorInfo)
@@ -503,9 +515,7 @@ public class Task<Progress, Value, Error>
503515
}
504516
}
505517

506-
}
507-
508-
return newTask
518+
}.name("\(self.name)-then")
509519
}
510520

511521
///
@@ -527,7 +537,7 @@ public class Task<Progress, Value, Error>
527537
///
528538
public func success<Progress2, Value2>(successClosure: Value -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
529539
{
530-
let newTask = Task<Progress2, Value2, Error> { [weak self] machine, progress, fulfill, _reject, configure in
540+
return Task<Progress2, Value2, Error> { [weak self] machine, progress, fulfill, _reject, configure in
531541

532542
let bind = { [weak machine] (value: Value) -> Void in
533543
let innerTask = successClosure(value)
@@ -582,9 +592,7 @@ public class Task<Progress, Value, Error>
582592
}
583593
}
584594

585-
}
586-
587-
return newTask
595+
}.name("\(self.name)-success")
588596
}
589597

590598
///
@@ -608,7 +616,7 @@ public class Task<Progress, Value, Error>
608616
///
609617
public func failure(failureClosure: ErrorInfo -> Task) -> Task
610618
{
611-
let newTask = Task { [weak self] machine, progress, fulfill, _reject, configure in
619+
return Task { [weak self] machine, progress, fulfill, _reject, configure in
612620

613621
let bind = { [weak machine] (errorInfo: ErrorInfo) -> Void in
614622
let innerTask = failureClosure(errorInfo)
@@ -664,9 +672,7 @@ public class Task<Progress, Value, Error>
664672
}
665673
}
666674

667-
}
668-
669-
return newTask
675+
}.name("\(self.name)-failure")
670676
}
671677

672678
public func pause() -> Bool
@@ -740,7 +746,7 @@ extension Task
740746
configure.resume = { self.resumeAll(tasks); return }
741747
configure.cancel = { self.cancelAll(tasks); return }
742748

743-
}
749+
}.name("Task.all")
744750
}
745751

746752
public class func any(tasks: [Task]) -> Task
@@ -783,7 +789,7 @@ extension Task
783789
configure.resume = { self.resumeAll(tasks); return }
784790
configure.cancel = { self.cancelAll(tasks); return }
785791

786-
}
792+
}.name("Task.any")
787793
}
788794

789795
/// Returns new task which performs all given tasks and stores only fulfilled values.
@@ -824,7 +830,7 @@ extension Task
824830
configure.resume = { self.resumeAll(tasks); return }
825831
configure.cancel = { self.cancelAll(tasks); return }
826832

827-
}
833+
}.name("Task.some")
828834
}
829835

830836
public class func cancelAll(tasks: [Task])

0 commit comments

Comments
 (0)