Skip to content
This repository has been archived by the owner on May 8, 2019. It is now read-only.

Commit

Permalink
Bugs fixed in autocancelable tasks in monix
Browse files Browse the repository at this point in the history
  • Loading branch information
Javi Pacheco committed Nov 24, 2016
1 parent 7f26d50 commit 1cab792
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MomentBroadcastReceiver
def verifyConnectionStatus(maybeNetworkInfo: Option[android.net.NetworkInfo]): Unit =
maybeNetworkInfo foreach { networkInfo =>
if (networkInfo.getType == ConnectivityManager.TYPE_WIFI) {
connectionStatusTaskRef := connectionStatusChangedJobs.connectionStatusChanged().resolveAsyncDelayed(5.seconds)
connectionStatusTaskRef := connectionStatusChangedJobs.connectionStatusChanged().resolveAutoCancelableAsyncDelayed(5.seconds)
}
}

Expand All @@ -43,7 +43,7 @@ class MomentBroadcastReceiver
}

maybeService foreach { service =>
fenceStatusRef := service.resolveAsyncDelayed(500.millis)
fenceStatusRef := service.resolveAutoCancelableAsyncDelayed(500.millis)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ trait SharedCollectionItem

background.getPaint.setColor(theme.getRandomIndexColor)
val apps = collection.resolvedPackages
android.util.Log.d("9cards", s"${collection.name} -- apps: ${apps.map(_.title).mkString(",")}")
(icon <~ ivSrc(collection.getIconCollectionDetail)) ~
(appsIcons <~
vgRemoveAllViews <~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ object TaskServiceOps {
}
}

def resolveAutoCancelableAsyncDelayed[E >: Throwable](
finiteDuration: FiniteDuration,
onResult: A => Unit = a => (),
onException: E => Unit = (e: Throwable) => ()
): Cancelable = {
Task.defer(t.value).executeWithOptions(_.enableAutoCancelableRunLoops).delayExecution(finiteDuration).runAsync { result =>
result match {
case Failure(ex) =>
printErrorTaskMessage("=> EXCEPTION Disjunction <=", ex)
onException(ex)
case Success(Right(value)) => onResult(value)
case Success(Left(ex)) =>
printErrorTaskMessage(s"=> EXCEPTION Left) <=", ex)
onException(ex)
}
}
}

def resolveAsyncService[E >: Throwable](
onResult: (A) => TaskService[A] = a => TaskService(Task(Either.right(a))),
onException: (E) => TaskService[A] = (e: NineCardException) => TaskService(Task(Either.left(e)))): Cancelable = {
Expand All @@ -70,6 +88,22 @@ object TaskServiceOps {
}
}

def resolveAutoCancelableAsyncService[E >: Throwable](
onResult: (A) => TaskService[A] = a => TaskService(Task(Either.right(a))),
onException: (E) => TaskService[A] = (e: NineCardException) => TaskService(Task(Either.left(e)))): Cancelable = {
Task.defer(t.value).executeWithOptions(_.enableAutoCancelableRunLoops).runAsync { result =>
result match {
case Failure(ex) =>
printErrorTaskMessage("=> EXCEPTION Disjunction <=", ex)
onException(ex).value.runAsync
case Success(Right(response)) => onResult(response).value.coeval
case Success(Left(ex)) =>
printErrorTaskMessage(s"=> EXCEPTION Left) <=", ex)
onException(ex).value.runAsync
}
}
}

def resolve[E >: Throwable](
onResult: A => Unit = a => (),
onException: E => Unit = (e: Throwable) => ()): Unit = {
Expand Down Expand Up @@ -97,6 +131,9 @@ object TaskServiceOps {
def resolveAsyncServiceOr[E >: Throwable](exception: (E) => TaskService[A]): Cancelable =
resolveAsyncService(onException = exception)

def resolveAutoCancelableAsyncServiceOr[E >: Throwable](exception: (E) => TaskService[A]): Cancelable =
resolveAutoCancelableAsyncService(onException = exception)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class PublicCollectionsFragment(implicit launcherJobs: LauncherJobs)
}

override def loadPublicCollectionsByTypeSharedCollection(typeSharedCollection: TypeSharedCollection): Unit =
serialCancelableTaskRef := collectionJobs.loadPublicCollectionsByTypeSharedCollection(typeSharedCollection).resolveAsyncServiceOr(onError)
serialCancelableTaskRef := collectionJobs.loadPublicCollectionsByTypeSharedCollection(typeSharedCollection).resolveAutoCancelableAsyncServiceOr(onError)

override def loadPublicCollectionsByCategory(category: NineCardsCategory): Unit =
serialCancelableTaskRef := collectionJobs.loadPublicCollectionsByCategory(category).resolveAsyncServiceOr(onError)
serialCancelableTaskRef := collectionJobs.loadPublicCollectionsByCategory(category).resolveAutoCancelableAsyncServiceOr(onError)

override def loadPublicCollections(): Unit =
serialCancelableTaskRef := collectionJobs.loadPublicCollections().resolveAsyncServiceOr(onError)
serialCancelableTaskRef := collectionJobs.loadPublicCollections().resolveAutoCancelableAsyncServiceOr(onError)

override def onAddCollection(sharedCollection: SharedCollection): Unit =
(for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ class ProfileActivity
tab match {
case AccountsTab =>
serialCancelableTaskRef := withError(jobs.loadUserAccounts())
.resolveAsyncServiceOr(_ => jobs.profileUiActions.showEmptyAccountsContent(error = true))
.resolveAutoCancelableAsyncServiceOr(_ => jobs.profileUiActions.showEmptyAccountsContent(error = true))
case PublicationsTab =>
serialCancelableTaskRef := withError(jobs.loadPublications())
.resolveAsyncServiceOr(onException(jobs.profileUiActions.showEmptyPublicationsContent(error = true)))
.resolveAutoCancelableAsyncServiceOr(onException(jobs.profileUiActions.showEmptyPublicationsContent(error = true)))
case SubscriptionsTab =>
serialCancelableTaskRef := withError(jobs.loadSubscriptions())
.resolveAsyncServiceOr(onException(jobs.profileUiActions.showEmptySubscriptionsContent(error = true)))
.resolveAutoCancelableAsyncServiceOr(onException(jobs.profileUiActions.showEmptySubscriptionsContent(error = true)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package cards.nine.commons

import cats.Monad
import cats.data.EitherT
import monix.eval.Task
import cats.syntax.either._
import monix.cats.MonixToCatsConversions
import monix.eval.Task

import scala.language.{higherKinds, implicitConversions}

package object services {

object TaskService {

implicit val taskMonad: Monad[Task] =
monix.cats.monixToCatsMonad[Task](monix.eval.Task.nondeterminism)
object TaskService extends MonixToCatsConversions {

trait NineCardException extends RuntimeException {
def message: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class UserProcessImpl(
androidId <- persistenceServices.getAndroidId
_ <- apiServices.updateInstallation(deviceToken)(RequestConfig(apiKey, sessionToken, androidId))
} yield ()).resolve[UserException]
case _ => TaskService(Task(Right(0)))
case _ => TaskService.right(0)
}

private[this] def findUserById(id: Int): TaskService[User] =
Expand Down
2 changes: 1 addition & 1 deletion project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ object Versions {
val gfcTimeUUIDV = "0.0.6"
val monixV = "2.1.1"
val catsV = "0.8.1"
val flowUpV= "0.1.6"
val flowUpV= "0.1.8"
}

0 comments on commit 1cab792

Please sign in to comment.