forked from ReactiveCocoa/ReactiveCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectiveCBridging.swift
183 lines (156 loc) · 5.72 KB
/
ObjectiveCBridging.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// ObjectiveCBridging.swift
// ReactiveCocoa
//
// Created by Justin Spahr-Summers on 2014-07-02.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//
import Result
extension RACDisposable: Disposable {}
extension RACScheduler: DateSchedulerType {
public var currentDate: NSDate {
return NSDate()
}
public func schedule(action: () -> ()) -> Disposable? {
return self.schedule(action)
}
public func scheduleAfter(date: NSDate, action: () -> ()) -> Disposable? {
return self.after(date, schedule: action)
}
public func scheduleAfter(date: NSDate, repeatingEvery: NSTimeInterval, withLeeway: NSTimeInterval, action: () -> ()) -> Disposable? {
return self.after(date, repeatingEvery: repeatingEvery, withLeeway: withLeeway, schedule: action)
}
}
extension ImmediateScheduler {
public func toRACScheduler() -> RACScheduler {
return RACScheduler.immediateScheduler()
}
}
extension UIScheduler {
public func toRACScheduler() -> RACScheduler {
return RACScheduler.mainThreadScheduler()
}
}
extension QueueScheduler {
public func toRACScheduler() -> RACScheduler {
return RACTargetQueueScheduler(name: "org.reactivecocoa.ReactiveCocoa.QueueScheduler.toRACScheduler()", targetQueue: queue)
}
}
private func defaultNSError(message: String, #file: String, #line: Int) -> NSError {
return Result<(), NSError>.error(message: message, file: file, line: line)
}
extension RACSignal {
/// Creates a SignalProducer which will subscribe to the receiver once for
/// each invocation of start().
public func toSignalProducer(file: String = __FILE__, line: Int = __LINE__) -> SignalProducer<AnyObject?, NSError> {
return SignalProducer { observer, disposable in
let next = { (obj: AnyObject?) -> () in
sendNext(observer, obj)
}
let error = { (nsError: NSError?) -> () in
sendError(observer, nsError ?? defaultNSError("Nil RACSignal error", file: file, line: line))
}
let completed = {
sendCompleted(observer)
}
disposable += self.subscribeNext(next, error: error, completed: completed)
}
}
}
/// Turns each value into an Optional.
private func optionalize<T, E>(signal: Signal<T, E>) -> Signal<T?, E> {
return signal |> map { Optional($0) }
}
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<T: AnyObject, E>(producer: SignalProducer<T, E>) -> RACSignal {
return toRACSignal(producer |> optionalize)
}
/// Creates a RACSignal that will start() the producer once for each
/// subscription.
///
/// Any `Interrupted` events will be silently discarded.
public func toRACSignal<T: AnyObject, E>(producer: SignalProducer<T?, E>) -> RACSignal {
return RACSignal.createSignal { subscriber in
let selfDisposable = producer.start(next: { value in
subscriber.sendNext(value)
}, error: { error in
subscriber.sendError(error.nsError)
}, completed: {
subscriber.sendCompleted()
})
return RACDisposable {
selfDisposable.dispose()
}
}
}
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<T: AnyObject, E>(signal: Signal<T, E>) -> RACSignal {
return toRACSignal(signal |> optionalize)
}
/// Creates a RACSignal that will observe the given signal.
///
/// Any `Interrupted` event will be silently discarded.
public func toRACSignal<T: AnyObject, E>(signal: Signal<T?, E>) -> RACSignal {
return RACSignal.createSignal { subscriber in
let selfDisposable = signal.observe(next: { value in
subscriber.sendNext(value)
}, error: { error in
subscriber.sendError(error.nsError)
}, completed: {
subscriber.sendCompleted()
})
return RACDisposable {
selfDisposable?.dispose()
}
}
}
extension RACCommand {
/// Creates an Action that will execute the receiver.
///
/// Note that the returned Action will not necessarily be marked as
/// executing when the command is. However, the reverse is always true:
/// the RACCommand will always be marked as executing when the action is.
public func toAction(file: String = __FILE__, line: Int = __LINE__) -> Action<AnyObject?, AnyObject?, NSError> {
let enabledProperty = MutableProperty(true)
enabledProperty <~ self.enabled.toSignalProducer()
|> map { $0 as! Bool }
|> catch { _ in SignalProducer<Bool, NoError>(value: false) }
return Action(enabledIf: enabledProperty) { (input: AnyObject?) -> SignalProducer<AnyObject?, NSError> in
let executionSignal = RACSignal.defer {
return self.execute(input)
}
return executionSignal.toSignalProducer(file: file, line: line)
}
}
}
extension Action {
private var commandEnabled: RACSignal {
let enabled = self.enabled.producer |> map { $0 as NSNumber }
return toRACSignal(enabled)
}
}
/// Creates a RACCommand that will execute the action.
///
/// Note that the returned command will not necessarily be marked as
/// executing when the action is. However, the reverse is always true:
/// the Action will always be marked as executing when the RACCommand is.
public func toRACCommand<Output: AnyObject, E>(action: Action<AnyObject?, Output, E>) -> RACCommand {
return RACCommand(enabled: action.commandEnabled) { (input: AnyObject?) -> RACSignal in
return toRACSignal(action.apply(input))
}
}
/// Creates a RACCommand that will execute the action.
///
/// Note that the returned command will not necessarily be marked as
/// executing when the action is. However, the reverse is always true:
/// the Action will always be marked as executing when the RACCommand is.
public func toRACCommand<Output: AnyObject, E>(action: Action<AnyObject?, Output?, E>) -> RACCommand {
return RACCommand(enabled: action.commandEnabled) { (input: AnyObject?) -> RACSignal in
return toRACSignal(action.apply(input))
}
}