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

Class BoundedPubSubPow2

packages/effect/src/PubSub.ts:1696–1796  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1694}
1695
1696class BoundedPubSubPow2<in out A> implements PubSub.Atomic<A> {
1697 array: Array<A>
1698 replayIndices: Array<number>
1699 mask: number
1700 publisherIndex = 0
1701 subscribers: Array<number>
1702 subscriberCount = 0
1703 subscribersIndex = 0
1704
1705 readonly capacity: number
1706 readonly replayBuffer: ReplayBuffer<A> | undefined
1707
1708 constructor(capacity: number, replayBuffer: ReplayBuffer<A> | undefined) {
1709 this.capacity = capacity
1710 this.replayBuffer = replayBuffer
1711 this.array = Array.from({ length: capacity })
1712 this.replayIndices = replayBuffer ? Array.from({ length: capacity }) : []
1713 this.mask = capacity - 1
1714 this.subscribers = Array.from({ length: capacity })
1715 }
1716
1717 replayWindow(): PubSub.ReplayWindow<A> {
1718 return this.replayBuffer ? new ReplayWindowImpl(this.replayBuffer) : emptyReplayWindow
1719 }
1720
1721 isEmpty(): boolean {
1722 return this.publisherIndex === this.subscribersIndex
1723 }
1724
1725 isFull(): boolean {
1726 return this.publisherIndex === this.subscribersIndex + this.capacity
1727 }
1728
1729 size(): number {
1730 return this.publisherIndex - this.subscribersIndex
1731 }
1732
1733 publish(value: A): boolean {
1734 if (this.isFull()) {
1735 return false
1736 }
1737 const replayIndex = this.replayBuffer?.offer(value)
1738 if (this.subscriberCount !== 0) {
1739 const index = this.publisherIndex & this.mask
1740 this.array[index] = value
1741 if (replayIndex !== undefined) {
1742 this.replayIndices[index] = replayIndex
1743 }
1744 this.subscribers[index] = this.subscriberCount
1745 this.publisherIndex += 1
1746 }
1747 return true
1748 }
1749
1750 publishAll(elements: Iterable<A>): Array<A> {
1751 if (this.subscriberCount === 0) {
1752 if (this.replayBuffer) {
1753 this.replayBuffer.offerAll(elements)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected