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

Commit

Permalink
Execute multiple Kommands refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexruperez committed Nov 1, 2017
1 parent c742309 commit 64ab7cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
1 change: 0 additions & 1 deletion Major watchOS Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ExtensionDelegate: NSObject, WKExtensionDelegate {
}

func applicationWillResignActive() {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, etc.
}

Expand Down
66 changes: 37 additions & 29 deletions Source/Kommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,12 @@ open class Kommander {

/// Execute [Kommand<Result>] instances collection concurrently or sequentially
open func execute<Result>(_ kommands: [Kommand<Result>], concurrent: Bool = true, waitUntilFinished: Bool = false) {
let blocks = kommands.map { kommand -> () -> Void in
{
guard kommand.state == .ready else {
return
}
do {
if let actionBlock = kommand.actionBlock {
kommand.state = .running
let result = try actionBlock()
guard kommand.state == .running else {
return
}
self.deliverer.execute {
kommand.state = .finished
kommand.successBlock?(result)
}
}
} catch {
guard kommand.state == .running else {
return
}
self.deliverer.execute {
kommand.state = .finished
kommand.errorBlock?(error)
}
}
}
let executionBlocks = kommands.map { kommand in
executionBlock(kommand)
}
let actions = executor.execute(blocks, concurrent: concurrent, waitUntilFinished: waitUntilFinished)
let operations = executor.execute(executionBlocks, concurrent: concurrent, waitUntilFinished: waitUntilFinished)
for (index, kommand) in kommands.enumerated() {
kommand.operation = actions[index]
kommand.operation = operations[index]
}
}

Expand Down Expand Up @@ -129,3 +104,36 @@ open class Kommander {
}

}

private extension Kommander {

final func executionBlock<Result>(_ kommand: Kommand<Result>) -> () -> Void {
return {
guard kommand.state == .ready else {
return
}
do {
if let actionBlock = kommand.actionBlock {
kommand.state = .running
let result = try actionBlock()
guard kommand.state == .running else {
return
}
self.deliverer.execute {
kommand.state = .finished
kommand.successBlock?(result)
}
}
} catch {
guard kommand.state == .running else {
return
}
self.deliverer.execute {
kommand.state = .finished
kommand.errorBlock?(error)
}
}
}
}

}

0 comments on commit 64ab7cd

Please sign in to comment.