Skip to content

Commit

Permalink
Horrible hack to access latestSeqNr in typed persistence akka#25113
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Mar 5, 2019
1 parent 6071d89 commit 9df2087
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import akka.util.OptionVal
import Behavior._

protected var behavior: Behavior[T] = _initialBehavior
def getBehavior: Behavior[T] = behavior

private var _ctx: ActorContextAdapter[T] = _
def ctx: ActorContextAdapter[T] =
Expand Down Expand Up @@ -211,7 +212,7 @@ import akka.util.OptionVal
}

protected def initializeContext(): Unit = {
_ctx = new ActorContextAdapter[T](context)
_ctx = new ActorContextAdapter[T](context, this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import scala.concurrent.duration._
/**
* INTERNAL API. Wrapping an [[akka.actor.ActorContext]] as an [[TypedActorContext]].
*/
@InternalApi private[akka] final class ActorContextAdapter[T](val untypedContext: untyped.ActorContext) extends ActorContextImpl[T] {
@InternalApi private[akka] final class ActorContextAdapter[T](val untypedContext: untyped.ActorContext, adapter: ActorAdapter[T]) extends ActorContextImpl[T] {

import ActorRefAdapter.toUntyped

private[akka] def currentBehavior: Behavior[T] = adapter.getBehavior

// lazily initialized
private var actorLogger: OptionVal[Logger] = OptionVal.None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import akka.annotation.InternalApi
*/
@DoNotInherit
@ApiMayChange
trait ActorContext[T] extends TypedActorContext[T] { this: akka.actor.typed.javadsl.ActorContext[T]
trait ActorContext[T] extends TypedActorContext[T] {

/**
* Get the `javadsl` of this `ActorContext`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.persistence.typed.scaladsl

import akka.actor.typed.internal.adapter.ActorContextAdapter
import akka.actor.typed.scaladsl.ActorContext
import akka.persistence.typed.internal.Running

object PersistentActorContext {

/**
* Access the to details about the persistent actor from the actor context, can only be used in response
* to a command, will throw `IllegalState` for other cases
*/
def apply(context: ActorContext[_]): PersistentActorContext = {
context match {
case impl: ActorContextAdapter[_]
impl.currentBehavior match {
case w: Running.WithStateAccessible
new PersistentActorContext {
def lastSequenceNr: Long = w.state.seqNr
}

case s throw new IllegalStateException(s"Cannot extract the PersistentActorContext in state ${s.getClass.getName}")

}

case c throw new IllegalStateException(s"Cannot extract the PersistentActorContext from context ${c.getClass.getName}")
}
}

}

trait PersistentActorContext {
def lastSequenceNr: Long
}

0 comments on commit 9df2087

Please sign in to comment.