Skip to content

Commit 81a19ad

Browse files
committed
* Bump Carthage dependencies.
* Reimplement flatMapTaskEvents and ignoreTaskData as protocol extensions. * Update tests to use protocol extension dot-method syntax.
1 parent 91ed1de commit 81a19ad

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

Cartfile.resolved

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
github "Quick/Nimble" "a804949542eb2f299b823ffb36936aec54fe0db6"
22
github "Quick/Quick" "e3c802be6c55710b39dba73caab9f234932c2ed7"
3-
github "antitypical/Result" "0.5"
3+
github "antitypical/Result" "932ac1aabf6d881ead74c4e6911d1271e95d2859"
44
github "jspahrsummers/xcconfigs" "0.8.1"
5-
github "ReactiveCocoa/ReactiveCocoa" "fc54992143255e085371e1bb2e638931e19680e0"
5+
github "ReactiveCocoa/ReactiveCocoa" "f40b21368a19c31afb043cf35baa72f78718f43c"

ReactiveTask/Task.swift

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ private final class Pipe {
101101
class func create(queue: dispatch_queue_t, _ group: dispatch_group_t) -> Result<Pipe, ReactiveTaskError> {
102102
var fildes: [Int32] = [ 0, 0 ]
103103
if pipe(&fildes) == 0 {
104-
return .success(self.init(readFD: fildes[0], writeFD: fildes[1], queue: queue, group: group))
104+
return .Success(self.init(readFD: fildes[0], writeFD: fildes[1], queue: queue, group: group))
105105
} else {
106-
return .failure(.POSIXError(errno))
106+
return .Failure(.POSIXError(errno))
107107
}
108108
}
109109

@@ -264,9 +264,23 @@ private func aggregateDataReadFromPipe(pipe: Pipe) -> SignalProducer<ReadData, R
264264
}
265265
}
266266

267+
public protocol TaskEventType {
268+
/// The type of values being sent on the signal.
269+
typealias T
270+
271+
/// The resulting value, if the event is `Success`.
272+
var value: T? { get }
273+
274+
/// Maps over the value embedded in a `Success` event.
275+
func map<U>(@noescape transform: T -> U) -> TaskEvent<U>
276+
277+
/// Convenience operator for mapping TaskEvents to SignalProducers.
278+
func producerMap<U, Error>(@noescape transform: T -> SignalProducer<U, Error>) -> SignalProducer<TaskEvent<U>, Error>
279+
}
280+
267281
/// Represents events that can occur during the execution of a task that is
268282
/// expected to terminate with a result of type T (upon success).
269-
public enum TaskEvent<T> {
283+
public enum TaskEvent<T>: TaskEventType {
270284
/// Some data arrived from the task on `stdout`.
271285
case StandardOutput(NSData)
272286

@@ -352,23 +366,33 @@ extension TaskEvent: CustomStringConvertible {
352366
}
353367
}
354368

355-
/// Maps the values inside a stream of TaskEvents into new SignalProducers.
356-
public func flatMapTaskEvents<T, U, Error>(strategy: FlattenStrategy, transform: T -> SignalProducer<U, Error>) -> SignalProducer<TaskEvent<T>, Error> -> SignalProducer<TaskEvent<U>, Error> {
357-
return { producer in
358-
return producer.flatMap(strategy) { taskEvent in
369+
extension SignalProducer where T: TaskEventType {
370+
371+
/// Maps the values inside a stream of TaskEvents into new SignalProducers.
372+
public func flatMapTaskEvents<U>(strategy: FlattenStrategy, transform: T.T -> SignalProducer<U, E>) -> SignalProducer<TaskEvent<U>, E> {
373+
return self.flatMap(strategy) { taskEvent in
359374
return taskEvent.producerMap(transform)
360375
}
361376
}
377+
378+
public func ignoreTaskData() -> SignalProducer<T.T, E> {
379+
return lift { $0.ignoreTaskData() }
380+
}
381+
382+
362383
}
363384

364-
/// Ignores incremental standard output and standard error data from the given
365-
/// task, sending only a single value with the final, aggregated result.
366-
public func ignoreTaskData<T, Error>(signal: Signal<TaskEvent<T>, Error>) -> Signal<T, Error> {
367-
return signal
368-
.map { event in
369-
return event.value.map { $0 }
370-
}
371-
.ignoreNil()
385+
extension Signal where T: TaskEventType {
386+
/// Ignores incremental standard output and standard error data from the given
387+
/// task, sending only a single value with the final, aggregated result.
388+
public func ignoreTaskData() -> Signal<T.T, E> {
389+
return self
390+
.map { event in
391+
return event.value
392+
}
393+
.ignoreNil()
394+
}
395+
372396
}
373397

374398
/// Launches a new shell task, using the parameters from `taskDescription`.

ReactiveTaskTests/TaskSpec.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TaskSpec: QuickSpec {
1717
override func spec() {
1818
it("should launch a task that writes to stdout") {
1919
let result = launchTask(TaskDescription(launchPath: "/bin/echo", arguments: [ "foobar" ]))
20-
|> reduce(NSMutableData()) { aggregated, event in
20+
.reduce(NSMutableData()) { aggregated, event in
2121
switch event {
2222
case let .StandardOutput(data):
2323
aggregated.appendData(data)
@@ -28,7 +28,7 @@ class TaskSpec: QuickSpec {
2828

2929
return aggregated
3030
}
31-
|> single
31+
.single()
3232

3333
expect(result).notTo(beNil())
3434
if let data = result?.value {
@@ -38,7 +38,7 @@ class TaskSpec: QuickSpec {
3838

3939
it("should launch a task that writes to stderr") {
4040
let result = launchTask(TaskDescription(launchPath: "/usr/bin/stat", arguments: [ "not-a-real-file" ]))
41-
|> reduce(NSMutableData()) { aggregated, event in
41+
.reduce(NSMutableData()) { aggregated, event in
4242
switch event {
4343
case let .StandardError(data):
4444
aggregated.appendData(data)
@@ -49,7 +49,7 @@ class TaskSpec: QuickSpec {
4949

5050
return aggregated
5151
}
52-
|> single
52+
.single()
5353

5454
expect(result).notTo(beNil())
5555
if let data = result?.value {
@@ -62,9 +62,9 @@ class TaskSpec: QuickSpec {
6262
let data = strings.map { $0.dataUsingEncoding(NSUTF8StringEncoding)! }
6363

6464
let result = launchTask(TaskDescription(launchPath: "/usr/bin/sort", standardInput: SignalProducer(values: data)))
65-
|> map { event in event.value }
66-
|> ignoreNil
67-
|> single
65+
.map { event in event.value }
66+
.ignoreNil()
67+
.single()
6868

6969
expect(result).notTo(beNil())
7070
if let data = result?.value {

0 commit comments

Comments
 (0)