Created
June 26, 2015 21:32
-
-
Save hartbit/e60f37aa848d784ae0e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import Foundation | |
| protocol ObserverType { | |
| typealias Element | |
| func onNext(value: Self.Element) | |
| func onError() | |
| func onCompleted() | |
| } | |
| protocol ObservableType { | |
| typealias Element | |
| func subscribe<T : ObserverType where T.Element == Element>(observer: T) | |
| } | |
| class PrintingObserver<T> : ObserverType { | |
| typealias Element = T | |
| func onNext(value: T) { | |
| print("onNext \(value)") | |
| } | |
| func onError() { | |
| print("onError") | |
| } | |
| func onCompleted() { | |
| print("onCompleted") | |
| } | |
| } | |
| class NumbersObservable : ObservableType { | |
| typealias Element = Int | |
| func subscribe<T : ObserverType where T.Element == Int>(observer: T) { | |
| observer.onNext(1) | |
| observer.onNext(2) | |
| observer.onNext(3) | |
| observer.onCompleted() | |
| } | |
| } | |
| let numbers = NumbersObservable() | |
| let observer = PrintingObserver<Int>() | |
| numbers.subscribe(observer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment