Skip to content

Commit

Permalink
Add entity ID in Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Ricadat committed Jul 6, 2019
1 parent fca3ae4 commit 89ca74b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/zio/akka/cluster/sharding/Entity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
3 changes: 2 additions & 1 deletion src/main/scala/zio/akka/cluster/sharding/Sharding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 89ca74b

Please sign in to comment.