{"version":3,"file":"createMessageBus.cjs","names":["createMessageBus","subscribers","Map","publish","message","Array","from","values","forEach","subscriber","subscribe","subscriberId","Symbol","set","delete","exports"],"sources":["../../../src/tools/createMessageBus.ts"],"sourcesContent":["/*!\n * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nexport type MessageHandler<Message> = (message: Message) => void;\n\nexport type PublishMessage<Message> = MessageHandler<Message>;\n\nexport type UnsubscribeMessageSubscription = () => void;\n\nexport type MessageSubscription<Message> = (\n  subscriber: MessageHandler<Message>,\n) => UnsubscribeMessageSubscription;\n\nexport type MessageBus<Message> = {\n  publish: PublishMessage<Message>;\n  subscribe: MessageSubscription<Message>;\n};\n\n/**\n * Create a self-contained message bus that allows you to subscribe to events published by the publisher.\n */\nexport const createMessageBus = <Message = void>(): MessageBus<Message> => {\n  const subscribers = new Map<symbol, MessageHandler<Message>>();\n\n  const publish: PublishMessage<Message> = (message) => {\n    Array.from(subscribers.values()).forEach((subscriber) => {\n      subscriber(message);\n    });\n  };\n\n  const subscribe: MessageSubscription<Message> = (subscriber) => {\n    const subscriberId = Symbol();\n    subscribers.set(subscriberId, subscriber);\n\n    return () => {\n      subscribers.delete(subscriberId);\n    };\n  };\n\n  return {\n    publish,\n    subscribe,\n  };\n};\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAoBO,MAAMA,gBAAgB,GAAGA,CAAA,KAA2C;EACzE,MAAMC,WAAW,GAAG,IAAIC,GAAG,CAAkC,CAAC;EAE9D,MAAMC,OAAgC,GAAIC,OAAO,IAAK;IACpDC,KAAK,CAACC,IAAI,CAACL,WAAW,CAACM,MAAM,CAAC,CAAC,CAAC,CAACC,OAAO,CAAEC,UAAU,IAAK;MACvDA,UAAU,CAACL,OAAO,CAAC;IACrB,CAAC,CAAC;EACJ,CAAC;EAED,MAAMM,SAAuC,GAAID,UAAU,IAAK;IAC9D,MAAME,YAAY,GAAGC,MAAM,CAAC,CAAC;IAC7BX,WAAW,CAACY,GAAG,CAACF,YAAY,EAAEF,UAAU,CAAC;IAEzC,OAAO,MAAM;MACXR,WAAW,CAACa,MAAM,CAACH,YAAY,CAAC;IAClC,CAAC;EACH,CAAC;EAED,OAAO;IACLR,OAAO;IACPO;EACF,CAAC;AACH,CAAC;AAACK,OAAA,CAAAf,gBAAA,GAAAA,gBAAA","ignoreList":[]}