Created
April 16, 2021 22:32
-
-
Save Atternatt/2fa6fdd09a8681386496be52acf9ead1 to your computer and use it in GitHub Desktop.
Combination of async coroutine and Arrows Either object
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
override suspend fun execute(query: PostsQuery): Either<Failure, List<Post>> { | |
val operation = if (query.forceRefresh) MainSyncOperation else CacheSyncOperation | |
return either { | |
val posts = !repository.getAll(query = query, operation = operation) | |
posts | |
.filter { it.featuredImage != null } //we just want posts with featured images | |
.map { post -> | |
withContext(coroutineDispatcher) { | |
async { | |
val host: String? = try { | |
URI.create(post.authorUrl).host | |
} catch (e: IllegalArgumentException) { | |
null | |
} | |
if (host.isNullOrBlank()) { | |
post | |
} else { | |
post.copy( | |
numberOfSubscribers = !getsubsCountUseCase( | |
SubscribersQuery(host) | |
).handleError { 0L }) | |
} | |
} | |
} | |
}.awaitAll() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment