Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move name into basecomponent #414

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ trait ClusterComponentsCassandraPersistenceServiceImpl extends ClusterComponents
override def deleteSingletonEntity(name: String) = ServiceCall { _ =>
val maybeSingletonPersistenceId = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collectFirst { case (outerComponent: ClusterComponent.Singleton.EventSourced, innerComponent) if outerComponent.name == name =>
.collectFirst { case (outerComponent: ClusterComponent.Singleton.EventSourced, innerComponent) if innerComponent.name == name =>
innerComponent.asInstanceOf[outerComponent.BaseComponent].persistenceId
}

lazy val singletonEntities = clusterComponents.map(_.component).collect {
case outerComponent: ClusterComponent.Singleton.EventSourced => outerComponent.name
}
lazy val singletonEntities = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collect { case (_: ClusterComponent.Singleton.EventSourced, innerComponent) =>
innerComponent.name
}

maybeSingletonPersistenceId.fold(
throw new Exception(s"singleton with name=$name not found, existing singletonEntities=$singletonEntities")
Expand All @@ -55,13 +57,16 @@ trait ClusterComponentsCassandraPersistenceServiceImpl extends ClusterComponents
override def deleteShardedEntities(name: String) = ServiceCall { _ =>
val maybeTypeKey = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collectFirst { case (outerComponent: ClusterComponent.Sharded.EventSourced, innerComponent) if outerComponent.name == name =>
.collectFirst { case (outerComponent: ClusterComponent.Sharded.EventSourced, innerComponent) if innerComponent.name == name =>
innerComponent.asInstanceOf[outerComponent.BaseComponent].typeKey
}

lazy val shardedEntities = clusterComponents.map(_.component).collect {
case outerComponent: ClusterComponent.Sharded.EventSourced => outerComponent.name
}
lazy val shardedEntities =
clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collect { case (_: ClusterComponent.Sharded.EventSourced, innerComponent) =>
innerComponent.name
}

maybeTypeKey.fold(
throw new Exception(s"shardedEntity with name=$name not found, existing shardedEntities=$shardedEntities")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import net.sc8s.akka.components.persistence.projection.{ManagedProjection, Proje
import scala.concurrent.Future

trait CassandraProjection extends EventSourcedT.ProjectionT {
_: EventSourcedT#EventSourcedBaseComponentT =>
outerSelf: EventSourcedT#EventSourcedBaseComponentT =>

val numberOfProjectionInstances = 1

Expand All @@ -26,7 +26,7 @@ trait CassandraProjection extends EventSourcedT.ProjectionT {
actorSystem: ActorSystem[_]
): ManagedProjection[EventEnvelope[EventT]] = {
val projectionIds = (0 until numberOfProjectionInstances).map(tagIndex =>
ProjectionId(projection.name, generateTag(componentName, tagIndex))
ProjectionId(projection.name, generateTag(outerSelf.name, tagIndex))
)

new ManagedProjection[EventEnvelope[EventT]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ trait ClusterComponentsR2dbcPersistenceServiceImpl extends ClusterComponentsR2db
override def deleteSingletonEntity(name: String) = ServiceCall { _ =>
val maybeSingletonPersistenceId = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collectFirst { case (outerComponent: ClusterComponent.Singleton.EventSourced, innerComponent) if outerComponent.name == name =>
.collectFirst { case (outerComponent: ClusterComponent.Singleton.EventSourced, innerComponent) if innerComponent.name == name =>
innerComponent.asInstanceOf[outerComponent.BaseComponent].persistenceId
}

lazy val singletonEntities = clusterComponents.map(_.component).collect {
case outerComponent: ClusterComponent.Singleton.EventSourced => outerComponent.name
}
lazy val singletonEntities = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collect { case (_: ClusterComponent.Singleton.EventSourced, innerComponent) =>
innerComponent.name
}

maybeSingletonPersistenceId.fold(
throw new Exception(s"singleton with name=$name not found, existing singletonEntities=$singletonEntities")
Expand All @@ -53,13 +55,15 @@ trait ClusterComponentsR2dbcPersistenceServiceImpl extends ClusterComponentsR2db
override def deleteShardedEntities(name: String) = ServiceCall { _ =>
val maybeTypeKey = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collectFirst { case (outerComponent: ClusterComponent.Sharded.EventSourced, innerComponent) if outerComponent.name == name =>
.collectFirst { case (outerComponent: ClusterComponent.Sharded.EventSourced, innerComponent) if innerComponent.name == name =>
innerComponent.asInstanceOf[outerComponent.BaseComponent].typeKey
}

lazy val shardedEntities = clusterComponents.map(_.component).collect {
case outerComponent: ClusterComponent.Sharded.EventSourced => outerComponent.name
}
lazy val shardedEntities = clusterComponents
.map(wiredComponent => wiredComponent.component -> wiredComponent.innerComponent)
.collect { case (_: ClusterComponent.Sharded.EventSourced, innerComponent) =>
innerComponent.name
}

maybeTypeKey.fold(
throw new Exception(s"shardedEntity with name=$name not found, existing shardedEntities=$shardedEntities")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.sc8s.akka.components.testkit
import akka.actor.testkit.typed.scaladsl.TestProbe
import akka.actor.typed.ActorRef
import akka.actor.typed.scaladsl.Behaviors
import akka.cluster.sharding.typed.scaladsl.{EntityContext, EntityRef}
import akka.cluster.sharding.typed.scaladsl.{EntityContext, EntityRef, EntityTypeKey}
import akka.cluster.sharding.typed.testkit.scaladsl.TestEntityRef
import akka.persistence.query.Offset
import akka.persistence.testkit.scaladsl.EventSourcedBehaviorTestKit
Expand Down Expand Up @@ -174,7 +174,8 @@ trait ClusterComponentTestKit {
implicit classTag: ClassTag[outerComponent.SerializableCommand]
): ShardedComponent[OuterComponentT] = {
new ClusterComponent.ShardedComponent[OuterComponentT] {
override def entityRefFor(entityId: outerComponent.EntityId): EntityRef[outerComponent.SerializableCommand] = TestEntityRef(outerComponent.generateTypeKey, outerComponent.entityIdCodec.encode(entityId), entityRefProbes(entityId).ref)
override def entityRefFor(entityId: outerComponent.EntityId): EntityRef[outerComponent.SerializableCommand] = TestEntityRef(
EntityTypeKey[outerComponent.SerializableCommand]("any"), outerComponent.entityIdCodec.encode(entityId), entityRefProbes(entityId).ref)

override private[components] lazy val innerComponent = ???
override private[components] lazy val component: outerComponent.type = outerComponent
Expand Down
13 changes: 6 additions & 7 deletions akka-components/src/main/scala/ClusterComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ object ClusterComponent {
// allows the user to wrap the behavior, especially useful to wrap EventSourcedBehavior in e.g. Behaviors.withTimers
def wrapBehavior: BehaviorComponentContextS => BehaviorS => Behavior[Command] = _ => identity

val name = outerSelf.name

private[components] def behaviorTransformer: (BehaviorComponentContextS, BehaviorS) => BehaviorS = (_, behavior) => behavior

final val transformedBehavior: BehaviorComponentContextS => Behavior[outerSelf.Command] =
Expand All @@ -65,6 +67,7 @@ object ClusterComponent {
// this can't be moved into the BaseComponent itself as otherwise circular dependencies between components lead to initialization loops
def init(component: => BaseComponent)(implicit actorSystem: => ActorSystem[_]): Wiring

@deprecated("define in BaseComponent instead", "0.95.0")
val name: String

val additionalSerializers: Seq[CirceSerializer[_]] = Nil
Expand Down Expand Up @@ -98,7 +101,6 @@ object ClusterComponent {

// for mixin traits accessibility
private[components] type EventT = Event
private[components] val componentName = name

override private[components] type BehaviorS = EventSourcedBehavior[Command, Event, State]

Expand Down Expand Up @@ -223,7 +225,7 @@ object ClusterComponent {

private[components] def delayedInit(): Unit = managedProjections.foreach(_.init())

override def toString = s"ClusterComponent(name = ${component.name}, serializers = ${serializers.map(_.entityClass)})"
override def toString = s"ClusterComponent(name = ${innerComponent.name}, serializers = ${serializers.map(_.entityClass)})"
}

trait SingletonComponent[OuterComponentT <: Singleton.SingletonT] extends Component[OuterComponentT] {
Expand Down Expand Up @@ -298,7 +300,7 @@ object ClusterComponent {

override private[components] type ComponentContextS = ComponentContext with ComponentContext.EventSourced

private[components] val persistenceId = PersistenceId.ofUniqueId(name)
private[components] lazy val persistenceId = PersistenceId.ofUniqueId(name)

override private[components] type BehaviorComponentContextS = ComponentContext with ComponentContext.Actor[Command] with ComponentContext.EventSourced

Expand Down Expand Up @@ -432,9 +434,6 @@ object ClusterComponent {

implicit val entityIdCodec: EntityIdCodec[EntityId]

// an initialized typeKey is available in the InnerComponent, in general you should not need this
def generateTypeKey(implicit classTag: ClassTag[SerializableCommand]) = EntityTypeKey[SerializableCommand](name)

override type BaseComponent <: ShardedBaseComponentT

override def init(_innerComponent: => BaseComponent)(implicit actorSystem: => ActorSystem[_]) =
Expand All @@ -460,7 +459,7 @@ object ClusterComponent {

override private[components] type BehaviorComponentContextS = ComponentContextS with ComponentContext.Actor[Command] with ComponentContext.ShardedEntity[SerializableCommand]

val typeKey: EntityTypeKey[SerializableCommand] = generateTypeKey
lazy val typeKey: EntityTypeKey[SerializableCommand] = EntityTypeKey[SerializableCommand](name)

def entityRefFor(entityId: EntityId)(implicit actorSystem: ActorSystem[_]) = {
val clusterSharding: ClusterSharding = ClusterSharding(actorSystem)
Expand Down