diff --git a/README.md b/README.md index 3be38e1..98c6f2e 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ def start[Msg, State]( It requires: - the `name` of the entity type. Entities will be distributed on all the nodes of the cluster where `start` was called with this `name`. - `onMessage` is the behavior of the sharded entity. For each received message, it will run an effect of type `ZIO[Entity[State], Nothing, Unit]`: - - `Entity[State]` gives you access to a `Ref[Option[State]]` which you can use to read or modify the state of the entity. The state is set to `None` when the entity is started. This `Entity` object also allows you to stop the entity from within (e.g. after some time of inactivity). + - `Entity[State]` gives you access to a `Ref[Option[State]]` which you can use to read or modify the state of the entity. The state is set to `None` when the entity is started. This `Entity` object also allows you to get the entity ID and to stop the entity from within (e.g. after some time of inactivity). - `Nothing` means the effect should not fail, you must catch and handle potential errors - `Unit` means the effect should not return anything - `numberOfShards` indicates how entities will be split across nodes. See [this page](https://doc.akka.io/docs/akka/current/cluster-sharding.html#an-example) for more information. diff --git a/src/main/scala/zio/akka/cluster/sharding/Entity.scala b/src/main/scala/zio/akka/cluster/sharding/Entity.scala index 3eccbb2..f6544da 100644 --- a/src/main/scala/zio/akka/cluster/sharding/Entity.scala +++ b/src/main/scala/zio/akka/cluster/sharding/Entity.scala @@ -3,6 +3,7 @@ package zio.akka.cluster.sharding import zio.{ Ref, UIO } trait Entity[State] { + def id: String def state: Ref[Option[State]] def stop: UIO[Unit] } diff --git a/src/main/scala/zio/akka/cluster/sharding/Sharding.scala b/src/main/scala/zio/akka/cluster/sharding/Sharding.scala index f25c51c..d80d668 100644 --- a/src/main/scala/zio/akka/cluster/sharding/Sharding.scala +++ b/src/main/scala/zio/akka/cluster/sharding/Sharding.scala @@ -65,7 +65,7 @@ object Sharding { * @param numberOfShards a fixed number of shards * @return a [[Sharding]] object that can be used to send messages to sharded entities on other nodes */ - def startProxy[Msg, State]( + def startProxy[Msg]( name: String, role: Option[String], numberOfShards: Int = 100 @@ -112,6 +112,7 @@ object Sharding { val ref: Ref[Option[State]] = rts.unsafeRun(Ref.make[Option[State]](None)) val actorContext: ActorContext = context val entity: Entity[State] = new Entity[State] { + override def id: String = context.self.path.name override def state: Ref[Option[State]] = ref override def stop: UIO[Unit] = UIO(actorContext.stop(self)) }