typescript - RxJS fromEventPattern com eventos do React Native
Estou a tentar filtrar eventos nativos no React Native com RxJS. Estou a usar o react-native-ble-manager, que expõe um EventEmitter. Basicamente, sem RxJS, estou a fazer o seguinte (apenas o código relevante é mostrado aqui):
import React from 'react';
import RX from 'reactxp';
import { AppState, Platform, NativeModules, NativeEventEmitter, EmitterSubscription, PermissionsAndroid, ListView } from 'react-native';
import BleManager, { Peripheral } from 'react-native-ble-manager';
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
export class App extends RX.Component<IAppProps, IAppState> {
private handlerDiscover!: EmitterSubscription;
private handlerStop!:EmitterSubscription;
private handlerDisconnect!:EmitterSubscription;
private handlerUpdate!:EmitterSubscription;
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
BleManager.start({ showAlert: false });
this.handlerDiscover = bleManagerEmitter.addListener('BleManagerDiscoverPeripheral', this.handleDiscoverPeripheral);
this.handlerStop = bleManagerEmitter.addListener('BleManagerStopScan', this.handleStopScan);
this.handlerDisconnect = bleManagerEmitter.addListener('BleManagerDisconnectPeripheral', this.handleDisconnectedPeripheral);
this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', this.handleUpdateValueForCharacteristic);
}
componentWillUnmount() {
this.handlerDiscover.remove();
this.handlerStop.remove();
this.handlerDisconnect.remove();
}
handleUpdateValueForCharacteristic = (data:any) => {
console.log('Received data from ' + data.peripheral + ' characteristic ' + data.characteristic + ' length ' + data.value.length + ' values ' + data.value);
}
}
Uma vez que estou ligado ao meu periférico BLE e comecei a recuperação de dados, a função de seta handleUpdateValueForCharacteristic é chamada com os dados. Isso está a funcionar bem.
Agora gostava de usar RxJS para filtrar eventos, porque poderia receber até 200 eventos por segundo, mas estou interessado apenas em 1/4 deles.
Simplesmente não consigo compreender o uso de fromEventPattern.
O que eu tentei até agora é o seguinte:
Adicionada alguma função manipuladora na classe:
private addHandler:Function = (handler:NodeEventHandler):any => {
this.handlerUpdate = bleManagerEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', handler);
return this.handlerUpdate;
}
private removeHandler:Function = (handler:EmitterSubscription, _signal?:any) => {
handler.remove();
}
Em componentDidMount():
this.emitter = fromEventPattern<any>(this.addHandler(this.handleUpdateValueForCharacteristic), this.removeHandler(this.handlerUpdate));
this.emitter.subscribe(...);
Mas a subscrição falha com o seguinte erro:
addHandler não é uma função.