graphql-subscriptions icon indicating copy to clipboard operation
graphql-subscriptions copied to clipboard

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

Open jisaacks opened this issue 1 year ago • 1 comments

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)
  }
}

jisaacks avatar Feb 22 '24 12:02 jisaacks

they should be completely removed from PubSubAsyncIterator interface

falkenhawk avatar Jun 05 '25 08:06 falkenhawk