Skip to content

Commit

Permalink
Merge pull request #48 from Carthage/code-improvements
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
mdiep committed Dec 18, 2015
2 parents e5bca45 + 25acd20 commit 7d3182a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
19 changes: 8 additions & 11 deletions ReactiveTask/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct Task {
}

/// A GCD group which to wait completion
private static var group = dispatch_group_create()
private static let group = dispatch_group_create()

/// wait for all task termination
public static func waitForAllTaskTermination() {
Expand Down Expand Up @@ -262,13 +262,10 @@ public enum TaskEvent<T>: TaskEventType {

/// The resulting value, if the event is `Success`.
public var value: T? {
switch self {
case .Launch, .StandardOutput, .StandardError:
return nil

case let .Success(value):
if case let .Success(value) = self {
return value
}
return nil
}

/// Maps over the value embedded in a `Success` event.
Expand All @@ -292,16 +289,16 @@ public enum TaskEvent<T>: TaskEventType {
public func producerMap<U, Error>(@noescape transform: T -> SignalProducer<U, Error>) -> SignalProducer<TaskEvent<U>, Error> {
switch self {
case let .Launch(task):
return SignalProducer<TaskEvent<U>, Error>(value: .Launch(task))
return .init(value: .Launch(task))

case let .StandardOutput(data):
return SignalProducer<TaskEvent<U>, Error>(value: .StandardOutput(data))
return .init(value: .StandardOutput(data))

case let .StandardError(data):
return SignalProducer<TaskEvent<U>, Error>(value: .StandardError(data))
return .init(value: .StandardError(data))

case let .Success(value):
return transform(value).map { .Success($0) }
return transform(value).map(TaskEvent<U>.Success)
}
}
}
Expand Down Expand Up @@ -471,7 +468,7 @@ public func launchTask(task: Task, standardInput: SignalProducer<NSData, NoError
// through stdout.
disposable += stderrAggregated
.then(stdoutAggregated)
.map { data in .Success(data) }
.map(TaskEvent.Success)
.start(observer)
} else {
// Wait for stdout to finish, then pass
Expand Down
12 changes: 2 additions & 10 deletions ReactiveTaskTests/TaskSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ class TaskSpec: QuickSpec {
it("should launch a task that writes to stdout") {
let result = launchTask(Task("/bin/echo", arguments: [ "foobar" ]))
.reduce(NSMutableData()) { aggregated, event in
switch event {
case let .StandardOutput(data):
if case let .StandardOutput(data) = event {
aggregated.appendData(data)

default:
break
}

return aggregated
Expand All @@ -56,12 +52,8 @@ class TaskSpec: QuickSpec {
it("should launch a task that writes to stderr") {
let result = launchTask(Task("/usr/bin/stat", arguments: [ "not-a-real-file" ]))
.reduce(NSMutableData()) { aggregated, event in
switch event {
case let .StandardError(data):
if case let .StandardError(data) = event {
aggregated.appendData(data)

default:
break
}

return aggregated
Expand Down

0 comments on commit 7d3182a

Please sign in to comment.