{"version":3,"file":"connection-pool.mjs","names":[],"sources":["../../src/mixins/connection-pool.ts"],"sourcesContent":["import type { ClientMixin } from \"./base-mixin\";\nimport type { ChatClient, ConnectionPredicate } from \"~/client/client\";\nimport type { SingleConnection } from \"~/client/connection\";\nimport { applyReplacements } from \"~/utils/apply-function-replacements\";\n\nexport interface ConnectionPoolOptions {\n  poolSize: number; //how many fast connection to create\n}\n\nexport class ConnectionPool implements ClientMixin {\n  private client: ChatClient;\n  private poolSize: number;\n\n  constructor(client: ChatClient, options: ConnectionPoolOptions) {\n    this.client = client;\n    this.poolSize = options.poolSize;\n  }\n\n  public applyToClient(client: ChatClient): void {\n    client.connectionPool = this;\n    const replacement = (\n      oldFunction: (predicate?: ConnectionPredicate) => SingleConnection,\n      predicate?: ConnectionPredicate,\n    ): SingleConnection => {\n      this.ensureEnoughConnections();\n      return oldFunction(predicate);\n    };\n\n    applyReplacements(this, client, {\n      requireConnection: replacement,\n    });\n  }\n\n  public ensureEnoughConnections(): void {\n    while (this.client.connections.length < this.poolSize) {\n      this.client.newConnection();\n    }\n  }\n}\n"],"mappings":";;AASA,IAAa,iBAAb,MAAmD;CACjD;CACA;CAEA,YAAY,QAAoB,SAAgC;AAC9D,OAAK,SAAS;AACd,OAAK,WAAW,QAAQ;;CAG1B,cAAqB,QAA0B;AAC7C,SAAO,iBAAiB;EACxB,MAAM,eACJ,aACA,cACqB;AACrB,QAAK,yBAAyB;AAC9B,UAAO,YAAY,UAAU;;AAG/B,oBAAkB,MAAM,QAAQ,EAC9B,mBAAmB,aACpB,CAAC;;CAGJ,0BAAuC;AACrC,SAAO,KAAK,OAAO,YAAY,SAAS,KAAK,SAC3C,MAAK,OAAO,eAAe"}