Created
April 5, 2021 10:22
-
-
Save Starmel/d249f04577a72fae3874b08b31a2c39f 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
class AsyncResult<Element>( | |
private val source: (AsyncResultEmitter<Element>) -> Unit | |
) { | |
fun subscribe( | |
onSuccess: (Element) -> Unit, | |
onError: (Throwable) -> Unit | |
): AsyncResultDisposable { | |
val emitter = AsyncResultEmitter(onSuccess, onError) | |
source.invoke(emitter) | |
return object : AsyncResultDisposable { | |
override fun dispose() { | |
emitter.disposable?.invoke() | |
} | |
} | |
} | |
class AsyncResultEmitter<Element>( | |
val onSuccess: (Element) -> Unit, | |
val onError: (Throwable) -> Unit, | |
var disposable: (() -> Unit)? = null | |
) | |
companion object { | |
fun <Element> create(source: (AsyncResultEmitter<Element>) -> Unit): AsyncResult<Element> { | |
return AsyncResult(source) | |
} | |
fun <Element> from(callable: () -> Element): AsyncResult<Element> { | |
return create { it.onSuccess.invoke(callable.invoke()) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment