{"version":3,"file":"ReEmitter.js","names":["ReEmitter","constructor","target","_defineProperty2","default","Map","reEmit","source","eventNames","reEmittersByEvent","reEmitters","get","set","eventName","forSource","args","listenerCount","emit","on","stopReEmitting","off","delete","size","exports","TypedReEmitter"],"sources":["../src/ReEmitter.ts"],"sourcesContent":["/*\nCopyright 2015, 2016 OpenMarket Ltd\nCopyright 2017 Vector Creations Ltd\nCopyright 2017 New Vector Ltd\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n// eslint-disable-next-line no-restricted-imports\nimport { EventEmitter } from \"events\";\n\nimport { ListenerMap, TypedEventEmitter } from \"./models/typed-event-emitter\";\n\nexport class ReEmitter {\n public constructor(private readonly target: EventEmitter) {}\n\n // Map from emitter to event name to re-emitter\n private reEmitters = new Map void>>();\n\n public reEmit(source: EventEmitter, eventNames: string[]): void {\n let reEmittersByEvent = this.reEmitters.get(source);\n if (!reEmittersByEvent) {\n reEmittersByEvent = new Map();\n this.reEmitters.set(source, reEmittersByEvent);\n }\n\n for (const eventName of eventNames) {\n // We include the source as the last argument for event handlers which may need it,\n // such as read receipt listeners on the client class which won't have the context\n // of the room.\n const forSource = (...args: any[]): void => {\n // EventEmitter special cases 'error' to make the emit function throw if no\n // handler is attached, which sort of makes sense for making sure that something\n // handles an error, but for re-emitting, there could be a listener on the original\n // source object so the test doesn't really work. We *could* try to replicate the\n // same logic and throw if there is no listener on either the source or the target,\n // but this behaviour is fairly undesireable for us anyway: the main place we throw\n // 'error' events is for calls, where error events are usually emitted some time\n // later by a different part of the code where 'emit' throwing because the app hasn't\n // added an error handler isn't terribly helpful. (A better fix in retrospect may\n // have been to just avoid using the event name 'error', but backwards compat...)\n if (eventName === \"error\" && this.target.listenerCount(\"error\") === 0) return;\n this.target.emit(eventName, ...args, source);\n };\n source.on(eventName, forSource);\n reEmittersByEvent.set(eventName, forSource);\n }\n }\n\n public stopReEmitting(source: EventEmitter, eventNames: string[]): void {\n const reEmittersByEvent = this.reEmitters.get(source);\n if (!reEmittersByEvent) return; // We were never re-emitting these events in the first place\n\n for (const eventName of eventNames) {\n source.off(eventName, reEmittersByEvent.get(eventName)!);\n reEmittersByEvent.delete(eventName);\n }\n\n if (reEmittersByEvent.size === 0) this.reEmitters.delete(source);\n }\n}\n\nexport class TypedReEmitter> extends ReEmitter {\n public constructor(target: TypedEventEmitter) {\n super(target);\n }\n\n public reEmit(\n source: TypedEventEmitter,\n eventNames: T[],\n ): void {\n super.reEmit(source, eventNames);\n }\n\n public stopReEmitting(\n source: TypedEventEmitter,\n eventNames: T[],\n ): void {\n super.stopReEmitting(source, eventNames);\n }\n}\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AAKO,MAAMA,SAAS,CAAC;EACZC,WAAWA,CAAkBC,MAAoB,EAAE;IAAA,KAAtBA,MAAoB,GAApBA,MAAoB;IAAA,IAAAC,gBAAA,CAAAC,OAAA,sBAGnC,IAAIC,GAAG,EAAuD;EAHxB;;EAE3D;;EAGOC,MAAMA,CAACC,MAAoB,EAAEC,UAAoB,EAAQ;IAC5D,IAAIC,iBAAiB,GAAG,IAAI,CAACC,UAAU,CAACC,GAAG,CAACJ,MAAM,CAAC;IACnD,IAAI,CAACE,iBAAiB,EAAE;MACpBA,iBAAiB,GAAG,IAAIJ,GAAG,EAAE;MAC7B,IAAI,CAACK,UAAU,CAACE,GAAG,CAACL,MAAM,EAAEE,iBAAiB,CAAC;IAClD;IAEA,KAAK,MAAMI,SAAS,IAAIL,UAAU,EAAE;MAChC;MACA;MACA;MACA,MAAMM,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