Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Ready to Swift 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Rupérez committed Feb 22, 2017
1 parent 3b1f83d commit 06869b5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 152 deletions.
4 changes: 2 additions & 2 deletions Kommander/CurrentDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import Foundation

open class CurrentDispatcher: MainDispatcher {
public class CurrentDispatcher: MainDispatcher {

public override init() {
super.init()
if let currentOperationQueue = OperationQueue.current {
if let currentOperationQueue = NSOperationQueue.currentQueue() {
operationQueue = currentOperationQueue
if let underlyingQueue = currentOperationQueue.underlyingQueue {
dispatchQueue = underlyingQueue
Expand Down
61 changes: 22 additions & 39 deletions Kommander/Dispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,59 @@ private enum Priority {
case operation, dispatch
}

open class Dispatcher {
public class Dispatcher {

internal final var operationQueue = OperationQueue()
internal final var dispatchQueue = DispatchQueue(label: UUID().uuidString)
internal final var operationQueue = NSOperationQueue()
internal final var dispatchQueue = dispatch_queue_create(NSUUID().UUIDString, DISPATCH_QUEUE_SERIAL)
private final var priority = Priority.operation

public convenience init() {
self.init(name: nil, qos: nil, maxConcurrentOperationCount: OperationQueue.defaultMaxConcurrentOperationCount)
self.init(name: nil, qos: nil, maxConcurrentOperationCount: NSOperationQueueDefaultMaxConcurrentOperationCount)
}

public init(name: String?, qos: QualityOfService?, maxConcurrentOperationCount: Int) {
operationQueue.name = name ?? UUID().uuidString
operationQueue.qualityOfService = qos ?? .default
public init(name: String?, qos: NSQualityOfService?, maxConcurrentOperationCount: Int) {
operationQueue.name = name ?? NSUUID().UUIDString
operationQueue.qualityOfService = qos ?? .Default
operationQueue.maxConcurrentOperationCount = maxConcurrentOperationCount
}

public init(label: String?, qos: DispatchQoS?, attributes: DispatchQueue.Attributes?, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency?, target: DispatchQueue?) {
dispatchQueue = DispatchQueue(label: label ?? UUID().uuidString, qos: qos ?? .default, attributes: attributes ?? .concurrent, autoreleaseFrequency: autoreleaseFrequency ?? .inherit, target: target)
public init(label: String?, qos: dispatch_qos_class_t, attributes: dispatch_queue_attr_t?, target: dispatch_queue_t?) {
let attr = dispatch_queue_attr_make_with_qos_class(attributes, qos, QOS_MIN_RELATIVE_PRIORITY)
dispatchQueue = dispatch_queue_create(NSUUID().UUIDString, attr)
dispatch_set_target_queue(dispatchQueue, target)
priority = .dispatch
}

open func execute(_ operation: Operation) {
public func execute(operation: NSOperation) {
operationQueue.addOperation(operation)
}

open func execute(_ operations: [Operation], waitUntilFinished: Bool = false) {
public func execute(operations: [NSOperation], waitUntilFinished: Bool = false) {
operationQueue.addOperations(operations, waitUntilFinished: waitUntilFinished)
}

open func execute(_ block: @escaping () -> Void) -> Any {
public func execute(block: () -> Void) -> Any {
if priority == .dispatch {
return execute(qos: nil, flags: nil, block: block)
return execute(block as dispatch_block_t)
}
else {
let blockOperation = BlockOperation(block: block)
let blockOperation = NSBlockOperation(block: block)
execute(blockOperation)
return blockOperation
}
}

open func execute(_ blocks: [() -> Void], concurrent: Bool = true, waitUntilFinished: Bool = false) -> [Any] {
public func execute(blocks: [() -> Void], waitUntilFinished: Bool = false) -> [Any] {
var actions = [Any]()
if concurrent {
for block in blocks {
actions.append(execute(block))
}
}
else {
for block in blocks {
let blockOperation = BlockOperation(block: block)
if let lastOperation = actions.last as? Operation {
blockOperation.addDependency(lastOperation)
}
actions.append(blockOperation)
}
if let operations = actions as? [Operation] {
execute(operations, waitUntilFinished: waitUntilFinished)
}
for block in blocks {
actions.append(execute(block))
}
return actions
}

open func execute(qos: DispatchQoS?, flags: DispatchWorkItemFlags?, block: @escaping @convention(block) () -> ()) -> DispatchWorkItem {
let work = DispatchWorkItem(qos: qos ?? .default, flags: flags ?? .assignCurrentContext, block: block)
execute(work)
return work
}

open func execute(_ work: DispatchWorkItem) {
dispatchQueue.async(execute: work)
public func execute(block: dispatch_block_t) -> dispatch_block_t {
dispatch_async(dispatchQueue, block)
return block
}

}
26 changes: 13 additions & 13 deletions Kommander/Kommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

import Foundation

open class Kommand<T> {
public class Kommand<T> {

public typealias ActionBlock = () throws -> T
public typealias SuccessBlock = (_ result: T) -> Void
public typealias ErrorBlock = (_ error: Error) -> Void
public typealias SuccessBlock = (result: T) -> Void
public typealias ErrorBlock = (error: ErrorType) -> Void

private final let deliverer: Dispatcher
private final let executor: Dispatcher
Expand All @@ -21,43 +21,43 @@ open class Kommand<T> {
private(set) internal final var errorBlock: ErrorBlock?
internal final var action: Any?

public init(deliverer: Dispatcher, executor: Dispatcher, actionBlock: @escaping ActionBlock) {
public init(deliverer: Dispatcher, executor: Dispatcher, actionBlock: ActionBlock) {
self.deliverer = deliverer
self.executor = executor
self.actionBlock = actionBlock
}

open func onSuccess(_ onSuccess: @escaping SuccessBlock) -> Self {
public func onSuccess(onSuccess: SuccessBlock) -> Self {
self.successBlock = onSuccess
return self
}

open func onError(_ onError: @escaping ErrorBlock) -> Self {
public func onError(onError: ErrorBlock) -> Self {
self.errorBlock = onError
return self
}

open func execute() {
public func execute() {
action = executor.execute {
do {
let result = try self.actionBlock()
_ = self.deliverer.execute {
self.successBlock?(result)
self.successBlock?(result: result)
}
} catch {
_ = self.deliverer.execute {
self.errorBlock?(error)
self.errorBlock?(error: error)
}
}
}
}

open func cancel() {
if let operation = action as? Operation, operation.isExecuting {
public func cancel() {
if let operation = action as? NSOperation where operation.executing {
operation.cancel()
}
else if let work = action as? DispatchWorkItem, !work.isCancelled {
work.cancel()
else if let block = action as? dispatch_block_t where dispatch_block_testcancel(block) == 0 {
dispatch_block_cancel(block)
}
}

Expand Down
30 changes: 15 additions & 15 deletions Kommander/Kommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

open class Kommander {
public class Kommander {

private final let deliverer: Dispatcher
private final let executor: Dispatcher
Expand All @@ -30,58 +30,58 @@ open class Kommander {
self.executor = executor ?? Dispatcher()
}

public convenience init(name: String?, qos: QualityOfService?, maxConcurrentOperationCount: Int) {
public convenience init(name: String?, qos: NSQualityOfService?, maxConcurrentOperationCount: Int) {
self.init(deliverer: nil, name: name, qos: qos, maxConcurrentOperationCount: maxConcurrentOperationCount)
}

public convenience init(name: String?, qos: DispatchQoS?, attributes: DispatchQueue.Attributes?, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency?, target: DispatchQueue?) {
self.init(deliverer: nil, name: name, qos: qos, attributes: attributes, autoreleaseFrequency: autoreleaseFrequency, target: target)
public convenience init(name: String?, qos: dispatch_qos_class_t, attributes: dispatch_queue_attr_t?, target: dispatch_queue_t?) {
self.init(deliverer: nil, name: name, qos: qos, attributes: attributes, target: target)
}

public init(deliverer: Dispatcher?, name: String?, qos: QualityOfService?, maxConcurrentOperationCount: Int) {
public init(deliverer: Dispatcher?, name: String?, qos: NSQualityOfService?, maxConcurrentOperationCount: Int) {
self.deliverer = deliverer ?? CurrentDispatcher()
executor = Dispatcher(name: name, qos: qos, maxConcurrentOperationCount: maxConcurrentOperationCount)
}

public init(deliverer: Dispatcher?, name: String?, qos: DispatchQoS?, attributes: DispatchQueue.Attributes?, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency?, target: DispatchQueue?) {
public init(deliverer: Dispatcher?, name: String?, qos: dispatch_qos_class_t, attributes: dispatch_queue_attr_t?, target: dispatch_queue_t?) {
self.deliverer = deliverer ?? CurrentDispatcher()
executor = Dispatcher(label: name, qos: qos, attributes: attributes, autoreleaseFrequency: autoreleaseFrequency, target: target)
executor = Dispatcher(label: name, qos: qos, attributes: attributes, target: target)
}

open func makeKommand<T>(_ actionBlock: @escaping () throws -> T) -> Kommand<T> {
public func makeKommand<T>(actionBlock: () throws -> T) -> Kommand<T> {
return Kommand<T>(deliverer: deliverer, executor: executor, actionBlock: actionBlock)
}

open func makeKommands<T>(_ actionBlocks: [() throws -> T]) -> [Kommand<T>] {
public func makeKommands<T>(actionBlocks: [() throws -> T]) -> [Kommand<T>] {
var kommands = [Kommand<T>]()
for actionBlock in actionBlocks {
kommands.append(Kommand<T>(deliverer: deliverer, executor: executor, actionBlock: actionBlock))
}
return kommands
}

open func execute<T>(_ kommands: [Kommand<T>], concurrent: Bool = true, waitUntilFinished: Bool = false) {
public func execute<T>(kommands: [Kommand<T>], waitUntilFinished: Bool = false) {
let blocks = kommands.map { kommand -> () -> Void in
return {
do {
let result = try kommand.actionBlock()
_ = self.deliverer.execute {
kommand.successBlock?(result)
kommand.successBlock?(result: result)
}
} catch {
_ = self.deliverer.execute {
kommand.errorBlock?(error)
kommand.errorBlock?(error: error)
}
}
}
}
let actions = executor.execute(blocks, concurrent: concurrent, waitUntilFinished: waitUntilFinished)
for (index, kommand) in kommands.enumerated() {
let actions = executor.execute(blocks, waitUntilFinished: waitUntilFinished)
for (index, kommand) in kommands.enumerate() {
kommand.action = actions[index]
}
}

open func cancel<T>(_ kommands: [Kommand<T>]) {
public func cancel<T>(kommands: [Kommand<T>]) {
for kommand in kommands {
kommand.cancel()
}
Expand Down
16 changes: 8 additions & 8 deletions Kommander/MainDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@

import Foundation

open class MainDispatcher: Dispatcher {
public class MainDispatcher: Dispatcher {

public init() {
super.init(name: nil, qos: nil, maxConcurrentOperationCount: OperationQueue.defaultMaxConcurrentOperationCount)
operationQueue = OperationQueue.main
dispatchQueue = DispatchQueue.main
super.init(name: nil, qos: nil, maxConcurrentOperationCount: NSOperationQueueDefaultMaxConcurrentOperationCount)
operationQueue = NSOperationQueue.mainQueue()
dispatchQueue = dispatch_get_main_queue()
}

private override convenience init(name: String?, qos: QualityOfService?, maxConcurrentOperationCount: Int) {
private override convenience init(name: String?, qos: NSQualityOfService?, maxConcurrentOperationCount: Int) {
self.init()
assertionFailure("You can't use this initializer for a \(String(describing: type(of: self))).")
assertionFailure("You can't use this initializer for a \(String(self.dynamicType)).")
}

private override convenience init(label: String?, qos: DispatchQoS?, attributes: DispatchQueue.Attributes?, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency?, target: DispatchQueue?) {
private override convenience init(label: String?, qos: dispatch_qos_class_t, attributes: dispatch_queue_attr_t?, target: dispatch_queue_t?) {
self.init()
assertionFailure("You can't use this initializer for a \(String(describing: type(of: self))).")
assertionFailure("You can't use this initializer for a \(String(self.dynamicType)).")
}

}
Loading

0 comments on commit 06869b5

Please sign in to comment.