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

Class BoundedPubSubPow2Subscription

packages/effect/src/PubSub.ts:1798–1889  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1796}
1797
1798class BoundedPubSubPow2Subscription<in out A> implements PubSub.BackingSubscription<A> {
1799 private self: BoundedPubSubPow2<A>
1800 private subscriberIndex: number
1801 private unsubscribed: boolean
1802
1803 constructor(
1804 self: BoundedPubSubPow2<A>,
1805 subscriberIndex: number,
1806 unsubscribed: boolean
1807 ) {
1808 this.self = self
1809 this.subscriberIndex = subscriberIndex
1810 this.unsubscribed = unsubscribed
1811 }
1812
1813 isEmpty(): boolean {
1814 return (
1815 this.unsubscribed ||
1816 this.self.publisherIndex === this.subscriberIndex ||
1817 this.self.publisherIndex === this.self.subscribersIndex
1818 )
1819 }
1820
1821 size() {
1822 if (this.unsubscribed) {
1823 return 0
1824 }
1825 return this.self.publisherIndex - Math.max(this.subscriberIndex, this.self.subscribersIndex)
1826 }
1827
1828 poll(): A | MutableList.Empty {
1829 if (this.unsubscribed) {
1830 return MutableList.Empty
1831 }
1832 this.subscriberIndex = Math.max(this.subscriberIndex, this.self.subscribersIndex)
1833 if (this.subscriberIndex !== this.self.publisherIndex) {
1834 const index = this.subscriberIndex & this.self.mask
1835 const elem = this.self.array[index]!
1836 this.self.subscribers[index] -= 1
1837 if (this.self.subscribers[index] === 0) {
1838 this.self.array[index] = AbsentValue as unknown as A
1839 this.self.subscribersIndex += 1
1840 }
1841 this.subscriberIndex += 1
1842 return elem
1843 }
1844 return MutableList.Empty
1845 }
1846
1847 pollUpTo(n: number): Array<A> {
1848 if (this.unsubscribed) {
1849 return []
1850 }
1851 this.subscriberIndex = Math.max(this.subscriberIndex, this.self.subscribersIndex)
1852 const size = this.self.publisherIndex - this.subscriberIndex
1853 const toPoll = Math.min(n, size)
1854 if (toPoll <= 0) {
1855 return []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected