dueTime동안 받은 element의 마지막 값을 방출해준다.
20개의 이벤트가 끊이지 않고 들어왔을 경우
마지막 이벤트를 시점으로 0.5초가 지난 뒤에 19인 이벤트를 방출한다.
extension ObservableType {
/**
Ignores elements from an observable sequence which are followed by another element within a specified relative time duration, using the specified scheduler to run throttling timers.
- seealso: [debounce operator on reactivex.io](http://reactivex.io/documentation/operators/debounce.html)
- parameter dueTime: Throttling duration for each element.
- parameter scheduler: Scheduler to run the throttle timers on.
- returns: The throttled sequence.
*/
public func debounce(_ dueTime: RxSwift.RxTimeInterval, scheduler: RxSwift.SchedulerType) -> RxSwift.Observable<Self.Element>
}
/// debounce
let observable = Observable<Int>
.interval(.milliseconds(200),
scheduler: MainScheduler.instance)
.take(20)
.debounce(.milliseconds(500),
scheduler: MainScheduler.instance)
observable.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// prints 19
latest가 true일 때는 dueTime 동안의 처음 값과 끝 값을, false일 때는 처음 값만 방출한다.
0에 쓰로틀이 걸려 0.5초까지 0, 1, 2 중 0만 방출되고,
3이 방출되는 0.6초에 다시 쓰로틀이 걸려 1.1초까지 방출되는 이벤트가 필터링된다.
extension ObservableType {
/**
Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.
This operator makes sure that no two elements are emitted in less then dueTime.
- seealso: [debounce operator on reactivex.io](http://reactivex.io/documentation/operators/debounce.html)
- parameter dueTime: Throttling duration for each element.
- parameter latest: Should latest element received in a dueTime wide time window since last element emission be emitted.
- parameter scheduler: Scheduler to run the throttle timers on.
- returns: The throttled sequence.
*/
public func throttle(_ dueTime: RxSwift.RxTimeInterval, latest: Bool = true, scheduler: RxSwift.SchedulerType) -> RxSwift.Observable<Self.Element>
}
/// throttle
let observable = Observable<Int>
.interval(.milliseconds(100),
scheduler: MainScheduler.instance)
.take(20)
.throttle(.milliseconds(300),
scheduler: MainScheduler.instance)
observable.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
0
3
6
9
12
15
18
19
*/
extension ObservableType where Self.Element : Equatable {
/**
Returns an observable sequence that contains only distinct contiguous elements according to equality operator.
- seealso: [distinct operator on reactivex.io](http://reactivex.io/documentation/operators/distinct.html)
- returns: An observable sequence only containing the distinct contiguous elements, based on equality operator, from the source sequence.
*/
public func distinctUntilChanged() -> RxSwift.Observable<Self.Element>
}
/// distinctUntilChanged
Observable<Int>
.of(1,1,1,1,2,2,3,3,3,3,4,5,5,5,5,5,5,5,5)
.distinctUntilChanged()
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
1
2
3
4
5
*/
extension ObservableType {
/**
Returns a sequence emitting only element _n_ emitted by an Observable
- seealso: [elementAt operator on reactivex.io](http://reactivex.io/documentation/operators/elementat.html)
- parameter index: The index of the required element (starting from 0).
- returns: An observable sequence that emits the desired element as its own sole emission.
*/
@available(*, deprecated, renamed: "element(at:)")
public func elementAt(_ index: Int) -> RxSwift.Observable<Self.Element>
/**
Returns a sequence emitting only element _n_ emitted by an Observable
- seealso: [elementAt operator on reactivex.io](http://reactivex.io/documentation/operators/elementat.html)
- parameter index: The index of the required element (starting from 0).
- returns: An observable sequence that emits the desired element as its own sole emission.
*/
public func element(at index: Int) -> RxSwift.Observable<Self.Element>
}
/// element
Observable<Int>
.of(0,1,2,3,4,5)
.element(at: 2)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/// prints 2
extension ObservableType {
/**
Filters the elements of an observable sequence based on a predicate.
- seealso: [filter operator on reactivex.io](http://reactivex.io/documentation/operators/filter.html)
- parameter predicate: A function to test each source element for a condition.
- returns: An observable sequence that contains elements from the input sequence that satisfy the condition.
*/
public func filter(_ predicate: @escaping (Self.Element) throws -> Bool) -> RxSwift.Observable<Self.Element>
}
/// filter
Observable<Int>
.of(0,1,2,3,4,5,6,7,8,9,10)
.filter { $0 % 2 == 0 }
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
0
2
4
6
8
10
*/
asSingle은 하나의 element를 받고 success or failure를 return한다.
noElements나 moreThanOneElement를 반환할 수 있다.
First는 처음 값을 Single로 방출한다.
따라서 success(Optional(0))이 방출된다.
asMaybe는 moreThanOneElement를 반환할 수 있다.
아무것도 안들어오면 completed를, 1개가 들어오면 success의 element를 방출한다.
extension ObservableType {
/**
The `asSingle` operator throws a `RxError.noElements` or `RxError.moreThanOneElement`
if the source Observable does not emit exactly one element before successfully completing.
- seealso: [single operator on reactivex.io](http://reactivex.io/documentation/operators/first.html)
- returns: An observable sequence that emits a single element when the source Observable has completed, or throws an exception if more (or none) of them are emitted.
*/
public func asSingle() -> RxSwift.Single<Self.Element>
/**
The `first` operator emits only the very first item emitted by this Observable,
or nil if this Observable completes without emitting anything.
- seealso: [single operator on reactivex.io](http://reactivex.io/documentation/operators/first.html)
- returns: An observable sequence that emits a single element or nil if the source observable sequence completes without emitting any items.
*/
public func first() -> RxSwift.Single<Self.Element?>
/**
The `asMaybe` operator throws a `RxError.moreThanOneElement`
if the source Observable does not emit at most one element before successfully completing.
- seealso: [single operator on reactivex.io](http://reactivex.io/documentation/operators/first.html)
- returns: An observable sequence that emits a single element, completes when the source Observable has completed, or throws an exception if more of them are emitted.
*/
public func asMaybe() -> RxSwift.Maybe<Self.Element>
}
/// asSingle
Observable<Int>
.of(0,1,2,3,4,5,6,7,8,9,10)
.asSingle()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// failure(Sequence contains more than one element.)
/// asSingle
Observable<Int>
.of(0)
.asSingle()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// success(0)
/// first
Observable<Int>
.of(0,1,2,3,4,5,6,7,8,9,10)
.first()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// success(Optional(0))
/// asMaybe
Observable<Int>
.of(0,1,2)
.asMaybe()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// error(Sequence contains more than one element.)
/// asMaybe
Observable<Int>
.of(0)
.asMaybe()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// success(0)
/// asMaybe
Observable<Int>
.of()
.asMaybe()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// completed
IgnoreElements는 아무 element로 방출하지 않고 complete 시킨다.
extension ObservableType {
/**
Skips elements and completes (or errors) when the observable sequence completes (or errors). Equivalent to filter that always returns false.
- seealso: [ignoreElements operator on reactivex.io](http://reactivex.io/documentation/operators/ignoreelements.html)
- returns: An observable sequence that skips all elements of the source sequence.
*/
public func ignoreElements() -> RxSwift.Observable<Never>
}
/// ignoreElements
Observable<Int>
.of(1,2,3,4,5)
.ignoreElements()
.subscribe({
print($0)
})
.disposed(by: disposeBag)
// completed
extension ObservableType {
/**
Samples the source observable sequence using a sampler observable sequence producing sampling ticks.
Upon each sampling tick, the latest element (if any) in the source sequence during the last sampling interval is sent to the resulting sequence.
**In case there were no new elements between sampler ticks, you may provide a default value to be emitted, instead
to the resulting sequence otherwise no element is sent.**
- seealso: [sample operator on reactivex.io](http://reactivex.io/documentation/operators/sample.html)
- parameter sampler: Sampling tick sequence.
- parameter defaultValue: a value to return if there are no new elements between sampler ticks
- returns: Sampled observable sequence.
*/
public func sample<Source: ObservableType>(_ sampler: Source, defaultValue: Element? = nil)
-> Observable<Element> {
return Sample(source: self.asObservable(), sampler: sampler.asObservable(), defaultValue: defaultValue)
}
}
extension ObservableType {
/**
Bypasses a specified number of elements in an observable sequence and then returns the remaining elements.
- seealso: [skip operator on reactivex.io](http://reactivex.io/documentation/operators/skip.html)
- parameter count: The number of elements to skip before returning the remaining elements.
- returns: An observable sequence that contains the elements that occur after the specified index in the input sequence.
*/
public func skip(_ count: Int) -> RxSwift.Observable<Self.Element>
}
/// skip
Observable<Int>
.of(1,2,3,4,5,6,7,8,9,10)
.skip(3)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
4
5
6
7
8
9
10
*/
extension ObservableType {
/**
Bypasses elements in an observable sequence as long as a specified condition is true and then returns the remaining elements.
- seealso: [skipWhile operator on reactivex.io](http://reactivex.io/documentation/operators/skipwhile.html)
- parameter predicate: A function to test each element for a condition.
- returns: An observable sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.
*/
public func skip(while predicate: @escaping (Self.Element) throws -> Bool) -> RxSwift.Observable<Self.Element>
}
/// skip
Observable<Int>
.of(1,2,3,4,5,6,7,8,9,10)
.skip(while: {
$0 < 5
})
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
5
6
7
8
9
10
*/
extension ObservableType {
/**
Returns the elements from the source observable sequence that are emitted after the other observable sequence produces an element.
- seealso: [skipUntil operator on reactivex.io](http://reactivex.io/documentation/operators/skipuntil.html)
- parameter other: Observable sequence that starts propagation of elements of the source sequence.
- returns: An observable sequence containing the elements of the source sequence that are emitted after the other sequence emits an item.
*/
public func skip<Source>(until other: Source) -> RxSwift.Observable<Self.Element> where Source : RxSwift.ObservableType
}
extension ObservableType {
/**
Returns a specified number of contiguous elements from the start of an observable sequence.
- seealso: [take operator on reactivex.io](http://reactivex.io/documentation/operators/take.html)
- parameter count: The number of elements to return.
- returns: An observable sequence that contains the specified number of elements from the start of the input sequence.
*/
public func take(_ count: Int) -> RxSwift.Observable<Self.Element>
}
/// take
Observable<Int>
.of(1,2,3,4,5,6,7,8,9,10)
.take(3)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
1
2
3
*/
/**
Returns elements from an observable sequence as long as a specified condition is true.
- seealso: [takeWhile operator on reactivex.io](http://reactivex.io/documentation/operators/takewhile.html)
- parameter predicate: A function to test each element for a condition.
- returns: An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
*/
public func take(while predicate: @escaping (Self.Element) throws -> Bool, behavior: RxSwift.TakeBehavior = .exclusive) -> RxSwift.Observable<Self.Element>
/// take
Observable<Int>
.of(1,2,3,4,5,6,7,8,9,10)
.take(while: {
$0 < 5
})
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
1
2
3
4
*/
/**
Returns elements from an observable sequence until the specified condition is true.
- seealso: [takeUntil operator on reactivex.io](http://reactivex.io/documentation/operators/takeuntil.html)
- parameter predicate: A function to test each element for a condition.
- parameter behavior: Whether or not to include the last element matching the predicate. Defaults to `exclusive`.
- returns: An observable sequence that contains the elements from the input sequence that occur before the element at which the test passes.
*/
public func take(until predicate: @escaping (Self.Element) throws -> Bool, behavior: RxSwift.TakeBehavior = .exclusive) -> RxSwift.Observable<Self.Element>