{"ast":null,"code":"import _defineProperty from \"@babel/runtime/helpers/defineProperty\";\nimport _classCallCheck from \"@babel/runtime/helpers/classCallCheck\";\nimport _createClass from \"@babel/runtime/helpers/createClass\";\nimport invariant from 'invariant';\nimport NativeEventEmitter from \"react-native-web/dist/exports/NativeEventEmitter\";\nimport Platform from \"react-native-web/dist/exports/Platform\";\nvar nativeEmitterSubscriptionKey = '@@nativeEmitterSubscription@@';\nexport var EventEmitter = function () {\n  function EventEmitter(nativeModule) {\n    _classCallCheck(this, EventEmitter);\n\n    this._listenerCount = 0;\n    this._nativeModule = nativeModule;\n    this._eventEmitter = new NativeEventEmitter(nativeModule);\n  }\n\n  _createClass(EventEmitter, [{\n    key: \"addListener\",\n    value: function addListener(eventName, listener) {\n      var _this = this,\n          _subscription;\n\n      if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.startObserving) {\n        this._nativeModule.startObserving();\n      }\n\n      this._listenerCount++;\n\n      var nativeEmitterSubscription = this._eventEmitter.addListener(eventName, listener);\n\n      var subscription = (_subscription = {}, _defineProperty(_subscription, nativeEmitterSubscriptionKey, nativeEmitterSubscription), _defineProperty(_subscription, \"remove\", function remove() {\n        _this.removeSubscription(subscription);\n      }), _subscription);\n      return subscription;\n    }\n  }, {\n    key: \"removeAllListeners\",\n    value: function removeAllListeners(eventName) {\n      var removedListenerCount = this._eventEmitter.listenerCount ? this._eventEmitter.listenerCount(eventName) : this._eventEmitter.listeners(eventName).length;\n\n      this._eventEmitter.removeAllListeners(eventName);\n\n      this._listenerCount -= removedListenerCount;\n      invariant(this._listenerCount >= 0, \"EventEmitter must have a non-negative number of listeners\");\n\n      if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.stopObserving) {\n        this._nativeModule.stopObserving();\n      }\n    }\n  }, {\n    key: \"removeSubscription\",\n    value: function removeSubscription(subscription) {\n      var nativeEmitterSubscription = subscription[nativeEmitterSubscriptionKey];\n\n      if (!nativeEmitterSubscription) {\n        return;\n      }\n\n      if ('remove' in nativeEmitterSubscription) {\n        nativeEmitterSubscription.remove();\n      } else if ('removeSubscription' in this._eventEmitter) {\n        this._eventEmitter.removeSubscription(nativeEmitterSubscription);\n      }\n\n      this._listenerCount--;\n      delete subscription[nativeEmitterSubscriptionKey];\n\n      subscription.remove = function () {};\n\n      if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.stopObserving) {\n        this._nativeModule.stopObserving();\n      }\n    }\n  }, {\n    key: \"emit\",\n    value: function emit(eventName) {\n      var _this$_eventEmitter;\n\n      for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n        params[_key - 1] = arguments[_key];\n      }\n\n      (_this$_eventEmitter = this._eventEmitter).emit.apply(_this$_eventEmitter, [eventName].concat(params));\n    }\n  }]);\n\n  return EventEmitter;\n}();","map":{"version":3,"sources":["../src/EventEmitter.ts"],"names":[],"mappings":";;;AAAA,OAAO,SAAP,MAAsB,WAAtB;;;AAGA,IAAM,4BAA4B,GAAG,+BAArC;AAiBA,WAAa,YAAb;AAKE,wBAAY,YAAZ,EAAsC;AAAA;;AAAA,SAJtC,cAIsC,GAJrB,CAIqB;AACpC,SAAK,aAAL,GAAqB,YAArB;AACA,SAAK,aAAL,GAAqB,IAAI,kBAAJ,CAAuB,YAAvB,CAArB;AACD;;AARH;AAAA;AAAA,gCAUiB,SAVjB,EAUoC,QAVpC,EAUgE;AAAA;AAAA;;AAC5D,UAAI,CAAC,KAAK,cAAN,IAAwB,QAAQ,CAAC,EAAT,KAAgB,KAAxC,IAAiD,KAAK,aAAL,CAAmB,cAAxE,EAAwF;AACtF,aAAK,aAAL,CAAmB,cAAnB;AACD;;AAED,WAAK,cAAL;;AACA,UAAM,yBAAyB,GAAG,KAAK,aAAL,CAAmB,WAAnB,CAA+B,SAA/B,EAA0C,QAA1C,CAAlC;;AACA,UAAM,YAAY,uDACf,4BADe,EACgB,yBADhB,4CAER,kBAAK;AACX,QAAA,KAAI,CAAC,kBAAL,CAAwB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Aa;AAAA;;AACtC,kCAAK,aAAL,EAAmB,IAAnB,6BAAwB,SAAxB,SAAsC,MAAtC;AACD;AAzEH;;AAAA;AAAA","sourcesContent":["import invariant from 'invariant';\nimport { NativeEventEmitter, Platform } from 'react-native';\n\nconst nativeEmitterSubscriptionKey = '@@nativeEmitterSubscription@@';\n\ntype NativeModule = {\n  startObserving?: () => void;\n  stopObserving?: () => void;\n  addListener: (eventName: string) => void;\n  removeListeners: (count: number) => void;\n};\n\n// @needsAudit\nexport type Subscription = {\n  /**\n   * A method to unsubscribe the listener.\n   */\n  remove: () => void;\n};\n\nexport class EventEmitter {\n  _listenerCount = 0;\n  _nativeModule: NativeModule;\n  _eventEmitter: NativeEventEmitter;\n\n  constructor(nativeModule: NativeModule) {\n    this._nativeModule = nativeModule;\n    this._eventEmitter = new NativeEventEmitter(nativeModule as any);\n  }\n\n  addListener<T>(eventName: string, listener: (event: T) => void): Subscription {\n    if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.startObserving) {\n      this._nativeModule.startObserving();\n    }\n\n    this._listenerCount++;\n    const nativeEmitterSubscription = this._eventEmitter.addListener(eventName, listener);\n    const subscription = {\n      [nativeEmitterSubscriptionKey]: nativeEmitterSubscription,\n      remove: () => {\n        this.removeSubscription(subscription);\n      },\n    };\n    return subscription;\n  }\n\n  removeAllListeners(eventName: string): void {\n    // @ts-ignore: the EventEmitter interface has been changed in react-native@0.64.0\n    const removedListenerCount = this._eventEmitter.listenerCount\n      ? // @ts-ignore: this is available since 0.64\n        this._eventEmitter.listenerCount(eventName)\n      : // @ts-ignore: this is available in older versions\n        this._eventEmitter.listeners(eventName).length;\n    this._eventEmitter.removeAllListeners(eventName);\n    this._listenerCount -= removedListenerCount;\n    invariant(\n      this._listenerCount >= 0,\n      `EventEmitter must have a non-negative number of listeners`\n    );\n\n    if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.stopObserving) {\n      this._nativeModule.stopObserving();\n    }\n  }\n\n  removeSubscription(subscription: Subscription): void {\n    const nativeEmitterSubscription = subscription[nativeEmitterSubscriptionKey];\n    if (!nativeEmitterSubscription) {\n      return;\n    }\n\n    if ('remove' in nativeEmitterSubscription) {\n      // `react-native-web@0.17.1` doesn't support `removeSubscription`\n      nativeEmitterSubscription.remove();\n    } else if ('removeSubscription' in this._eventEmitter) {\n      this._eventEmitter.removeSubscription(nativeEmitterSubscription!);\n    }\n    this._listenerCount--;\n\n    // Ensure that the emitter's internal state remains correct even if `removeSubscription` is\n    // called again with the same subscription\n    delete subscription[nativeEmitterSubscriptionKey];\n\n    // Release closed-over references to the emitter\n    subscription.remove = () => {};\n\n    if (!this._listenerCount && Platform.OS !== 'ios' && this._nativeModule.stopObserving) {\n      this._nativeModule.stopObserving();\n    }\n  }\n\n  emit(eventName: string, ...params: any[]): void {\n    this._eventEmitter.emit(eventName, ...params);\n  }\n}\n"],"sourceRoot":""},"metadata":{},"sourceType":"module"}