| 1 | /** |
| 2 | * The `SingletonAddress` module models the runtime address assigned to a cluster |
| 3 | * singleton registration. The address pairs the singleton `name` with the |
| 4 | * `ShardId` selected from that name and its shard group, giving sharding one |
| 5 | * stable value for registration events, equality, hashing, and local singleton |
| 6 | * fiber tracking. |
| 7 | * |
| 8 | * @stability unstable |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | import * as Equal from "../Equal.ts" |
| 12 | import * as Hash from "../Hash.ts" |
| 13 | import * as Schema from "../Schema.ts" |
| 14 | import { ShardId } from "./ShardId.ts" |
| 15 | |
| 16 | const TypeId = "~effect/cluster/SingletonAddress" |
| 17 | |
| 18 | /** |
| 19 | * Represents the unique address of an singleton within the cluster. |
| 20 | * |
| 21 | * @stability unstable |
| 22 | * @category schemas |
| 23 | * @since 4.0.0 |
| 24 | */ |
| 25 | export class SingletonAddress extends Schema.Class<SingletonAddress>(TypeId)({ |
| 26 | shardId: ShardId, |
| 27 | name: Schema.String |
| 28 | }) { |
| 29 | /** |
| 30 | * Marks this value as a cluster singleton address for runtime guards. |
| 31 | * |
| 32 | * @stability unstable |
| 33 | * @since 4.0.0 |
| 34 | */ |
| 35 | readonly [TypeId] = TypeId; |
| 36 | /** |
| 37 | * Computes a structural hash from the singleton name and shard id. |
| 38 | * |
| 39 | * @stability unstable |
| 40 | * @since 4.0.0 |
| 41 | */ |
| 42 | [Hash.symbol]() { |
| 43 | return Hash.string(`${this.name}:${this.shardId.toString()}`) |
| 44 | } |
| 45 | /** |
| 46 | * Compares singleton addresses by name and shard id. |
| 47 | * |
| 48 | * @stability unstable |
| 49 | * @since 4.0.0 |
| 50 | */ |
| 51 | [Equal.symbol](that: SingletonAddress): boolean { |
| 52 | return this.name === that.name && Equal.equals(this.shardId, that.shardId) |
| 53 | } |
| 54 | } |