Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the pubsub-async-iterator properties and methods be protected instead of private? #271

Open
jisaacks opened this issue Feb 22, 2024 · 0 comments

Comments

@jisaacks
Copy link

I am trying to make a version of the pubsub async iterator that throws an error if the pushQueue goes over a certain size. Right now it is difficult to extend the existing pubsub async iterator to add this because most things are set to private and cannot be accessed by an overriding class.

Right now I am having to do this:

class MaxQueuePubSubAsyncIterator<T> extends PubSubAsyncIterator<T> {
  constructor(pubsub: PubSubEngine, eventNames: string | string[]) {
    super(pubsub, eventNames)
    // pushValue and pushQueue are private so this is a hack to access/override them.
    const super_pushValue = this["pushValue"] as ((event:T) => Promise<void>)
    this["pushValue"] = async (event: T) => {
      if ((this["pushQueue"] as T[]).length >= MAX_QUEUE) {
        throw new MaxQueueError('Maximum Queue Size Reached')
      }
      return await super_pushValue(event)
    }
  }
}

However if pushValue and pushQueue were marked as protected instead of private, they would still be inaccessible in normal usage but could be accessed from extending classes and simplify this code quite a bit:

class MaxQueuePubSubAsyncIterator<T> extends PubSubAsyncIterator<T> {
  override async pushValue(event: T) {
    if (this.pushQueue.length >= MAX_QUEUE) {
      throw new MaxQueueError('Maximum Queue Size Reached')
    }
    return super.pushValue(event)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant