react-native#NativeEventEmitter TypeScript Examples

The following examples show how to use react-native#NativeEventEmitter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Iterable.spec.ts    From react-native-sdk with MIT License 6 votes vote down vote up
test("open url when url handler returns false", () => {
  MockLinking.canOpenURL = jest.fn(() => {
    return new Promise(res => { res(true) })
  })
  MockLinking.openURL.mockReset()

  const nativeEmitter = new NativeEventEmitter();
  nativeEmitter.removeAllListeners(EventName.handleUrlCalled)

  const expectedUrl = "https://somewhere.com"
  const config = new IterableConfig()
  config.urlHandler = jest.fn((url: string, _: IterableActionContext) => {
    return false
  })

  Iterable.initialize("apiKey", config)
  const actionDict = { "type": "openUrl" }
  nativeEmitter.emit(EventName.handleUrlCalled, { "url": expectedUrl, "context": { "action": actionDict, "source": "inApp" } });

  return TestHelper.delayed(0, () => {
    expect(config.urlHandler).toBeCalledWith(expectedUrl, expect.any(Object))
    expect(MockLinking.openURL).toBeCalledWith(expectedUrl)
  })
})
Example #2
Source File: Iterable.spec.ts    From react-native-sdk with MIT License 6 votes vote down vote up
test("do not open url when url handler returns false and canOpen is false", () => {
  MockLinking.canOpenURL = jest.fn(() => {
    return new Promise(res => { res(false) })
  })
  MockLinking.openURL.mockReset()

  const nativeEmitter = new NativeEventEmitter();
  nativeEmitter.removeAllListeners(EventName.handleUrlCalled)

  const expectedUrl = "https://somewhere.com"
  const config = new IterableConfig()
  config.urlHandler = jest.fn((url: string, _: IterableActionContext) => {
    return false
  })

  Iterable.initialize("apiKey", config)
  const actionDict = { "type": "openUrl" }
  nativeEmitter.emit(EventName.handleUrlCalled, { "url": expectedUrl, "context": { "action": actionDict, "source": "inApp" } });

  return TestHelper.delayed(0, () => {
    expect(config.urlHandler).toBeCalledWith(expectedUrl, expect.any(Object))
    expect(MockLinking.openURL).not.toBeCalled()
  })
})
Example #3
Source File: Iterable.spec.ts    From react-native-sdk with MIT License 6 votes vote down vote up
test("do not open url when url handler returns true", () => {
  MockLinking.canOpenURL = jest.fn(() => {
    return new Promise(res => { res(true) })
  })
  MockLinking.openURL.mockReset()

  const nativeEmitter = new NativeEventEmitter();
  nativeEmitter.removeAllListeners(EventName.handleUrlCalled)

  const expectedUrl = "https://somewhere.com"
  const config = new IterableConfig()
  config.urlHandler = jest.fn((url: string, _: IterableActionContext) => {
    return true
  })

  Iterable.initialize("apiKey", config)
  const actionDict = { "type": "openUrl" }
  nativeEmitter.emit(EventName.handleUrlCalled, { "url": expectedUrl, "context": { "action": actionDict, "source": "inApp" } });

  return TestHelper.delayed(0, () => {
    expect(MockLinking.openURL).not.toBeCalled()
  })
})
Example #4
Source File: Iterable.spec.ts    From react-native-sdk with MIT License 6 votes vote down vote up
test("custom action handler is called", () => {
  const nativeEmitter = new NativeEventEmitter();
  nativeEmitter.removeAllListeners(EventName.handleCustomActionCalled)

  const actionName = "zeeActionName"
  const actionData = "zeeActionData"
  const config = new IterableConfig()

  config.customActionHandler = jest.fn((action: IterableAction, context: IterableActionContext) => {
    return true
  })

  Iterable.initialize("apiKey", config)
  const actionDict = { "type": actionName, "data": actionData }
  nativeEmitter.emit(EventName.handleCustomActionCalled, { "action": actionDict, "context": { "action": actionDict, "actionSource": IterableActionSource.inApp } });

  return TestHelper.delayed(0, () => {
    const expectedAction = new IterableAction(actionName, actionData)
    const expectedContext = new IterableActionContext(expectedAction, IterableActionSource.inApp)
    expect(config.customActionHandler).toBeCalledWith(expectedAction, expectedContext)
  })
})
Example #5
Source File: IterableInApp.spec.ts    From react-native-sdk with MIT License 6 votes vote down vote up
test("in-app handler is called", () => {
  MockRNIterableAPI.setInAppShowResponse.mockReset()

  const nativeEmitter = new NativeEventEmitter();
  nativeEmitter.removeAllListeners(EventName.handleInAppCalled)

  const config = new IterableConfig()

  config.inAppHandler = jest.fn((message: IterableInAppMessage) => {
    return IterableInAppShowResponse.show
  })

  Iterable.initialize("apiKey", config)
  const messageDict = {
    "messageId": "message1",
    "campaignId": 1234,
    "trigger": { "type": IterableInAppTriggerType.immediate },
    "priorityLevel": 300.5
  }
  nativeEmitter.emit(EventName.handleInAppCalled, messageDict);

  return TestHelper.delayed(0, () => {
    expect(config.inAppHandler)
    const expectedMessage = new IterableInAppMessage("message1", 1234, new IterableInAppTrigger(IterableInAppTriggerType.immediate), undefined, undefined, false, undefined, undefined, false, 300.5)
    expect(config.inAppHandler).toBeCalledWith(expectedMessage)
    expect(MockRNIterableAPI.setInAppShowResponse).toBeCalledWith(IterableInAppShowResponse.show)
  })
})
Example #6
Source File: BLEService.ts    From hamagen-react-native with MIT License 6 votes vote down vote up
registerBLEListeners = () => {
  if (ENABLE_BLE) {
    const eventEmitter = new NativeEventEmitter(SpecialBle);
    eventEmitter.addListener('scanningStatus', (status) => {
      // TODO handle ble event
    });

    eventEmitter.addListener('advertisingStatus', (status) => {
      // TODO handle ble event
    });
  }
}
Example #7
Source File: InAppUpdates.android.ts    From sp-react-native-in-app-updates with MIT License 6 votes vote down vote up
constructor() {
    super();
    this.eventEmitter = new NativeEventEmitter(SpInAppUpdates);
    this.eventEmitter.addListener(
      SpInAppUpdatesOrEmpty?.IN_APP_UPDATE_STATUS_KEY,
      this.onIncomingNativeStatusUpdate
    );
    this.eventEmitter.addListener(
      SpInAppUpdatesOrEmpty?.IN_APP_UPDATE_RESULT_KEY,
      this.onIncomingNativeResult
    );
  }
