Skip to content

Commit

Permalink
Replace setInterval with chrome alarms for MetaMetrics FinalizeEventF…
Browse files Browse the repository at this point in the history
…ragment (#16003)
  • Loading branch information
NiranjanaBinoy authored Oct 4, 2022
1 parent 46d970e commit 29c2b13
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
52 changes: 44 additions & 8 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
TRAITS,
} from '../../../shared/constants/metametrics';
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';

Expand Down Expand Up @@ -144,16 +147,49 @@ export default class MetaMetricsController {
// 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
// tracked if the event isn't progressed within that amount of time.
setInterval(() => {
Object.values(this.store.getState().fragments).forEach((fragment) => {
if (
fragment.timeout &&
Date.now() - fragment.lastUpdated / 1000 > fragment.timeout
) {
this.finalizeEventFragment(fragment.id, { abandoned: true });
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,
});
}
});
}, SECOND * 30);
chrome.alarms.onAlarm.addListener(() => {
chrome.alarms.getAll((alarms) => {
const hasAlarm = checkAlarmExists(
alarms,
METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM,
);

if (hasAlarm) {
this.finalizeAbandonedFragments();
}
});
});
} else {
setInterval(() => {
this.finalizeAbandonedFragments();
}, SECOND * 30);
}
}

finalizeAbandonedFragments() {
Object.values(this.store.getState().fragments).forEach((fragment) => {
if (
fragment.timeout &&
Date.now() - fragment.lastUpdated / 1000 > fragment.timeout
) {
this.finalizeEventFragment(fragment.id, { abandoned: true });
}
});
}

generateMetaMetricsId() {
Expand Down
12 changes: 12 additions & 0 deletions app/scripts/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ function getChainType(chainId) {
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 {
getPlatform,
getEnvironmentType,
Expand All @@ -161,4 +172,5 @@ export {
addHexPrefix,
bnToHex,
getChainType,
checkAlarmExists,
};
2 changes: 2 additions & 0 deletions shared/constants/alarms.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const AUTO_LOCK_TIMEOUT_ALARM = 'AUTO_LOCK_TIMEOUT_ALARM';
export const METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM =
'METAMETRICS_FINALIZE_EVENT_FRAGMENT_ALARM';

0 comments on commit 29c2b13

Please sign in to comment.