Skip to content

add .success / .failure methods that cause sideeffects only #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions SwiftTask/SwiftTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ public class Task<Progress, Value, Error>: Cancellable, CustomStringConvertible
})
}

///
/// creates rejected task with ErrorInfo
///
/// - e.g. Task<P, V, E>(errorInfo: someErrorInfo)
///
internal convenience init(errorInfo: ErrorInfo)
{
self.init(_initClosure: { machine, progress, fulfill, _reject, configure in
_reject(errorInfo)
})

self.name = "RejectedTask"
}

/// internal-init for accessing `machine` inside `_initClosure`
/// (NOTE: _initClosure has _RejectInfoHandler as argument)
internal init(weakified: Bool = false, paused: Bool = false, _initClosure: _InitClosure)
Expand Down Expand Up @@ -447,6 +461,26 @@ public class Task<Progress, Value, Error>: Cancellable, CustomStringConvertible
}
}

///
/// success (fulfilled) + closure returning nothing
/// used as transparent (i.e. it doesn't affect passed Task) handler.
///
/// - e.g. task.success { value -> Void in ... }
///
public func success(successClosure: Value -> Void) -> Task
{
var dummyCanceller: Canceller? = nil
return self.success(&dummyCanceller, successClosure)
}

public func success<C: Canceller>(inout canceller: C?, _ successClosure: Value -> Void) -> Task
{
return self.success(&canceller) { (value: Value) -> Task in
successClosure(value)
return Task(value: value)
}
}

///
/// success (fulfilled) + closure returning **value**
///
Expand Down Expand Up @@ -496,6 +530,28 @@ public class Task<Progress, Value, Error>: Cancellable, CustomStringConvertible
}.name("\(self.name)-success")
}

///
/// failure (rejected or cancelled) + closure returning nothing
/// used as transparent (i.e. it doesn't affect passed Task) handler.
///
/// - e.g. task.failure { errorInfo -> Void in ... }
/// - e.g. task.failure { error, isCancelled -> Void in ... }
///

public func failure(failureClosure: ErrorInfo -> Void) -> Task
{
var dummyCanceller: Canceller? = nil
return self.failure(&dummyCanceller, failureClosure)
}

public func failure<C: Canceller>(inout canceller: C?, _ failureClosure: ErrorInfo -> Void) -> Task
{
return self.failure(&canceller) { (errorInfo: ErrorInfo) -> Task in
failureClosure(errorInfo)
return Task(errorInfo: errorInfo)
}
}

///
/// failure (rejected or cancelled) + closure returning **value**
///
Expand Down