Example #8
Source File: App.tsx    From leopard with Apache License 2.0 6 votes vote down vote up
async init() {
    try {
      this._leopard = await Leopard.create(
        this._accessKey,
        'leopard_params.pv',
      );
      this._voiceProcessor = VoiceProcessor.getVoiceProcessor(
        512,
        this._leopard.sampleRate,
      );
      this._bufferEmitter = new NativeEventEmitter(BufferEmitter);
      this._bufferListener = this._bufferEmitter.addListener(
        BufferEmitter.BUFFER_EMITTER_KEY,
        async (buffer: number[]) => {
          if (this.state.appState !== UIState.ERROR) {
            try {
              await this._recorder.writeSamples(buffer);
            } catch {
              this.handleError('Failed to write to wav file.');
            }
          }
        },
      );
      this.setState({
        appState: UIState.INIT,
      });
    } catch (err: any) {
      this.handleError(err);
    }
  }
Example #9
Source File: contact-tracing-provider.tsx    From SQUID with MIT License 5 votes vote down vote up
eventEmitter = new NativeEventEmitter(NativeModules.ContactTracerModule)
Example #10
Source File: InAppUpdatesBase.ts    From sp-react-native-in-app-updates with MIT License 5 votes vote down vote up
protected eventEmitter?: NativeEventEmitter;
Example #11
Source File: EventEmitter.d.ts    From nlw2-proffy with MIT License 5 votes vote down vote up
_eventEmitter: NativeEventEmitter;
Example #12
Source File: EventEmitter.ts    From nlw2-proffy with MIT License 5 votes vote down vote up
constructor(nativeModule: NativeModule) {
    this._nativeModule = nativeModule;
    this._eventEmitter = new NativeEventEmitter(nativeModule as any);
  }
