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

Class BoundedPubSubSingleSubscription

packages/effect/src/PubSub.ts:1974–2039  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1972}
1973
1974class BoundedPubSubSingleSubscription<in out A> implements PubSub.BackingSubscription<A> {
1975 private self: BoundedPubSubSingle<A>
1976 private subscriberIndex: number
1977 private unsubscribed: boolean
1978
1979 constructor(
1980 self: BoundedPubSubSingle<A>,
1981 subscriberIndex: number,
1982 unsubscribed: boolean
1983 ) {
1984 this.self = self
1985 this.subscriberIndex = subscriberIndex
1986 this.unsubscribed = unsubscribed
1987 }
1988
1989 isEmpty(): boolean {
1990 return (
1991 this.unsubscribed ||
1992 this.self.subscribers === 0 ||
1993 this.subscriberIndex === this.self.publisherIndex
1994 )
1995 }
1996
1997 size() {
1998 return this.isEmpty() ? 0 : 1
1999 }
2000
2001 poll(): A | MutableList.Empty {
2002 if (this.isEmpty()) {
2003 return MutableList.Empty
2004 }
2005 const elem = this.self.value
2006 this.self.subscribers -= 1
2007 if (this.self.subscribers === 0) {
2008 this.self.value = AbsentValue as unknown as A
2009 }
2010 this.subscriberIndex += 1
2011 return elem
2012 }
2013
2014 pollUpTo(n: number): Array<A> {
2015 if (this.isEmpty() || n < 1) {
2016 return []
2017 }
2018 const a = this.self.value
2019 this.self.subscribers -= 1
2020 if (this.self.subscribers === 0) {
2021 this.self.value = AbsentValue as unknown as A
2022 }
2023 this.subscriberIndex += 1
2024 return [a]
2025 }
2026
2027 unsubscribe(): void {
2028 if (!this.unsubscribed) {
2029 this.unsubscribed = true
2030 this.self.subscriberCount -= 1
2031 if (this.subscriberIndex !== this.self.publisherIndex) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected