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

Commit

Permalink
Release 0.9.1 Removed DispatchWorkItem, now internally working with O…
Browse files Browse the repository at this point in the history
…peration.
  • Loading branch information
alexruperez committed Oct 26, 2017
1 parent 3d573eb commit 86d1aac
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Release 0.9.1

- [x] Removed DispatchWorkItem, now internally working with Operation.

# Release 0.9.0

- [x] Removed deprecated DispatchQueue methods and initializers.
Expand Down
2 changes: 1 addition & 1 deletion Kommander.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Kommander'
s.version = '0.9.0'
s.version = '0.9.1'
s.summary = 'A command pattern implementation written in Swift 4'

s.homepage = 'https://github.com/intelygenz/Kommander-iOS'
Expand Down
4 changes: 2 additions & 2 deletions Kommander.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = 3VW789WSMP;
DYLIB_COMPATIBILITY_VERSION = 0.9.0;
DYLIB_CURRENT_VERSION = 0.9.0;
DYLIB_CURRENT_VERSION = 0.9.1;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
Expand Down Expand Up @@ -1052,7 +1052,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = 3VW789WSMP;
DYLIB_COMPATIBILITY_VERSION = 0.9.0;
DYLIB_CURRENT_VERSION = 0.9.0;
DYLIB_CURRENT_VERSION = 0.9.1;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
Expand Down
46 changes: 17 additions & 29 deletions KommanderTests/DispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ class DispatcherTests: XCTestCase {
}

func testDefaultDispatcherOperationQueue() {
if let operation = dispatcher.execute({ sleep(2) }) as? Operation {
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
} else {
XCTFail("Default dispatcher isn't using OperationQueue.")
}
let operation = dispatcher.execute({ sleep(2) })
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
}

func testDefaultDispatcherDispatchQueue() {
Expand All @@ -55,25 +52,19 @@ class DispatcherTests: XCTestCase {
XCTAssertEqual(dispatcher.operationQueue.name, randomName)
XCTAssertEqual(dispatcher.operationQueue.maxConcurrentOperationCount, 1)
XCTAssertEqual(dispatcher.operationQueue.qualityOfService, .background)
if let operation = dispatcher.execute({ sleep(2) }) as? Operation {
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
} else {
XCTFail("Custom dispatcher isn't using OperationQueue.")
}
let operation = dispatcher.execute({ sleep(2) })
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
}

func testMainDispatcherOperationQueue() {
dispatcher = .main
if let operation = dispatcher.execute({ sleep(2) }) as? Operation {
XCTAssertEqual(dispatcher.operationQueue, OperationQueue.main)
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
} else {
XCTFail("Main dispatcher isn't using OperationQueue.")
}
let operation = dispatcher.execute({ sleep(2) })
XCTAssertEqual(dispatcher.operationQueue, OperationQueue.main)
XCTAssertGreaterThan(dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
}

func testMainDispatcherDispatchQueue() {
Expand All @@ -90,13 +81,10 @@ class DispatcherTests: XCTestCase {
let operationQueue = OperationQueue()
operationQueue.addOperation {
self.dispatcher = .current
if let operation = self.dispatcher.execute({ sleep(2) }) as? Operation {
XCTAssertGreaterThan(self.dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
} else {
XCTFail("Current dispatcher isn't using OperationQueue.")
}
let operation = self.dispatcher.execute({ sleep(2) })
XCTAssertGreaterThan(self.dispatcher.operationQueue.operationCount, 0)
operation.cancel()
XCTAssertTrue(operation.isCancelled)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Major/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ViewController: UIViewController {
}

@IBAction func concurrentAction(_ sender: UIButton) {
kommander.execute(kommander.makeKommands([{ () -> Any? in
kommander.execute(kommander.makeKommands([ { () -> Any? in
sleep(self.sleepTime)
print("Concurrent first: " + String(describing: Date().timeIntervalSince1970))
return nil
Expand All @@ -36,11 +36,11 @@ class ViewController: UIViewController {
sleep(self.sleepTime)
print("Concurrent third: " + String(describing: Date().timeIntervalSince1970))
return nil
}]), concurrent: true)
} ]), concurrent: true)
}

@IBAction func sequentialAction(_ sender: UIButton) {
kommander.execute(kommander.makeKommands([{ () -> Any? in
kommander.execute(kommander.makeKommands([ { () -> Any? in
sleep(self.sleepTime)
print("Sequential first: " + String(describing: Date().timeIntervalSince1970))
return nil
Expand All @@ -52,7 +52,7 @@ class ViewController: UIViewController {
sleep(self.sleepTime)
print("Sequential third: " + String(describing: Date().timeIntervalSince1970))
return nil
}]), concurrent: false)
} ]), concurrent: false)
}

@IBAction func errorAction(_ sender: UIButton) {
Expand Down
10 changes: 7 additions & 3 deletions Source/Dispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ open class Dispatcher {
}

/// Execute block in priority queue
@discardableResult open func execute(_ block: @escaping () -> Void) -> Any {
@discardableResult open func execute(_ block: @escaping () -> Void) -> Operation {
let blockOperation = BlockOperation(block: block)
execute(blockOperation)
return blockOperation
}

/// Execute [block] collection in priority queue (if possible) concurrently or sequentially
@discardableResult open func execute(_ blocks: [() -> Void], concurrent: Bool = true, waitUntilFinished: Bool = false) -> [Any] {
@discardableResult open func execute(_ blocks: [() -> Void], concurrent: Bool = true, waitUntilFinished: Bool = false) -> [Operation] {
var lastOperation: Operation?
let operations = blocks.map { block -> Operation in
let blockOperation = BlockOperation(block: block)
Expand Down Expand Up @@ -93,7 +93,11 @@ open class Dispatcher {
dispatchQueue.async(execute: work)
}

private final func dispatchQoS(_ qos: QualityOfService) -> DispatchQoS {
}

private extension Dispatcher {

final func dispatchQoS(_ qos: QualityOfService) -> DispatchQoS {
switch qos {
case .userInteractive: return .userInteractive
case .userInitiated: return .userInitiated
Expand Down
19 changes: 3 additions & 16 deletions Source/Kommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ open class Kommand<Result> {
/// Error block
private(set) final var errorBlock: ErrorBlock?
/// Operation to cancel
final weak var operation: Operation?
/// Work to cancel
final weak var work: DispatchWorkItem?
internal(set) final weak var operation: Operation?

/// Kommand<Result> instance with your deliverer, your executor and your actionBlock returning generic and throwing errors
/// Kommand<Result> instance with deliverer, executor and actionBlock returning generic and throwing errors
public init(deliverer: Dispatcher = .current, executor: Dispatcher = .default, actionBlock: @escaping ActionBlock) {
self.deliverer = deliverer
self.executor = executor
Expand All @@ -61,7 +59,6 @@ open class Kommand<Result> {
/// Release all resources
deinit {
operation = nil
work = nil
deliverer = nil
executor = nil
actionBlock = nil
Expand Down Expand Up @@ -95,7 +92,7 @@ open class Kommand<Result> {
guard state == .ready else {
return self
}
let action = executor?.execute {
operation = executor?.execute {
do {
if let actionBlock = self.actionBlock {
self.state = .running
Expand All @@ -118,12 +115,6 @@ open class Kommand<Result> {
}
}
}
if let operationAction = action as? Operation {
operation = operationAction
} else if let workAction = action as? DispatchWorkItem {
work = workAction
}

return self
}

Expand All @@ -149,11 +140,7 @@ open class Kommand<Result> {
if let operation = operation, !operation.isFinished {
operation.cancel()
}
else if let work = work, !work.isCancelled {
work.cancel()
}
operation = nil
work = nil
executor = nil
successBlock = nil
actionBlock = nil
Expand Down
6 changes: 1 addition & 5 deletions Source/Kommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ open class Kommander {
}
let actions = executor.execute(blocks, concurrent: concurrent, waitUntilFinished: waitUntilFinished)
for (index, kommand) in kommands.enumerated() {
if let operationAction = actions[index] as? Operation {
kommand.operation = operationAction
} else if let workAction = actions[index] as? DispatchWorkItem {
kommand.work = workAction
}
kommand.operation = actions[index]
}
}

Expand Down

0 comments on commit 86d1aac

Please sign in to comment.