react-native-acoustic-connect-beta
Version:
BETA: React native plugin for Acoustic Connect
281 lines (252 loc) • 8.45 kB
text/typescript
/********************************************************************************************
* Copyright (C) 2025 Acoustic, L.P. All rights reserved.
*
* NOTICE: This file contains material that is confidential and proprietary to
* Acoustic, L.P. and/or other developers. No license is granted under any intellectual or
* industrial property rights of Acoustic, L.P. except as may be provided in an agreement with
* Acoustic, L.P. Any unauthorized copying or distribution of content from this file is
* prohibited.
********************************************************************************************/
// @ts-ignore
import MessageQueue from "react-native/Libraries/BatchedBridge/MessageQueue.js";
import { Platform } from "react-native";
import KeyboardListener from "./utils/KeyboardListener";
import AcousticConnectRN from './index';
// @ts-ignore
global.ErrorUtils.setGlobalHandler((e: any, _isFatal: boolean) => {
AcousticConnectRN.logExceptionEvent(
JSON.stringify(e),
JSON.stringify(e),
true
);
});
class TLTRN {
static currentScreen =
"***initialCurrentScreen not set in ConnectLogger constructor***";
static lastJSBridgeMessageTime = 0;
static totalRenderTime = 0;
static messageRenderTime = 0;
static countMsgs = 0;
static messageConsole = 0;
static lastMessageConsole = 0;
static isLoggingData = 0;
static displayDebug = false;
static myTimer = {
handle: null as NodeJS.Timeout | null,
started: 0,
time: 1000,
/**
* @type Class
*/
startTimer: function () {
this.started = 1;
this.handle = setInterval(TLTRN.checkTime, this.time);
},
/**
* @type Class
*/
stopTimer: function () {
if (this.handle) {
clearInterval(this.handle);
this.handle = null;
this.started = 0;
}
},
};
static init = (initialCurrentScreen: String, showDebugConsoleMessages: boolean) => {
TLTRN.currentScreen =
initialCurrentScreen === undefined
// @ts-ignore:
? currentScreenMsg
: initialCurrentScreen;
MessageQueue.spy(TLTRN.listenToBridge);
TLTRN.displayDebug = showDebugConsoleMessages === undefined ? false : showDebugConsoleMessages;
};
static interceptKeyboardEvents = (enable: boolean) => {
keyListener.interceptKeyboardEvents(enable);
};
static logScreenViewPageName = (name: string | undefined) => {
let result = false
try {
result = AcousticConnectRN.setCurrentScreenName(name || '');
} catch (error: Error | any) {
console.log('LogScreenViewPageName error: ', error.message);
}
return result;
};
static logScreenViewContextLoad = (name: string, prevName: string) => {
let result = false
try {
result = AcousticConnectRN.logScreenViewContextLoad(name, prevName);
} catch (error: Error | any) {
console.log('LogScreenViewContextLoad error: ', error.message);
}
return result;
};
static logScreenLayout = (name: string | undefined) => {
TLTRN.currentScreen = name || '';
let result = false
try {
result = AcousticConnectRN.logScreenLayout(TLTRN.currentScreen, 0);
} catch (error: Error | any) {
console.log('LogScreenLayout error: ', error.message);
}
return result;
};
static logClickEvent = async (event: any) => {
let result = false
if(!event.target || !event.target._nativeTag || typeof event.target._nativeTag === 'undefined'){ return result; }
const events = event._dispatchInstances.filter((node: any) => {
return node.memoizedProps && node.memoizedProps.id;
});
const id = (events.length) ? events[0].memoizedProps.id : '';
let target = event.target._nativeTag;
let controlId = event._targetInst.memoizedProps.id !== undefined ? event._targetInst.memoizedProps.id : id;
let ariaLabel = event._targetInst.memoizedProps.accessible !== undefined ? event._targetInst.memoizedProps.accessibilityLabel : '';
try {
if (Platform.OS === 'ios') {
result = AcousticConnectRN.logClickEvent(target, controlId);
} else if (Platform.OS === 'android') {
result = AcousticConnectRN.logClickEvent(target, controlId);
}
} catch(error: Error | any){
console.log('LogClickEvent error: ', error.message);
}
return result;
};
static logTextChangeEvent = async (target: number, controlId: string, text: string, _ariaLabel: string) => {
let result = false
try {
if (Platform.OS === 'ios') {
result = AcousticConnectRN.logTextChangeEvent(target, controlId, text);
} else if (Platform.OS === 'android') {
AcousticConnectRN.logTextChangeEvent(target, controlId, text);
}
} catch (error: Error | any) {
console.log('LogTextChangeEvent error: ', error.message);
}
return result;
};
static logCustomEvent = async (eventName: string, values: Record<string, string | number | boolean>, level: number) => {
let result = false
try {
result = AcousticConnectRN.logCustomEvent(eventName, values, level);
} catch (error: Error | any) {
console.log('LogCustomEvent error: ', error.message);
}
return result;
};
static listenToBridge = (message: any) => {
if (TLTRN.displayDebug) {
console.log(
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&:isLoggingData:" +
TLTRN.isLoggingData
);
}
if (TLTRN.isLoggingData == 1) {
return;
}
var now = new Date().getTime();
if (TLTRN.lastJSBridgeMessageTime === 0) {
TLTRN.lastJSBridgeMessageTime = now;
}
if (TLTRN.messageConsole > 0) {
TLTRN.lastMessageConsole = TLTRN.messageConsole;
}
TLTRN.messageConsole = now;
TLTRN.countMsgs++;
if (TLTRN.myTimer.started === 0) {
TLTRN.myTimer.startTimer();
}
TLTRN.messageRenderTime = now - TLTRN.lastJSBridgeMessageTime;
TLTRN.totalRenderTime = TLTRN.totalRenderTime + TLTRN.messageRenderTime;
if (TLTRN.displayDebug) {
console.log(
"*after:countMsgs: " +
TLTRN.countMsgs +
":tTotal:" +
TLTRN.totalRenderTime +
":messageDuration:" +
TLTRN.messageRenderTime
);
}
TLTRN.lastJSBridgeMessageTime = now;
const from = message.type === 0 ? "N->JS" : "JS->N";
const data =
from +
" : " +
message.module +
"." +
message.method +
"(" +
JSON.stringify(message.args) +
")";
if (TLTRN.displayDebug) {
if (message.module !== "UIManager") {
console.log("Message", message);
}
console.log("Message data:", data);
}
if (message.module === "ExceptionsManager") {
AcousticConnectRN.logExceptionEvent(
message.args[0],
JSON.stringify(message.args[1]),
true
);
}
};
static checkTime = () => {
var now = new Date().getTime();
if (TLTRN.displayDebug) {
console.log(
"======>Done compare total:" +
TLTRN.totalRenderTime +
" :lastMessageConsole:" +
TLTRN.lastMessageConsole +
":now:" +
now +
":diff:" +
(now - TLTRN.lastMessageConsole) +
":" +
(now - TLTRN.lastMessageConsole > 50)
);
}
if (TLTRN.lastMessageConsole == 0) {
return;
}
if (now - TLTRN.lastMessageConsole > 20) {
if (TLTRN.displayDebug) {
console.log("======>Done capture:" + new Date().getTime());
}
TLTRN.messageConsole = 0;
TLTRN.lastMessageConsole = 0;
TLTRN.myTimer.stopTimer();
this.logTeal();
}
};
static logTeal = async () => {
try {
TLTRN.isLoggingData = 1;
var res = await AcousticConnectRN.logScreenLayout(TLTRN.currentScreen, -1);
var dict = { ReactLayoutTime: TLTRN.totalRenderTime };
var result = await AcousticConnectRN.logCustomEvent("ReactPlugin", dict, 1);
if (TLTRN.displayDebug) {
console.log(
"======>logTeal:" +
TLTRN.currentScreen +
":" +
res +
":time:" +
TLTRN.totalRenderTime
);
}
TLTRN.isLoggingData = 0;
TLTRN.lastJSBridgeMessageTime = 0;
TLTRN.totalRenderTime = 0;
} catch (e) {
console.error(e);
}
};
}
const keyListener = KeyboardListener._instance(TLTRN);
export default TLTRN;