Example #13
Source File: EventEmitter.ts    From nlw2-proffy with MIT License 5 votes vote down vote up
_eventEmitter: NativeEventEmitter;
Example #14
Source File: index.tsx    From nlw2-proffy with MIT License 5 votes vote down vote up
nativeEventEmitter = new NativeEventEmitter(NativeAppearance)
Example #15
Source File: index.tsx    From react-native-network-client with Apache License 2.0 5 votes vote down vote up
Emitter = new NativeEventEmitter(NativeAPIClient)
Example #16
Source File: index.tsx    From react-native-network-client with Apache License 2.0 5 votes vote down vote up
Emitter = new NativeEventEmitter(NativeWebSocketClient)
Example #17
Source File: useApplePay.tsx    From stripe-react-native with MIT License 5 votes vote down vote up
eventEmitter = new NativeEventEmitter(NativeModules.StripeSdk)
Example #18
Source File: grpc.ts    From react-native-lightning with MIT License 5 votes vote down vote up
constructor(lndModule: NativeModulesStatic) {
		this.lnd = lndModule;
		// TODO try expose the iOS event emitter in the same native module.
		this.lndEvent =
			Platform.OS === 'ios'
				? new NativeEventEmitter(NativeModules.LightningEventEmitter)
				: new NativeEventEmitter(NativeModules.ReactNativeLightning);
	}
Example #19
Source File: grpc.ts    From react-native-lightning with MIT License 5 votes vote down vote up
readonly lndEvent: NativeEventEmitter;
Example #20
Source File: FBAccessToken.ts    From react-native-fbsdk-next with MIT License 5 votes vote down vote up
eventEmitter = new NativeEventEmitter(AccessToken)
Example #21
Source File: module.ts    From react-native-keyboard-area with MIT License 5 votes vote down vote up
eventEmitter = new NativeEventEmitter(KBModule)
Example #22
Source File: ContactTracer.tsx    From SQUID with MIT License 5 votes vote down vote up
eventEmitter = new NativeEventEmitter(NativeModules.ContactTracerModule)
Example #23
Source File: native.ts    From react-native-file-access with MIT License 5 votes vote down vote up
FileAccessEventEmitter = new NativeEventEmitter(
  NativeModules.RNFileAccess
)
Example #24
Source File: index.tsx    From react-native-location-enabler with MIT License 5 votes vote down vote up
locationEnabler = new NativeEventEmitter(LocationEnabler)
Example #25
Source File: Background.ts    From BleInTheBackground-iOS with Apache License 2.0 5 votes vote down vote up
PLXEventEmitter = new NativeEventEmitter(PLXBackground)
Example #26
Source File: App.tsx    From leopard with Apache License 2.0 5 votes vote down vote up
_bufferEmitter?: NativeEventEmitter;
Example #27
Source File: debug.tsx    From protect-scotland with Apache License 2.0 5 votes vote down vote up
emitter = new NativeEventEmitter(ExposureNotification)
Example #28
Source File: index.ts    From react-native-twilio-phone with MIT License 5 votes vote down vote up
twilioPhoneEmitter = new NativeEventEmitter(NativeModules.TwilioPhone)
Example #29
Source File: native-types.ts    From react-native-healthkit with MIT License 5 votes vote down vote up
EventEmitter = new NativeEventEmitter(
  NativeModules.ReactNativeHealthkit
) as HealthkitEventEmitter