There are numerous operators implemented in RxSwift > Marble diagrams for all operators can be found on ReacitveX.io
RxSwift에는 수많은 연산자가 구현되어 있습니다.
모든 연산자의 Marble Diagram 은 Reactive.io 에서 확인할수 있습니다.
Almost all operators are demonstrated in Playgrounds
[https://github.com/ReactiveX/RxSwift/tree/main/Rx.playground]
To use playgorunds please open `Rx.xworkspace` , build `RxSwift-macOS` scheme and then open playgorinds in `Rx.xworkspace` tree view
대부분의 연산자는 Playgrounds 에서 시연됩니다.
Playgrounds를 사용하려면, Rx.xcworkspace를 열고,
RxSwift-macOS 스킴을 빌드한 후, Rx.xcworkspace 트리 뷰에서 Playgrounds를 열면 됩니다.
In case you need an operator, and don't know how to find it there is a decision tree of operators. [https://reactivex.io/documentation/operators.html#tree]
만약 필요한 연산자를 찾는 방법을 모를 경우, 연산자 선택 트리(Decision Tree of Operators)를 참조할 수 있습니다.
Threre are two ways how you can create custom operators.
사용자 정의 연산자를 구현하는 방법은 두가지가 있습니다.
Easy way
all of the inernal code uses highly encouraged to use standard operators, so they aren't the best tutorial material. That's why it's highly encouraged to use standard operators.
Fortunately there is an easier way to create operators.
Creating new operators is actually all about creating observables, and previous chapter already descibes how to do that,
RxSwift의 내부 코드에는 고도로 최적화된 연산자가 사용되며, 이들은 학습자료로 적합하지 않으수도 있습니다.
따라서 표준 연산자를 사용하는것이 권장됩니다.
다행히도 사용자 정의 연산자를 만드는 간단한 방법이 있습니다.
새로운 연산자를 만드는것은 결국 Observable을 생성하는 과정이며, 이전 장에서 리를 다뤘습니다.
Let's see how an unoptimized map operator can be implemented.
이제 최적화 되지않은 map 연산자를 구현하는 방법을 살펴 보겠습니다.
extension ObservableType {
func myMap<R>(transform: @escaping(Element) -> R) -> Observable<R> {
return Observable.create { observer in
let subscription = self.subscribe { e in
switch e {
case .next(let value):
let result = transform(value)
observer.on(.next(result))
case .error(let error):
observer.on(.error(error))
case .completed:
observer.on(.completed)
}
}
return subscription
}
}
}
So now you can use your own map:
let subscription = myInterval(.milliseconds(100))
.myMap { e in
return "This is simply \(e)"
}
.subscribe(onNext: { n in
print(n)
})
This will print:
Subscribed
This is simply 0
This is simply 1
This is simply 2
This is simply 3
This is simply 4
This is simply 5
This is simply 6
This is simply 7
This is simply 8
...