{"version":3,"file":"consumable.mjs","names":[],"sources":["../../../../../../../@warlock.js/herald/src/decorators/consumable.ts"],"sourcesContent":["import { brokerRegistry } from \"../communicators\";\nimport { type EventConsumerClass } from \"../message-managers/types\";\n\nexport type ConsumableOptions = {\n  broker?: string;\n};\n\nexport const pendingSubscribers = new Set<{\n  Consumer: EventConsumerClass;\n  options?: ConsumableOptions;\n}>();\n\n/**\n * Register the consumer to the broker\n */\nexport function Consumable(options?: ConsumableOptions) {\n  return function (target: EventConsumerClass) {\n    const brokerName = options?.broker;\n\n    try {\n      const currentBroker = brokerRegistry.get(brokerName);\n\n      // if broker is connected, subscribe the consumer\n      if (currentBroker?.isConnected) {\n        currentBroker.subscribe(target);\n      } else {\n        pendingSubscribers.add({ Consumer: target, options });\n      }\n    } catch {\n      // mostly it will be an error that broker is not registered yet\n      // then add it to the pending subscribers\n      pendingSubscribers.add({ Consumer: target, options });\n    }\n  };\n}\n\n// Register pending consumers on broker's connection is done\nbrokerRegistry.on(\"connected\", (broker) => {\n  for (const { Consumer, options } of pendingSubscribers) {\n    if (options?.broker && broker.name !== options.broker) {\n      continue;\n    }\n\n    broker.subscribe(Consumer);\n  }\n});\n"],"mappings":";;;;AAOA,MAAa,qCAAqB,IAAI,IAGnC;;;;AAKH,SAAgB,WAAW,SAA6B;CACtD,OAAO,SAAU,QAA4B;EAC3C,MAAM,aAAa,SAAS;EAE5B,IAAI;GACF,MAAM,gBAAgB,eAAe,IAAI,UAAU;GAGnD,IAAI,eAAe,aACjB,cAAc,UAAU,MAAM;QAE9B,mBAAmB,IAAI;IAAE,UAAU;IAAQ;GAAQ,CAAC;EAExD,QAAQ;GAGN,mBAAmB,IAAI;IAAE,UAAU;IAAQ;GAAQ,CAAC;EACtD;CACF;AACF;AAGA,eAAe,GAAG,cAAc,WAAW;CACzC,KAAK,MAAM,EAAE,UAAU,aAAa,oBAAoB;EACtD,IAAI,SAAS,UAAU,OAAO,SAAS,QAAQ,QAC7C;EAGF,OAAO,UAAU,QAAQ;CAC3B;AACF,CAAC"}