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

Class BoundedPubSubArbSubscription

packages/effect/src/PubSub.ts:1602–1694  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1600}
1601
1602class BoundedPubSubArbSubscription<in out A> implements PubSub.BackingSubscription<A> {
1603 private self: BoundedPubSubArb<A>
1604 private subscriberIndex: number
1605 private unsubscribed: boolean
1606
1607 constructor(
1608 self: BoundedPubSubArb<A>,
1609 subscriberIndex: number,
1610 unsubscribed: boolean
1611 ) {
1612 this.self = self
1613 this.subscriberIndex = subscriberIndex
1614 this.unsubscribed = unsubscribed
1615 }
1616
1617 isEmpty(): boolean {
1618 return (
1619 this.unsubscribed ||
1620 this.self.publisherIndex === this.subscriberIndex ||
1621 this.self.publisherIndex === this.self.subscribersIndex
1622 )
1623 }
1624
1625 size() {
1626 if (this.unsubscribed) {
1627 return 0
1628 }
1629 return this.self.publisherIndex - Math.max(this.subscriberIndex, this.self.subscribersIndex)
1630 }
1631
1632 poll(): A | MutableList.Empty {
1633 if (this.unsubscribed) {
1634 return MutableList.Empty
1635 }
1636 this.subscriberIndex = Math.max(this.subscriberIndex, this.self.subscribersIndex)
1637 if (this.subscriberIndex !== this.self.publisherIndex) {
1638 const index = this.subscriberIndex % this.self.capacity
1639 const elem = this.self.array[index]!
1640 this.self.subscribers[index] -= 1
1641 if (this.self.subscribers[index] === 0) {
1642 this.self.array[index] = AbsentValue as unknown as A
1643 this.self.subscribersIndex += 1
1644 }
1645 this.subscriberIndex += 1
1646 return elem
1647 }
1648 return MutableList.Empty
1649 }
1650
1651 pollUpTo(n: number): Array<A> {
1652 if (this.unsubscribed) {
1653 return []
1654 }
1655 this.subscriberIndex = Math.max(this.subscriberIndex, this.self.subscribersIndex)
1656 const size = this.self.publisherIndex - this.subscriberIndex
1657 const toPoll = Math.min(n, size)
1658 if (toPoll <= 0) {
1659 return []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected