Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
vmartinelli committed Aug 13, 2016
2 parents a8a5f3a + de7ca39 commit 3bcd019
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AlecrimAsyncKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "AlecrimAsyncKit"
s.version = "3.0-beta.3"
s.version = "2.2"
s.summary = "Bringing async and await to Swift world with some flavouring."
s.homepage = "https://github.com/Alecrim/AlecrimAsyncKit"

Expand Down
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A failable task is created passing a closure to the `async` global function (tha
// this code is running in background
do {
// the task is started immediately
let task = calculate()
let task = asyncCalculate()

// we can do other things while the calculation is made...
// ...
Expand All @@ -40,8 +40,7 @@ catch let error {
print(error)
}

@warn_unused_result
func calculate() -> Task<Int> {
func asyncCalculate() -> Task<Int> {
return async {
var value = 0

Expand Down Expand Up @@ -69,11 +68,10 @@ You mark a task as non-failable using `NonFailableTask<T>` class instead of `Tas

```swift
// this code is running in background
let value = await { calculate() }
let value = await { asyncCalculate() }
print("The result is \(value)")

@warn_unused_result
func calculate() -> NonFailableTask<Int> {
func asyncCalculate() -> NonFailableTask<Int> {
return async {
var value = 0

Expand Down Expand Up @@ -119,7 +117,6 @@ catch let error {

extension CKDatabase {

@warn_unused_result
public func asyncPerformQuery(query: CKQuery, inZoneWithID zoneID: CKRecordZoneID?) -> Task<[CKRecord]> {
return asyncEx { task in
self.performQuery(query, inZoneWithID: zoneID) { records, error in
Expand Down Expand Up @@ -160,8 +157,7 @@ One task may have one or more conditions. Different tasks can have the same cond
The **AlecrimAsyncKit** framework provides some predefined conditions, but you can create others. The `MutuallyExclusiveCondition` is one special kind of condition that prevents tasks that share the same behavior from running at the same time.

```swift
@warn_unused_result
func doSomething() -> Task<Void> {
func asyncDoSomething() -> Task<Void> {
let condition = TaskCondition { result in
if ... {
result(.Satisfied)
Expand All @@ -187,13 +183,12 @@ A task can have its beginning and its ending observed using the `TaskObserver` c
The **AlecrimAsyncKit** framework provides some predefined observers, but you can create others.

```swift
@warn_unused_result
func doSomething() -> Task<Void> {
func asyncDoSomething() -> Task<Void> {
let observer = TaskObserver()
.didStart { _ in
.didStartTask { _ in
print("The task was started...")
}
.didFinish { _ in
.didFinishTask { _ in
print("The task was finished...")
}

Expand Down Expand Up @@ -224,7 +219,7 @@ If you want to handle its completion you may use methods from `TaskAwaiter` help
```swift
// this code is running on the main thread

let _ = calculate()
let _ = asyncCalculate()
.didFinishWithValue { value in
print("The result is \(value)")
}
Expand All @@ -245,8 +240,7 @@ let _ = calculate()
}
}

@warn_unused_result
func calculate() -> Task<Int> {
func asyncCalculate() -> Task<Int> {
return async {
var value = 0

Expand Down
4 changes: 2 additions & 2 deletions Source/AlecrimAsyncKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 249;
CURRENT_PROJECT_VERSION = 255;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -423,7 +423,7 @@
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 249;
CURRENT_PROJECT_VERSION = 255;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand Down
36 changes: 21 additions & 15 deletions Source/AlecrimAsyncKit/Core/AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ public func await<V>(@noescape closure: () -> NonFailableTask<V>) -> V {

public func await<V>(task: NonFailableTask<V>) -> V {
// this should never be called, but just in case...
if let parentTask = NSThread.currentThread().task as? CancellableTask, let currentTask = task as? CancellableTask where currentTask !== parentTask {
parentTask.cancellationHandler = { [weak currentTask] in
currentTask?.cancel()
}
if let parentTask = NSThread.currentThread().task as? CancellableTask, let currentTask = task as? CancellableTask where parentTask !== currentTask {
currentTask.internalInheritCancellation(from: parentTask)
}

//
Expand All @@ -95,10 +93,8 @@ public func await<V>(@noescape closure: () -> Task<V>) throws -> V {

public func await<V>(task: Task<V>) throws -> V {
//
if let parentTask = NSThread.currentThread().task as? CancellableTask, let currentTask = task as? CancellableTask where currentTask !== parentTask {
parentTask.cancellationHandler = { [weak currentTask] in
currentTask?.cancel()
}
if let parentTask = NSThread.currentThread().task as? CancellableTask where parentTask !== task {
task.internalInheritCancellation(from: parentTask)
}

//
Expand All @@ -114,13 +110,13 @@ public func await<V>(task: Task<V>) throws -> V {
// MARK: - Helper methods

@warn_unused_result
public func delay(timeInterval: NSTimeInterval) -> NonFailableTask<Void> {
return sleep(forTimeInterval: timeInterval)
public func asyncDelay(in queue: NSOperationQueue = _defaultTaskQueue, timeInterval: NSTimeInterval) -> NonFailableTask<Void> {
return asyncSleep(in: queue, forTimeInterval: timeInterval)
}

@warn_unused_result
public func sleep(forTimeInterval ti: NSTimeInterval) -> NonFailableTask<Void> {
return asyncEx { t in
public func asyncSleep(in queue: NSOperationQueue = _defaultTaskQueue, forTimeInterval ti: NSTimeInterval) -> NonFailableTask<Void> {
return asyncEx(in: queue) { t in
let when = dispatch_time(DISPATCH_TIME_NOW, Int64(ti * Double(NSEC_PER_SEC)))
dispatch_after(when, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
t.finish()
Expand All @@ -129,17 +125,27 @@ public func sleep(forTimeInterval ti: NSTimeInterval) -> NonFailableTask<Void> {
}

@warn_unused_result
public func sleep(until date: NSDate) -> NonFailableTask<Void> {
public func asyncSleep(in queue: NSOperationQueue = _defaultTaskQueue, until date: NSDate) -> NonFailableTask<Void> {
let now = NSDate()
if now.compare(date) == .OrderedAscending {
let ti = date.timeIntervalSinceDate(now)
return sleep(forTimeInterval: ti)
return asyncSleep(in: queue, forTimeInterval: ti)
}
else {
return async {}
return async(in: queue) {}
}
}

@warn_unused_result
public func asyncValue<V>(in queue: NSOperationQueue = _defaultTaskQueue, value: V) -> Task<V> {
return async(in: queue) { return value }
}

@warn_unused_result
public func asyncError<V>(in queue: NSOperationQueue = _defaultTaskQueue, error: ErrorType) -> Task<V> {
return async(in: queue) { throw error }
}

// MARK: -

private func createdTask<T: InitializableTask>(queue queue: NSOperationQueue, qualityOfService: NSQualityOfService?, taskPriority: TaskPriority?, conditions: [TaskCondition]?, observers: [TaskObserver]?, asynchronous: Bool, closure: (T) -> Void) -> T {
Expand Down
21 changes: 15 additions & 6 deletions Source/AlecrimAsyncKit/Core/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public protocol CancellableTask: TaskProtocol {

extension CancellableTask {

@available(*, deprecated, message="A cancellable task now forward cancellation to their cancellable child tasks when they are awaited.")
@available(*, deprecated)
public func forwardCancellation(to task: CancellableTask) -> Self {
self.cancellationHandler = { [weak task] in
task?.cancel()
Expand All @@ -40,13 +40,23 @@ extension CancellableTask {
return self
}

@available(*, deprecated, message="Cancellable child tasks when awaited now inherit cancellation from the parent cancellable task.")
@available(*, deprecated)
public func inheritCancellation(from task: CancellableTask) -> Self {
task.forwardCancellation(to: self)
task.cancellationHandler = { [weak self] in
self?.cancel()
}

return self
}

internal func internalInheritCancellation(from task: CancellableTask) -> Self {
task.cancellationHandler = { [weak self] in
self?.cancel()
}

return self
}

}

// MARK: - ValueReportingTask
Expand Down Expand Up @@ -82,7 +92,6 @@ public protocol FailableTaskProtocol: CancellableTask, ValueReportingTask, Error

extension FailableTaskProtocol {


public func finish(with value: Self.ValueType!, or error: ErrorType?) {
if let error = error {
self.finish(with: error)
Expand All @@ -97,7 +106,7 @@ extension FailableTaskProtocol {
/// - parameter task: The task the execution is forward to.
public func forward<T: FailableTaskProtocol where T.ValueType == Self.ValueType>(to task: T, inheritCancellation: Bool = true) {
if inheritCancellation {
task.inheritCancellation(from: self)
task.internalInheritCancellation(from: self)
}

task.waitUntilFinished()
Expand Down
2 changes: 1 addition & 1 deletion Source/AlecrimAsyncKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.0</string>
<string>2.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit 3bcd019

Please sign in to comment.