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

Class BoundedPubSubSingleSubscription

packages/effect/src/PubSub.ts:2204–2263  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2202}
2203
2204class BoundedPubSubSingleSubscription<in out A> implements PubSub.BackingSubscription<A> {
2205 private self: BoundedPubSubSingle<A>
2206 private subscriberIndex: number
2207 private unsubscribed: boolean
2208
2209 constructor(
2210 self: BoundedPubSubSingle<A>,
2211 subscriberIndex: number,
2212 unsubscribed: boolean
2213 ) {
2214 this.self = self
2215 this.subscriberIndex = subscriberIndex
2216 this.unsubscribed = unsubscribed
2217 }
2218
2219 isEmpty(): boolean {
2220 return (
2221 this.unsubscribed ||
2222 this.self.subscribers === 0 ||
2223 this.subscriberIndex === this.self.publisherIndex
2224 )
2225 }
2226
2227 size() {
2228 return this.isEmpty() ? 0 : 1
2229 }
2230
2231 poll(): A | MutableList.Empty {
2232 if (this.isEmpty()) {
2233 return MutableList.Empty
2234 }
2235 const elem = this.self.value
2236 this.self.subscribers -= 1
2237 if (this.self.subscribers === 0) {
2238 this.self.value = AbsentValue as unknown as A
2239 }
2240 this.subscriberIndex = this.self.publisherIndex
2241 return elem
2242 }
2243
2244 pollUpTo(n: number): Array<A> {
2245 if (Count.normalize(n) < 1 || this.isEmpty()) {
2246 return []
2247 }
2248 return [this.poll() as A]
2249 }
2250
2251 unsubscribe(): void {
2252 if (!this.unsubscribed) {
2253 this.unsubscribed = true
2254 this.self.subscriberCount -= 1
2255 if (this.self.subscribers !== 0 && this.subscriberIndex !== this.self.publisherIndex) {
2256 this.self.subscribers -= 1
2257 if (this.self.subscribers === 0) {
2258 this.self.value = AbsentValue as unknown as A
2259 }
2260 }
2261 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected