UNPKG

1.46 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 * @flow
9 */
10
11'use strict';
12
13const EmitterSubscription = require('EmitterSubscription');
14const EventEmitter = require('EventEmitter');
15
16const invariant = require('invariant');
17
18class MissingNativeEventEmitterShim extends EventEmitter {
19 isAvailable: boolean = false;
20 _nativeModuleName: string;
21 _nativeEventEmitterName: string;
22
23 constructor(nativeModuleName: string, nativeEventEmitterName: string) {
24 super(null);
25 this._nativeModuleName = nativeModuleName;
26 this._nativeEventEmitterName = nativeEventEmitterName;
27 }
28
29 throwMissingNativeModule() {
30 invariant(
31 false,
32 `Cannot use '${this._nativeEventEmitterName}' module when ` +
33 `native '${this._nativeModuleName}' is not included in the build. ` +
34 `Either include it, or check '${
35 this._nativeEventEmitterName
36 }'.isAvailable ` +
37 'before calling any methods.',
38 );
39 }
40
41 // EventEmitter
42 addListener(eventType: string, listener: Function, context: ?Object) {
43 this.throwMissingNativeModule();
44 }
45
46 removeAllListeners(eventType: string) {
47 this.throwMissingNativeModule();
48 }
49
50 removeSubscription(subscription: EmitterSubscription) {
51 this.throwMissingNativeModule();
52 }
53}
54
55module.exports = MissingNativeEventEmitterShim;