Replace setInterval with chrome alarms for MetaMetrics FinalizeEventFragment (#16003)

feature/default_network_editable
Niranjana Binoy 2 years ago committed by GitHub
parent 46d970e362
commit 29c2b136b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      app/scripts/controllers/metametrics.js
  2. 12
      app/scripts/lib/util.js
  3. 2
      shared/constants/alarms.js

@ -18,6 +18,9 @@ import {
TRAITS, TRAITS,
} from '../../../shared/constants/metametrics'; } from '../../../shared/constants/metametrics';
import { SECOND } from '../../../shared/constants/time'; import { SECOND } from '../../../shared/constants/time';
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
import { METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM } from '../../../shared/constants/alarms';
import { checkAlarmExists } from '../lib/util';
const EXTENSION_UNINSTALL_URL = 'https://metamask.io/uninstalled'; const EXTENSION_UNINSTALL_URL = 'https://metamask.io/uninstalled';
@ -144,7 +147,41 @@ export default class MetaMetricsController {
// within the fragment's timeout window. When creating a new event fragment // within the fragment's timeout window. When creating a new event fragment
// a timeout can be specified that will cause an abandoned event to be // a timeout can be specified that will cause an abandoned event to be
// tracked if the event isn't progressed within that amount of time. // tracked if the event isn't progressed within that amount of time.
if (isManifestV3) {
/* eslint-disable no-undef */
chrome.alarms.getAll((alarms) => {
const hasAlarm = checkAlarmExists(
alarms,
METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM,
);
if (!hasAlarm) {
chrome.alarms.create(METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM, {
delayInMinutes: 1,
periodInMinutes: 1,
});
}
});
chrome.alarms.onAlarm.addListener(() => {
chrome.alarms.getAll((alarms) => {
const hasAlarm = checkAlarmExists(
alarms,
METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM,
);
if (hasAlarm) {
this.finalizeAbandonedFragments();
}
});
});
} else {
setInterval(() => { setInterval(() => {
this.finalizeAbandonedFragments();
}, SECOND * 30);
}
}
finalizeAbandonedFragments() {
Object.values(this.store.getState().fragments).forEach((fragment) => { Object.values(this.store.getState().fragments).forEach((fragment) => {
if ( if (
fragment.timeout && fragment.timeout &&
@ -153,7 +190,6 @@ export default class MetaMetricsController {
this.finalizeEventFragment(fragment.id, { abandoned: true }); this.finalizeEventFragment(fragment.id, { abandoned: true });
} }
}); });
}, SECOND * 30);
} }
generateMetaMetricsId() { generateMetaMetricsId() {

@ -152,6 +152,17 @@ function getChainType(chainId) {
return 'custom'; return 'custom';
} }
/**
* Checks if the alarmname exists in the list
*
* @param {Array} alarmList
* @param alarmName
* @returns
*/
function checkAlarmExists(alarmList, alarmName) {
return alarmList.some((alarm) => alarm.name === alarmName);
}
export { export {
getPlatform, getPlatform,
getEnvironmentType, getEnvironmentType,
@ -161,4 +172,5 @@ export {
addHexPrefix, addHexPrefix,
bnToHex, bnToHex,
getChainType, getChainType,
checkAlarmExists,
}; };

@ -1 +1,3 @@
export const AUTO_LOCK_TIMEOUT_ALARM = 'AUTO_LOCK_TIMEOUT_ALARM'; export const AUTO_LOCK_TIMEOUT_ALARM = 'AUTO_LOCK_TIMEOUT_ALARM';
export const METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM =
'METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM';

Loading…
Cancel
Save