Skip to content

Commit ef971ea

Browse files
committed
Weakify progressValue if possible, for less memory footprint.
1 parent 1ed3720 commit ef971ea

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class Task<Progress, Value, Error>: Printable
7474

7575
public var state: TaskState { return self._machine.state }
7676

77-
/// progress value
77+
/// progress value (NOTE: always nil when `weakified = true`)
7878
public var progress: Progress? { return self._machine.progress }
7979

8080
/// fulfilled value
@@ -118,7 +118,7 @@ public class Task<Progress, Value, Error>: Printable
118118
{
119119
self._weakified = weakified
120120
self._paused = paused
121-
self._machine = _Machine(paused: paused)
121+
self._machine = _Machine(weakified: weakified, paused: paused)
122122

123123
let _initClosure: _InitClosure = { _, progress, fulfill, _reject, configure in
124124
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
@@ -192,7 +192,7 @@ public class Task<Progress, Value, Error>: Printable
192192
{
193193
self._weakified = weakified
194194
self._paused = paused
195-
self._machine = _Machine(paused: paused)
195+
self._machine = _Machine(weakified: weakified, paused: paused)
196196

197197
self.setup(weakified, paused: paused, _initClosure)
198198
}
@@ -325,6 +325,8 @@ public class Task<Progress, Value, Error>: Printable
325325
///
326326
/// - e.g. task.progress { oldProgress, newProgress in ... }
327327
///
328+
/// NOTE: `oldProgress` is always nil when `weakified = true`
329+
///
328330
public func progress(progressClosure: ProgressTuple -> Void) -> Task
329331
{
330332
self._machine.progressTupleHandlers.append(progressClosure)

SwiftTask/_StateMachine.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class _StateMachine<Progress, Value, Error>
1414
{
1515
internal typealias ErrorInfo = Task<Progress, Value, Error>.ErrorInfo
1616

17+
internal let weakified: Bool
1718
internal var state: TaskState
1819

1920
internal var progress: Progress?
@@ -25,15 +26,19 @@ internal class _StateMachine<Progress, Value, Error>
2526

2627
internal let configuration = TaskConfiguration()
2728

28-
internal init(paused: Bool)
29+
internal init(weakified: Bool, paused: Bool)
2930
{
31+
self.weakified = weakified
3032
self.state = paused ? .Paused : .Running
3133
}
3234

3335
internal func handleProgress(progress: Progress)
3436
{
3537
let oldProgress = self.progress
36-
self.progress = progress
38+
39+
if !self.weakified {
40+
self.progress = progress
41+
}
3742

3843
if self.state == .Running {
3944
for handler in self.progressTupleHandlers {

0 commit comments

Comments
 (0)