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
@JvmStatic | |
fun main(args: Array<String>): Unit { | |
val user1 = User(UserId("user1")) | |
val user2 = User(UserId("user2")) | |
val user3 = User(UserId("unknown user")) | |
val maybeModule = Module(MaybeK.async()) | |
maybeModule.run { | |
repository.allTasksByUser(user1).fix().maybe.subscribe({ println(it) }, { println(it) }) | |
repository.allTasksByUser(user2).fix().maybe.subscribe({ println(it) }, { println(it) }) |
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 Module<F>(A: Async<F>) { | |
private val localDataSource: LocalDataSource<F> = LocalDataSource(A) | |
private val remoteDataSource: RemoteDataSource<F> = RemoteDataSource(A) | |
val repository: TaskRepository<F> = | |
TaskRepository(localDataSource, remoteDataSource, A) | |
} |
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
object test { | |
@JvmStatic | |
fun main(args: Array<String>): Unit { | |
val user1 = User(UserId("user1")) | |
val user2 = User(UserId("user2")) | |
val user3 = User(UserId("unknown user")) | |
val singleModule = Module(SingleK.async()) |
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
fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
localDS.allTasksByUser(user).handleErrorWith { | |
when (it) { | |
is UserNotInLocalStorage -> remoteDS.allTasksByUser(user) | |
else -> raiseError(UnknownError(it)) | |
} | |
} |
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 TaskRepository<F>( | |
private val localDS: DataSource<F>, | |
private val remoteDS: RemoteDataSource<F>, | |
AE: ApplicativeError<F, Throwable>) : ApplicativeError<F, Throwable> by AE { | |
fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
localDS.allTasksByUser(user).handleErrorWith { | |
when (it) { | |
is UserNotInLocalStorage -> remoteDS.allTasksByUser(user) | |
else -> raiseError(UnknownError(it)) |
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
override fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
async { callback: (Either<Throwable, List<Task>>) -> Unit -> | |
Option.fromNullable(internetStorage[user]).fold( | |
{ callback(UserNotInRemoteStorage(user).left()) }, | |
{ callback(it.right()) } | |
) | |
} |
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 RemoteDataSource<F>(A: Async<F>) : DataSource<F>, Async<F> by A { | |
private val internetStorage: Map<User, List<Task>> = | |
mapOf(User(UserId("user2")) to listOf(Task("Remote Task assigned to user2"))) | |
override fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
async { callback: (Either<Throwable, List<Task>>) -> Unit -> | |
Option.fromNullable(internetStorage[user]).fold( | |
{ callback(UserNotInRemoteStorage(user).left()) }, | |
{ callback(it.right()) } | |
) |
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 LocalDataSource<F>(A: ApplicativeError<F, Throwable>) : | |
DataSource<F>, ApplicativeError<F, Throwable> by A { |
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
override fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
Option.fromNullable(localCache[user]).fold( | |
{ raiseError(UserNotInLocalStorage(user)) }, | |
{ just(it) } | |
) |
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 LocalDataSource<F>(A: ApplicativeError<F, Throwable>) : | |
DataSource<F>, ApplicativeError<F, Throwable> by A { | |
private val localCache: Map<User, List<Task>> = | |
mapOf(User(UserId("user1")) to listOf(Task("LocalTask assigned to user1"))) | |
override fun allTasksByUser(user: User): Kind<F, List<Task>> = | |
Option.fromNullable(localCache[user]).fold( | |
{ raiseError(UserNotInLocalStorage(user)) }, | |
{ just(it) } |