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
scopedExample("Subscription") {
// Signal.pipe is a way to manually control a signal. the returned observer can be used to send values to the signal
let (signal, observer) = Signal<int, nonerror>.pipe()
let subscriber1 = Observer<int, nonerror>(next: { print("Subscriber 1 received \($0)") })
let subscriber2 = Observer<int, nonerror>(next: { print("Subscriber 2 received \($0)") })
print("Subscriber 1 subscribes to the signal")
print("\(observer)")
signal.observe(subscriber1)
print("Send value `10` on the signal")
// subscriber1 will receive the value
observer.sendNext(10)
print("Subscriber 2 subscribes to the signal")
// Notice how nothing happens at this moment, i.e. subscriber2 does not receive the previously sent value
signal.observe(subscriber2)
print("Send value `20` on the signal")
// Notice that now, subscriber1 and subscriber2 will receive the value
observer.sendNext(20)
}
--- Subscription ---
Subscriber 1 subscribes to the signal
Observer<int, nonerror>(action: (Function))
Send value `10` on the signal
Subscriber 1 received 10
Subscriber 2 subscribes to the signal
Send value `20` on the signal
Subscriber 1 received 20
Subscriber 2 received 20</int, nonerror></int, nonerror></int, nonerror></int, nonerror>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
scopedExample("`uniqueValues`") {
let (signal, observer) = Signal<int, noerror>.pipe()
let subscriber = Observer<int, noerror>(next: { print("Subscriber received \($0)") } )
let uniqueSignal = signal.uniqueValues()
uniqueSignal.observe(subscriber)
observer.sendNext(1)
observer.sendNext(2)
observer.sendNext(3)
observer.sendNext(4)
observer.sendNext(3)
observer.sendNext(3)
observer.sendNext(5)
}
--- `uniqueValues` ---
Subscriber received 1
Subscriber received 2
Subscriber received 3
Subscriber received 4
Subscriber received 5</int, noerror></int, noerror>
map
把每一个发送的值转换成新的值
1234567891011
scopedExample("`map`") {
let (signal, observer) = Signal<int, noerror>.pipe()
let subscriber = Observer<int, noerror>(next: { print("Subscriber received \($0)") } )
let mappedSignal = signal.map { $0 * 2 }
mappedSignal.observe(subscriber)
print("Send value `10` on the signal")
observer.sendNext(10)
}
--- `map` ---
Send value `10` on the signal
Subscriber received 20</int, noerror></int, noerror>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
scopedExample("`mapError`") {
let userInfo = [NSLocalizedDescriptionKey: "??"]
let code = error.code + 10000
let mappedError = NSError(domain: "com.reactivecocoa.errordomain", code: code, userInfo: userInfo)
let (signal, observer) = Signal<int, nserror>.pipe()
let subscriber = Observer<int, nserror>(failed: { print("Subscriber received error: \($0)") } )
let mappedErrorSignal = signal.mapError { (error:NSError) -> NSError in
return mappedError
}
mappedErrorSignal.observe(subscriber)
print("Send error `NSError(domain: \"com.reactivecocoa.errordomain\", code: 4815, userInfo: nil)` on the signal")
observer.sendFailed(NSError(domain: "com.reactivecocoa.errordomain", code: 4815, userInfo: nil))
}
--- `mapError` ---
Send error `NSError(domain: "com.reactivecocoa.errordomain", code: 4815, userInfo: nil)` on the signal
Subscriber received error: Error Domain=com.reactivecocoa.errordomain Code=14815 "??" UserInfo={NSLocalizedDescription=??}</int, nserror></int, nserror>
filter
用于过滤一些值
123456789101112131415
scopedExample("`filter`") {
let (signal, observer) = Signal<int, noerror>.pipe()
let subscriber = Observer<int, noerror>(next: { print("Subscriber received \($0)") } )
// subscriber will only receive events with values greater than 12
let filteredSignal = signal.filter { $0 > 12 ? true : false }
filteredSignal.observe(subscriber)
observer.sendNext(10)
observer.sendNext(11)
observer.sendNext(12)
observer.sendNext(13)
observer.sendNext(14)
}
--- `filter` ---
Subscriber received 13
Subscriber received 14</int, noerror></int, noerror>
ignoreNil
在发送的值为可选类型中:如果有值,把值解包,如果是nil丢弃掉
1234567891011121314
scopedExample("`ignoreNil`") {
let (signal, observer) = Signal<int?, noerror>.pipe()
// note that the signal is of type `Int?` and observer is of type `Int`, given we're unwrapping
// non-`nil` values
let subscriber = Observer<int, noerror>(next: { print("Subscriber received \($0)") } )
let ignoreNilSignal = signal.ignoreNil()
ignoreNilSignal.observe(subscriber)
observer.sendNext(1)
observer.sendNext(nil)
observer.sendNext(3)
}
--- `ignoreNil` ---
Subscriber received 1
Subscriber received 3</int, noerror></int?, noerror>
take
take(num)只取前num此值得信号
12345678910111213
scopedExample("`take`") {
let (signal, observer) = Signal<int, noerror>.pipe()
let subscriber = Observer<int, noerror>(next: { print("Subscriber received \($0)") } )
let takeSignal = signal.take(2)
takeSignal.observe(subscriber)
observer.sendNext(1)
observer.sendNext(2)
observer.sendNext(3)
observer.sendNext(4)
}
--- `take` ---
Subscriber received 1
Subscriber received 2</int, noerror></int, noerror>
collect
在发送complete事件之后,观察者会收到一个由之前事件组成的数组
注意:如果在发送complete事件的时候,没有任何事件发送,观察者会收到一个空的数据
12345678910111213141516
scopedExample("`collect`") {
let (signal, observer) = Signal<int, noerror>.pipe()
// note that the signal is of type `Int` and observer is of type `[Int]` given we're "collecting"
// `Int` values for the lifetime of the signal
let subscriber = Observer<[Int], NoError>(next: { print("Subscriber received \($0)") } )
let collectSignal = signal.collect()
collectSignal.observe(subscriber)
observer.sendNext(1)
observer.sendNext(2)
observer.sendNext(3)
observer.sendNext(4)
observer.sendCompleted()
}
--- `collect` ---
Subscriber received [1, 2, 3, 4]</int, noerror>
scopedExample("Subscription") {
let producer = SignalProducer<int, noerror> { observer, _ in
print("New subscription, starting operation")
observer.sendNext(1)
observer.sendNext(2)
}
let subscriber1 = Observer<int, noerror>(next: { print("Subscriber 1 received \($0)") })
let subscriber2 = Observer<int, noerror>(next: { print("Subscriber 2 received \($0)") })
print("Subscriber 1 subscribes to producer")
producer.start(subscriber1)
print("Subscriber 2 subscribes to producer")
// Notice, how the producer will start the work again
producer.start(subscriber2)
}
--- Subscription ---
Subscriber 1 subscribes to producer
New subscription, starting operation
Subscriber 1 received 1
Subscriber 1 received 2
Subscriber 2 subscribes to producer
New subscription, starting operation
Subscriber 2 received 1
Subscriber 2 received 2</int, noerror></int, noerror></int, noerror>
/*:
### `empty`
A producer for a Signal that will immediately complete without sending
any values.
*/
scopedExample("`empty`") {
let emptyProducer = SignalProducer<int, noerror>.empty
let observer = Observer<int, noerror>(
failed: { _ in print("error not called") },
completed: { print("completed called") },
interrupted: { print("interrupted called") },
next: { _ in print("next not called") }
)
emptyProducer.start(observer)
}
--- `empty` ---
completed called</int, noerror></int, noerror>
/*:
### `never`
A producer for a Signal that never sends any events to its observers.
*/
scopedExample("`never`") {
let neverProducer = SignalProducer<int, noerror>.never
let observer = Observer<int, noerror>(
failed: { _ in print("error not called") },
completed: { print("completed not called") },
next: { _ in print("next not called") }
)
neverProducer.start(observer)
}
--- `never` ---</int, noerror></int, noerror>
scopedExample("`materialize`") {
SignalProducer<int, noerror>(values: [ 1, 2, 3, 4 ])
.materialize()
.startWithNext { value in
print(value)
}
}
--- `materialize` ---
NEXT 1
NEXT 2
NEXT 3
NEXT 4
COMPLETED
// 注意 value 如果不做materialize就是Int类型
/*:
### `sampleOn`
Forwards the latest value from `self` whenever `sampler` sends a Next
event.
If `sampler` fires before a value has been observed on `self`, nothing
happens.
Returns a producer that will send values from `self`, sampled (possibly
multiple times) by `sampler`, then complete once both input producers have
completed, or interrupt if either input producer is interrupted.
*/
scopedExample("`sampleOn`") {
let baseProducer = SignalProducer<int, noerror>(values: [ 1, 2, 3, 4 ])
let sampledOnProducer = SignalProducer<int, noerror>(values: [ 1, 2 ])
.map { _ in () }
let newProduce = baseProducer
.sampleOn(sampledOnProducer)
newProduce .startWithNext { value in
print(value)
}
}
--- `sampleOn` ---
4
4</int, noerror></int, noerror>
sampler发送的2次值都被变换成baseProduce 的comlete前的最后一个值