Skip to content
Permalink
2 contributors

Users who have contributed to this file

@android10 @carbaj03
44 lines (38 sloc) 1.68 KB
/**
* Copyright (C) 2018 Fernando Cejas Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fernandocejas.sample.core.interactor
import com.fernandocejas.sample.core.exception.Failure
import com.fernandocejas.sample.core.functional.Either
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
/**
* Abstract class for a Use Case (Interactor in terms of Clean Architecture).
* This abstraction represents an execution unit for different use cases (this means than any use
* case in the application should implement this contract).
*
* By convention each [UseCase] implementation will execute its job in a background thread
* (kotlin coroutine) and will post the result in the UI thread.
*/
abstract class UseCase<out Type, in Params> where Type : Any {
abstract suspend fun run(params: Params): Either<Failure, Type>
operator fun invoke(params: Params, onResult: (Either<Failure, Type>) -> Unit = {}) {
val job = async(CommonPool) { run(params) }
launch(UI) { onResult(job.await()) }
}
class None
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.