import { Platform, NativeModules, NativeEventEmitter, DeviceEventEmitter, DeviceEventEmitterStatic, } from 'react-native'; import NativeComplyCubeModule from './NativeComplyCubeModule'; import Ajv from 'ajv'; import settingsSchema from './verificationSchema'; import betterAjvErrors from 'better-ajv-errors'; class ComplyCubeRN { complycube: any; emiter: NativeEventEmitter | DeviceEventEmitterStatic; errors: any[] = []; private resultSubs: Array<{ remove: () => void }> = []; private customSubs: Array<{ remove: () => void }> = []; constructor() { const legacyIOS = NativeModules['ComplyCubeRNSDK']; const legacyAndroid = NativeModules['ComplyCubeModule']; const turboModule = NativeComplyCubeModule; const ComplyCubeRNSDK = Platform.OS === 'ios' ? legacyIOS || turboModule || legacyAndroid : turboModule || legacyAndroid || legacyIOS; const eventSource = Platform.OS === 'ios' ? legacyIOS || ComplyCubeRNSDK : ComplyCubeRNSDK; this.emiter = Platform.OS == 'ios' ? new NativeEventEmitter(eventSource as any) : DeviceEventEmitter; this.complycube = ComplyCubeRNSDK; } _validateSettingsBySchema(settings: any) { this.errors = []; const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, }); try { const valid = ajv.validate(settingsSchema, settings); if (!valid) { const errors = ajv.errors || []; const output = betterAjvErrors(settingsSchema, settings, errors, { format: 'cli', }); this.onError(output); return false; } return this; } catch (e: any) { return false; } } setSettings(settings: any) { if (this.complycube == null) return false; try { if (this._validateSettingsBySchema(settings)) { this.complycube.setSettings(settings).then((x: any) => { console.log('Results:', x); }); } else { return false; } } catch (e: any) { console.log('Error during setting settings', e.description); } return this; } _wrapHandler(handler: any, isCustomEvent = false) { return async (data: any) => { let _data = data; if (Platform.OS === 'android' && typeof data === 'string') { try { _data = JSON.parse(data); } catch { _data = data; } } handler(_data); if (!isCustomEvent) { this.removeResultHandlers(); } }; } private addSub(sub: { remove: () => void } | undefined, isCustom: boolean) { if (!sub || typeof sub.remove !== 'function') return; if (isCustom) this.customSubs.push(sub); else this.resultSubs.push(sub); } private removeSubs(list: Array<{ remove: () => void }>) { list.forEach(s => { try { s.remove(); } catch {} }); list.length = 0; } removeResultHandlers() { this.removeSubs(this.resultSubs); } removeCustomHandlers() { this.removeSubs(this.customSubs); } removeHandlers() { this.removeResultHandlers(); this.removeCustomHandlers(); } addHandlers( successHandler = () => {}, errorHandler = () => {}, cancelHandler = () => {}, customEventHandler = () => {} ) { if (!this.emiter) return; const emitter = this.emiter; this.addSub(emitter.addListener( 'ComplyCubeCustomEvent', this._wrapHandler(customEventHandler, true) ), true); this.addSub(emitter.addListener( 'ComplyCubeEvent', this._wrapHandler(customEventHandler, true) ), true); this.addSub(emitter.addListener( 'ComplyCubeSuccess', this._wrapHandler(successHandler) ), false); this.addSub(emitter.addListener( 'ComplyCubeError', this._wrapHandler(errorHandler) ), false); this.addSub(emitter.addListener( 'ComplyCubeCancel', this._wrapHandler(cancelHandler) ), false); return this; } mount() { if (this.complycube == null) return; this.complycube.mount(); } onError(error_message: any) { this.errors = [ { message: error_message, }, ]; this.emiter.emit('ComplyCubeError', { ...this.errors[0] }); throw this.errors[0]; } } export default ComplyCubeRN;