Skip to content

Commit 5f091f7

Browse files
committed
Add init(weakified:initClosure:)
1 parent 7108d03 commit 5f091f7

File tree

1 file changed

+71
-28
lines changed

1 file changed

+71
-28
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,45 +111,64 @@ public class Task<Progress, Value, Error>
111111
return self.machine.state
112112
}
113113

114-
public convenience init(closure: PromiseInitClosure)
114+
///
115+
/// Creates new task.
116+
/// e.g. Task<P, V, E>(weakified: false) { (progress, fulfill, reject, configure) in ... }
117+
///
118+
/// :param: weakified Weakifies progress/fulfill/reject handlers to let player (inner asynchronous implementation inside initClosure) NOT CAPTURE this created new task. Normally, weakified = false should be set to gain "player -> task" retaining, so that task will be automatically deinited when player is deinited. If weakified = true, task must be manually retained somewhere else, or it will be immediately deinited.
119+
///
120+
/// :param: initClosure e.g. { (progress, fulfill, reject, configure) in ... }. fulfill(value) and reject(error) handlers must be called inside this closure, where calling progress(progressValue) handler is optional. Also as options, configure.pause/resume/cancel closures can be set to gain control from outside e.g. task.pause()/resume()/cancel(). When using configure, make sure to use weak modifier when appropriate to avoid "task -> player" retaining which often causes retain cycle.
121+
///
122+
/// :returns: New task.
123+
///
124+
public init(weakified: Bool, initClosure: InitClosure)
115125
{
116-
self.init(closure: { (progress, fulfill, reject, configure) in
117-
closure(fulfill: fulfill, reject: { (error: Error) in reject(error) })
126+
self.setup(weakified) { (progress, fulfill, _reject: ErrorInfo -> Void, configure) in
127+
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
128+
initClosure(progress: progress, fulfill: fulfill, reject: { (error: Error?) in _reject(ErrorInfo(error: error, isCancelled: false)) }, configure: configure)
118129
return
119-
})
130+
}
120131
}
121132

122-
public init(closure: InitClosure)
133+
/// creates task without weakifying progress/fulfill/reject handlers
134+
public convenience init(initClosure: InitClosure)
123135
{
124-
setup { (progress, fulfill, _reject: ErrorInfo -> Void, configure) in
125-
// NOTE: don't expose rejectHandler with ErrorInfo (isCancelled) for public init
126-
closure(progress: progress, fulfill: fulfill, reject: { (error: Error?) in _reject(ErrorInfo(error: error, isCancelled: false)) }, configure: configure)
127-
return
128-
}
136+
self.init(weakified: false, initClosure: initClosure)
129137
}
130138

139+
/// creates fulfilled task
131140
public convenience init(value: Value)
132141
{
133-
self.init(closure: { (progress, fulfill, reject, configure) in
142+
self.init(initClosure: { (progress, fulfill, reject, configure) in
134143
fulfill(value)
135144
return
136145
})
137146
}
138147

148+
/// creates rejected task
139149
public convenience init(error: Error)
140150
{
141-
self.init(closure: { (progress, fulfill, reject, configure) in
151+
self.init(initClosure: { (progress, fulfill, reject, configure) in
142152
reject(error)
143153
return
144154
})
145155
}
146156

147-
internal init(_closure: _InitClosure)
157+
/// creates promise-like task which only allows fulfill & reject (no progress & configure)
158+
public convenience init(promiseInitClosure: PromiseInitClosure)
159+
{
160+
self.init(initClosure: { (progress, fulfill, reject, configure) in
161+
promiseInitClosure(fulfill: fulfill, reject: { (error: Error) in reject(error) })
162+
return
163+
})
164+
}
165+
166+
internal init(_initClosure: _InitClosure)
148167
{
149-
setup(_closure)
168+
self.setup(false, _initClosure)
150169
}
151170

152-
internal func setup(_closure: _InitClosure)
171+
internal func setup(weakified: Bool, _initClosure: _InitClosure)
153172
{
154173
let configuration = Configuration()
155174

@@ -201,23 +220,47 @@ public class Task<Progress, Value, Error>
201220
configuration.clear()
202221
}
203222

204-
let progressHandler: ProgressHandler = { [weak self] (progress: Progress) in
205-
if let self_ = self {
206-
self_.machine <-! (.Progress, progress)
207-
}
208-
}
223+
var progressHandler: ProgressHandler
224+
var fulfillHandler: FulFillHandler
225+
var rejectHandler: _RejectHandler
209226

210-
let fulfillHandler: FulFillHandler = { /*[weak self]*/ (value: Value) in
211-
self.machine <-! (.Fulfill, value) // NOTE: capture self
212-
return
227+
if weakified {
228+
progressHandler = { [weak self] (progress: Progress) in
229+
if let self_ = self {
230+
self_.machine <-! (.Progress, progress)
231+
}
232+
}
233+
234+
fulfillHandler = { [weak self] (value: Value) in
235+
if let self_ = self {
236+
self_.machine <-! (.Fulfill, value)
237+
}
238+
}
239+
240+
rejectHandler = { [weak self] (errorInfo: ErrorInfo) in
241+
if let self_ = self {
242+
self_.machine <-! (.Reject, errorInfo)
243+
}
244+
}
213245
}
214-
215-
let rejectHandler: _RejectHandler = { /*[weak self]*/ (errorInfo: ErrorInfo) in
216-
self.machine <-! (.Reject, errorInfo) // NOTE: capture self
217-
return
246+
else {
247+
progressHandler = { (progress: Progress) in
248+
self.machine <-! (.Progress, progress)
249+
return
250+
}
251+
252+
fulfillHandler = { (value: Value) in
253+
self.machine <-! (.Fulfill, value)
254+
return
255+
}
256+
257+
rejectHandler = { (errorInfo: ErrorInfo) in
258+
self.machine <-! (.Reject, errorInfo)
259+
return
260+
}
218261
}
219262

220-
_closure(progress: progressHandler, fulfill: fulfillHandler, _reject: rejectHandler, configure: configuration)
263+
_initClosure(progress: progressHandler, fulfill: fulfillHandler, _reject: rejectHandler, configure: configuration)
221264

222265
}
223266

0 commit comments

Comments
 (0)