Created
July 30, 2018 17:37
-
-
Save JorgeCastilloPrz/0d985603eb402ff854b0a23c32157495 to your computer and use it in GitHub Desktop.
PolymorphicDataSources.kt
This file contains 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) } | |
) | |
} | |
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 { cb -> | |
Option.fromNullable(internetStorage[user]).fold( | |
{ cb(UserNotInRemoteStorage(user).left()) }, | |
{ cb(it.right()) } | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment