Skip to content

Commit

Permalink
Implemented Synchronizable as a protocol extension to make it read be…
Browse files Browse the repository at this point in the history
…tter.

Added @NoEscape to the public Promise initializer since the block is always run inline.
  • Loading branch information
jkolb committed Aug 24, 2015
1 parent 0848db9 commit 002f9bd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
6 changes: 3 additions & 3 deletions FranticApparatus/Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class Promise<T> : Synchronizable {
public let synchronizationQueue: DispatchQueue
private var state: State<T>

public init(_ resolver: (fulfill: T -> (), reject: ErrorType -> (), isCancelled: () -> Bool) -> ()) {
public init(@noescape _ resolver: (fulfill: T -> (), reject: ErrorType -> (), isCancelled: () -> Bool) -> ()) {
self.state = .Pending(PendingInfo())
self.synchronizationQueue = GCDQueue.serial("net.franticapparatus.Promise")

Expand Down Expand Up @@ -95,7 +95,7 @@ public class Promise<T> : Synchronizable {
}

private func resolve(result: Result<T>) {
synchronizeWrite(self) { promise in
synchronizeWrite { promise in
promise.transition(result)
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public class Promise<T> : Synchronizable {
}
}

synchronizeWrite(self) { parent in
self.synchronizeWrite { parent in
switch parent.state {
case .Pending(let info):
info.onFulfilled.append(fulfiller)
Expand Down
48 changes: 24 additions & 24 deletions FranticApparatus/Synchronizable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ public protocol Synchronizable: class {
var synchronizationQueue: DispatchQueue { get }
}

public func synchronizeRead<T: Synchronizable>(synchronizable: T, block: (T) -> ()) {
synchronizable.synchronizationQueue.dispatch { [weak synchronizable] in
if let strongSynchronizable = synchronizable {
block(strongSynchronizable)
public extension Synchronizable {
public func synchronizeRead(synchronized: (Self) -> ()) {
synchronizationQueue.dispatch { [weak self] in
guard let strongSelf = self else { return }
synchronized(strongSelf)
}
}
}

public func synchronizeRead<T: Synchronizable, R>(synchronizable: T, block: (T) -> R) -> R {
var result: R!
synchronizable.synchronizationQueue.dispatchAndWait { [unowned synchronizable] in
result = block(synchronizable)

public func synchronizeRead<ResultType>(synchronized: (Self) -> ResultType) -> ResultType {
var result: ResultType!
synchronizationQueue.dispatchAndWait { [unowned self] in
result = synchronized(self)
}
return result
}
return result
}

public func synchronizeRead<T: Synchronizable, R>(synchronizable: T, block: (T) -> R?) -> R? {
var result: R?
synchronizable.synchronizationQueue.dispatchAndWait { [unowned synchronizable] in
result = block(synchronizable)

public func synchronizeRead<ResultType>(synchronized: (Self) -> ResultType?) -> ResultType? {
var result: ResultType?
synchronizationQueue.dispatchAndWait { [unowned self] in
result = synchronized(self)
}
return result
}
return result
}

public func synchronizeWrite<T: Synchronizable>(synchronizable: T, block: (T) -> ()) {
synchronizable.synchronizationQueue.dispatchSerialized { [weak synchronizable] in
if let strongSynchronizable = synchronizable {
block(strongSynchronizable)

public func synchronizeWrite(synchronized: (Self) -> ()) {
synchronizationQueue.dispatchSerialized { [weak self] in
guard let strongSelf = self else { return }
synchronized(strongSelf)
}
}
}
8 changes: 4 additions & 4 deletions FranticApparatus/URLPromiseFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class SimpleURLSessionDataDelegate : NSObject, NSURLSessionDataDelegate,
public let synchronizationQueue: DispatchQueue = GCDQueue.concurrent("net.franticapparatus.PromiseSession")

func complete(task: NSURLSessionTask, error: NSError?) {
synchronizeRead(self) { (delegate) in
synchronizeRead { delegate in
if let callbacksAndData = delegate.callbacksAndData[task] {
if error == nil {
let value = URLResponse(metadata: task.response!, data: callbacksAndData.responseData)
Expand All @@ -76,14 +76,14 @@ public class SimpleURLSessionDataDelegate : NSObject, NSURLSessionDataDelegate,
}
}

synchronizeWrite(delegate) { (delegate) in
delegate.synchronizeWrite { delegate in
delegate.callbacksAndData[task] = nil
}
}
}

func accumulate(task: NSURLSessionTask, data: NSData) {
synchronizeWrite(self) { (delegate) in
synchronizeWrite { delegate in
if let callbacksAndData = delegate.callbacksAndData[task] {
if callbacksAndData.isCancelled() {
task.cancel()
Expand All @@ -101,7 +101,7 @@ public class SimpleURLSessionDataDelegate : NSObject, NSURLSessionDataDelegate,
return Promise<URLResponse> { (fulfill, reject, isCancelled) -> () in
let threadSafeRequest = request.copy() as! NSURLRequest

synchronizeWrite(self) { (delegate) in
self.synchronizeWrite { delegate in
if isCancelled() {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
E544BDB01AF4695B008271E7 /* FranticApparatus.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E544BDAF1AF4695B008271E7 /* FranticApparatus.framework */; };
E544BDB11AF4695B008271E7 /* FranticApparatus.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = E544BDAF1AF4695B008271E7 /* FranticApparatus.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
E59910A619D903D900E07A83 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E59910A519D903D900E07A83 /* AppDelegate.swift */; };
E59910A819D903D900E07A83 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E59910A719D903D900E07A83 /* ViewController.swift */; };
E59910AB19D903D900E07A83 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E59910A919D903D900E07A83 /* Main.storyboard */; };
Expand All @@ -34,15 +32,13 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
E544BDB11AF4695B008271E7 /* FranticApparatus.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
E544BDAF1AF4695B008271E7 /* FranticApparatus.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = FranticApparatus.framework; path = "/Users/jkolb/Library/Developer/Xcode/DerivedData/FranticApparatus-auswscgsauvevpgexacziduqkbjc/Build/Products/Debug-iphoneos/FranticApparatus.framework"; sourceTree = "<absolute>"; };
E59910A019D903D900E07A83 /* FranticApparatusExample_iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FranticApparatusExample_iOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
E59910A419D903D900E07A83 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E59910A519D903D900E07A83 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -60,7 +56,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E544BDB01AF4695B008271E7 /* FranticApparatus.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -77,7 +72,6 @@
E599109719D903D900E07A83 = {
isa = PBXGroup;
children = (
E544BDAF1AF4695B008271E7 /* FranticApparatus.framework */,
E59910A219D903D900E07A83 /* FranticApparatusExample_iOS */,
E59910B819D903D900E07A83 /* FranticApparatusExample_iOSTests */,
E59910A119D903D900E07A83 /* Products */,
Expand Down

0 comments on commit 002f9bd

Please sign in to comment.