{"version":3,"file":"NotificationSubscription.mjs","names":[],"sources":["../../src/notifications/NotificationSubscription.ts"],"sourcesContent":["import { v4 } from \"uuid\";\nimport type { ConnectedPlugin } from \"../types/ConnectedPlugin\";\nimport type { ConnectedContext } from \"../types/ConnectedContext\";\nimport type { SubscriptionCallbacks } from \"./SubscriptionCallbacks\";\nimport type { NotificationCallbackError } from \"../results/error/NotificationErrors\";\n\n/**\n * @internal\n * Abstract class for notification subscription methods.\n */\nexport abstract class NotificationSubscription<\n  Plugin extends ConnectedPlugin,\n  NotificationMessage,\n> {\n  protected resource: Plugin[\"types\"][\"resource\"];\n  protected parentSubscription: (message: NotificationMessage) => void;\n  protected context: ConnectedContext<Plugin[]>;\n  protected subscriptions: Record<\n    string,\n    SubscriptionCallbacks<NotificationMessage>\n  > = {};\n  private isOpen: boolean = false;\n\n  constructor(\n    resource: Plugin[\"types\"][\"resource\"],\n    parentSubscription: (message: NotificationMessage) => void,\n    context: ConnectedContext<Plugin[]>,\n  ) {\n    this.resource = resource;\n    this.parentSubscription = parentSubscription;\n    this.context = context;\n  }\n\n  public isSubscribedToNotifications(): boolean {\n    return this.isOpen;\n  }\n\n  /**\n   * ===========================================================================\n   * PUBLIC\n   * ===========================================================================\n   */\n\n  /**\n   * @internal\n   * subscribeToNotifications\n   */\n  async subscribeToNotifications(\n    subscriptionCallbacks?: SubscriptionCallbacks<NotificationMessage>,\n  ): Promise<string> {\n    const subscriptionId = v4();\n    this.subscriptions[subscriptionId] = subscriptionCallbacks ?? {};\n    if (!this.isOpen) {\n      await this.open();\n      this.setIsOpen(true);\n    }\n    return subscriptionId;\n  }\n\n  /**\n   * @internal\n   * unsubscribeFromNotification\n   */\n  async unsubscribeFromNotification(subscriptionId: string): Promise<void> {\n    if (\n      !!this.subscriptions[subscriptionId] &&\n      Object.keys(this.subscriptions).length === 1\n    ) {\n      await this.close();\n      this.setIsOpen(false);\n    }\n    delete this.subscriptions[subscriptionId];\n  }\n\n  /**\n   * @internal\n   * unsubscribeFromAllNotifications\n   */\n  async unsubscribeFromAllNotifications(): Promise<void> {\n    await Promise.all(\n      Object.keys(this.subscriptions).map((id) =>\n        this.unsubscribeFromNotification(id),\n      ),\n    );\n  }\n\n  /**\n   * ===========================================================================\n   * HELPERS\n   * ===========================================================================\n   */\n\n  /**\n   * @internal\n   * Opens the subscription\n   */\n  protected abstract open(): Promise<void>;\n\n  /**\n   * @internal\n   * Closes the subscription\n   */\n  protected abstract close(): Promise<void>;\n\n  /**\n   * ===========================================================================\n   * CALLBACKS\n   * ===========================================================================\n   */\n\n  /**\n   * @internal\n   * onNotification\n   */\n  protected onNotification(message: NotificationMessage): void {\n    this.parentSubscription(message);\n    Object.values(this.subscriptions).forEach(({ onNotification }) => {\n      onNotification?.(message);\n    });\n  }\n\n  /**\n   * @internal\n   * onNotificationError\n   */\n  protected onNotificationError(\n    message: NotificationCallbackError<Plugin[\"types\"][\"resource\"]>,\n  ): void {\n    Object.values(this.subscriptions).forEach(({ onNotificationError }) => {\n      onNotificationError?.(message);\n    });\n    if (message.type === \"disconnectedNotAttemptingReconnectError\") {\n      this.setIsOpen(false);\n    }\n  }\n\n  /**\n   * @internal\n   * setIsOpen\n   */\n  protected setIsOpen(status: boolean) {\n    const shouldUpdate = status !== this.isOpen;\n    this.isOpen = status;\n    if (shouldUpdate) this.resource.emit(\"update\");\n  }\n}\n"],"mappings":";;;;;;AAUA,IAAsB,2BAAtB,MAGE;CAUA,YACE,UACA,oBACA,SACA;AAVF,OAAU,gBAGN,EAAE;AACN,OAAQ,SAAkB;AAOxB,OAAK,WAAW;AAChB,OAAK,qBAAqB;AAC1B,OAAK,UAAU;;CAGjB,8BAA8C;AAC5C,SAAO,KAAK;;;;;;;;;;;CAad,MAAM,yBACJ,uBACiB;EACjB,MAAM,iBAAiB,IAAI;AAC3B,OAAK,cAAc,kBAAkB,yBAAyB,EAAE;AAChE,MAAI,CAAC,KAAK,QAAQ;AAChB,SAAM,KAAK,MAAM;AACjB,QAAK,UAAU,KAAK;;AAEtB,SAAO;;;;;;CAOT,MAAM,4BAA4B,gBAAuC;AACvE,MACE,CAAC,CAAC,KAAK,cAAc,mBACrB,OAAO,KAAK,KAAK,cAAc,CAAC,WAAW,GAC3C;AACA,SAAM,KAAK,OAAO;AAClB,QAAK,UAAU,MAAM;;AAEvB,SAAO,KAAK,cAAc;;;;;;CAO5B,MAAM,kCAAiD;AACrD,QAAM,QAAQ,IACZ,OAAO,KAAK,KAAK,cAAc,CAAC,KAAK,OACnC,KAAK,4BAA4B,GAAG,CACrC,CACF;;;;;;;;;;;CA+BH,eAAyB,SAAoC;AAC3D,OAAK,mBAAmB,QAAQ;AAChC,SAAO,OAAO,KAAK,cAAc,CAAC,SAAS,EAAE,qBAAqB;AAChE,oBAAiB,QAAQ;IACzB;;;;;;CAOJ,oBACE,SACM;AACN,SAAO,OAAO,KAAK,cAAc,CAAC,SAAS,EAAE,0BAA0B;AACrE,yBAAsB,QAAQ;IAC9B;AACF,MAAI,QAAQ,SAAS,0CACnB,MAAK,UAAU,MAAM;;;;;;CAQzB,UAAoB,QAAiB;EACnC,MAAM,eAAe,WAAW,KAAK;AACrC,OAAK,SAAS;AACd,MAAI,aAAc,MAAK,SAAS,KAAK,SAAS"}