Skip to content

Instantly share code, notes, and snippets.

@kaeawc
Created August 22, 2018 15:42
Show Gist options
  • Save kaeawc/2f64810d387125dd565103f82b7310c8 to your computer and use it in GitHub Desktop.
Save kaeawc/2f64810d387125dd565103f82b7310c8 to your computer and use it in GitHub Desktop.
// Just a small snippet of an SDK
public final class ChatSDK {
private static final Handler sUIThreadHandler
public static void connect(SDKConnectCallback handler) {
// do some weird obfuscated internal SDK processing stuff here
User user = connectAndGetUser();
if (sUIThreadHandler != null) {
sUIThreadHandler.post({
handler.onConnected(user, null);
})
}
}
// Imagine a ConnectHandler interface here that has a onConnected method
}
open class ConnectHandler(open val emitter: MaybeEmitter<User>) : ChatSDK.ConnectHandler {
override fun onConnected(user: User?, error: Exception?) {
when {
error != null -> onError(error)
user == null -> onMissingUser()
else -> onSuccess(user)
}
}
fun onSuccess(user: User) {
if (!emitter.isDisposed) emitter.onSuccess(user)
}
fun onError(error: Exception) {
if (!emitter.isDisposed) emitter.onError(error)
}
fun onMissingUser() {
if (!emitter.isDisposed) emitter.onError(NullPointerException("Missing user"))
}
}
class SDKWrapper {
fun connect(observer: MaybeObserver<User>) {
Maybe<User>.create { ChatSDK.connect(ConnectHandler(it)) }
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(observer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment