SDA-4291 (Add delay for sending analytics events) (#1981)

* SDA-4291 - Add delay for sending analytics events

* SDA-4291 - Fix uts
This commit is contained in:
Kiran Niranjan 2023-10-17 13:02:54 +05:30 committed by GitHub
parent 3253e4ec56
commit 3ecb3da1f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -315,3 +315,37 @@ export const isUrl = (str: string): boolean => {
return false;
}
};
/**
* Queues and delays function call with a given delay
*/
export class DelayedFunctionQueue {
private queue: Array<(...args: any[]) => void> = [];
private timer: NodeJS.Timeout | null = null;
constructor(private delay: number = 100) {}
/**
* Add a function to the queue
* @param func
* @param args
*/
public add(func: (...args: any[]) => void, ...args: any[]): void {
const boundFunc = () => func(...args);
this.queue.push(boundFunc);
if (!this.timer) {
this.timer = setInterval(() => {
const func = this.queue.shift();
if (func) {
func();
} else {
if (this.timer) {
clearInterval(this.timer);
}
this.timer = null;
}
}, this.delay);
}
}
}

View File

@ -34,7 +34,7 @@ import {
PhoneNumberProtocol,
} from '../common/api-interface';
import { i18n, LocaleType } from '../common/i18n-preload';
import { throttle } from '../common/utils';
import { DelayedFunctionQueue, throttle } from '../common/utils';
import { getSource } from './desktop-capturer';
import SSFNotificationHandler from './notification-ssf-handler';
import { ScreenSnippetBcHandler } from './screen-snippet-bc-handler';
@ -45,6 +45,7 @@ const MAIN_WINDOW_NAME = 'main';
let isAltKey: boolean = false;
let isMenuOpen: boolean = false;
let pendingAnalytics: object[] = [];
const analyticsDelayedFuncQueue = new DelayedFunctionQueue(100);
export interface ILocalObject {
ipcRenderer;
@ -366,7 +367,7 @@ export class SSFApi {
});
// Invoke all pending analytic calls.
for (const data of pendingAnalytics) {
analyticsEventHandler(data);
analyticsDelayedFuncQueue.add(analyticsEventHandler, data);
}
// Clear the pending data.
pendingAnalytics = [];
@ -1167,7 +1168,7 @@ local.ipcRenderer.on('protocol-action', (_event, arg: string) => {
local.ipcRenderer.on('analytics-callback', (_event, arg: object) => {
if (typeof local.analyticsEventHandler === 'function' && arg) {
local.analyticsEventHandler(arg);
analyticsDelayedFuncQueue.add(local.analyticsEventHandler, arg);
} else if (arg) {
pendingAnalytics.push(arg);
}