MCPcopy Create free account
hub / github.com/Effect-TS/effect / BoundedPubSubArb

Class BoundedPubSubArb

packages/effect/src/PubSub.ts:1502–1600  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1500 )
1501
1502class BoundedPubSubArb<in out A> implements PubSub.Atomic<A> {
1503 array: Array<A>
1504 replayIndices: Array<number>
1505 publisherIndex = 0
1506 subscribers: Array<number>
1507 subscriberCount = 0
1508 subscribersIndex = 0
1509
1510 readonly capacity: number
1511 readonly replayBuffer: ReplayBuffer<A> | undefined
1512
1513 constructor(capacity: number, replayBuffer: ReplayBuffer<A> | undefined) {
1514 this.capacity = capacity
1515 this.replayBuffer = replayBuffer
1516 this.array = Array.from({ length: capacity })
1517 this.replayIndices = replayBuffer ? Array.from({ length: capacity }) : []
1518 this.subscribers = Array.from({ length: capacity })
1519 }
1520
1521 replayWindow(): PubSub.ReplayWindow<A> {
1522 return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow
1523 }
1524
1525 isEmpty(): boolean {
1526 return this.publisherIndex === this.subscribersIndex
1527 }
1528
1529 isFull(): boolean {
1530 return this.publisherIndex === this.subscribersIndex + this.capacity
1531 }
1532
1533 size(): number {
1534 return this.publisherIndex - this.subscribersIndex
1535 }
1536
1537 publish(value: A): boolean {
1538 if (this.isFull()) {
1539 return false
1540 }
1541 const replayIndex = this.replayBuffer?.offer(value)
1542 if (this.subscriberCount !== 0) {
1543 const index = this.publisherIndex % this.capacity
1544 this.array[index] = value
1545 if (replayIndex !== undefined) {
1546 this.replayIndices[index] = replayIndex
1547 }
1548 this.subscribers[index] = this.subscriberCount
1549 this.publisherIndex += 1
1550 }
1551 return true
1552 }
1553
1554 publishAll(elements: Iterable<A>): Array<A> {
1555 if (this.subscriberCount === 0) {
1556 if (this.replayBuffer) {
1557 this.replayBuffer.offerAll(elements)
1558 }
1559 return []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected