Skip to content

Commit c2a40ef

Browse files
committed
refactored into reusable function replaceEmptySnap that now makes the ax toggle button work
1 parent 4512041 commit c2a40ef

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

src/extension/background.js

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ const pruneAxTree = (axTree) => {
3232
properties,
3333
} = node;
3434

35-
if(!name){
36-
if(ignored){
37-
name = {value: `ignored node: ${ignoredReasons[0].name}`};
38-
}
39-
else{
40-
name = {value: 'visible node with no name'};
35+
if (!name) {
36+
if (ignored) {
37+
name = { value: `ignored node: ${ignoredReasons[0].name}` };
38+
} else {
39+
name = { value: 'visible node with no name' };
4140
}
4241
}
43-
if(!name.value){
42+
if (!name.value) {
4443
name.value = 'visible node with no name';
4544
}
4645
const axNode = {
@@ -109,6 +108,24 @@ async function axRecord(tabId) {
109108
}
110109
}
111110

111+
async function replaceEmptySnap(tabsObj, tabId, toggleAxRecord) {
112+
console.log(
113+
'background.js: top of replaceEmptySnap: tabsObj[tabId]:',
114+
JSON.parse(JSON.stringify(tabsObj[tabId])),
115+
);
116+
if (tabsObj[tabId].currLocation.axSnapshot === 'emptyAxSnap' && toggleAxRecord === true) {
117+
// add new ax snapshot to currlocation
118+
const addedAxSnap = await axRecord(tabId);
119+
tabsObj[tabId].currLocation.axSnapshot = addedAxSnap;
120+
// modify array to include the new recorded ax snapshot
121+
tabsObj[tabId].axSnapshots[tabsObj[tabId].currLocation.index] = addedAxSnap;
122+
}
123+
console.log(
124+
'background.js: bottom of replaceEmptySnap: tabsObj[tabId]:',
125+
JSON.parse(JSON.stringify(tabsObj[tabId])),
126+
);
127+
}
128+
112129
// This function will create the first instance of the test app's tabs object
113130
// which will hold test app's snapshots, link fiber tree info, chrome tab info, etc.
114131
function createTabObj(title) {
@@ -296,7 +313,7 @@ chrome.runtime.onConnect.addListener((port) => {
296313
// INCOMING MESSAGE FROM FRONTEND (MainContainer) TO BACKGROUND.js
297314
// listen for message containing a snapshot from devtools and send it to contentScript -
298315
// (i.e. they're all related to the button actions on Reactime)
299-
port.onMessage.addListener((msg) => {
316+
port.onMessage.addListener(async (msg) => {
300317
// msg is action denoting a time jump in devtools
301318
// ---------------------------------------------------------------
302319
// message incoming from devTools should look like this:
@@ -372,7 +389,22 @@ chrome.runtime.onConnect.addListener((port) => {
372389

373390
case 'toggleAxRecord':
374391
toggleAxRecord = !toggleAxRecord;
375-
return true;
392+
393+
await replaceEmptySnap(tabsObj, tabId, toggleAxRecord);
394+
395+
// sends new tabs obj to devtools
396+
if (portsArr.length > 0) {
397+
portsArr.forEach((bg) =>
398+
bg.postMessage({
399+
action: 'sendSnapshots',
400+
payload: tabsObj,
401+
tabId,
402+
}),
403+
);
404+
} else {
405+
console.log('background.js: portsArr.length < 0');
406+
}
407+
return true; // return true so that port remains open
376408

377409
case 'reinitialize':
378410
chrome.tabs.sendMessage(tabId, msg);
@@ -424,16 +456,32 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
424456
break;
425457
}
426458
case 'jumpToSnap': {
459+
console.log(
460+
'background.js: top of jumpToSnap: tabsObj[tabId]:',
461+
JSON.parse(JSON.stringify(tabsObj[tabId])),
462+
);
427463
changeCurrLocation(tabsObj[tabId], tabsObj[tabId].hierarchy, index, name);
428464
// hack to test without message from mainSlice
429-
toggleAxRecord = true;
465+
// toggleAxRecord = true;
430466
// record ax tree snapshot of the state that has now been jumped to if user did not toggle button on
431-
if (tabsObj[tabId].currLocation.axSnapshot === 'emptyAxSnap' && toggleAxRecord === true) {
432-
// add new ax snapshot to currlocation
433-
const addedAxSnap = await axRecord(tabId);
434-
tabsObj[tabId].currLocation.axSnapshot = addedAxSnap;
435-
// modify array to include the new recorded ax snapshot
436-
tabsObj[tabId].axSnapshots[tabsObj[tabId].currLocation.index] = addedAxSnap;
467+
await replaceEmptySnap(tabsObj, tabId, toggleAxRecord);
468+
469+
console.log(
470+
'background.js: bottom of jumpToSnap: tabsObj[tabId]:',
471+
JSON.parse(JSON.stringify(tabsObj[tabId])),
472+
);
473+
474+
// sends new tabs obj to devtools
475+
if (portsArr.length > 0) {
476+
portsArr.forEach((bg) =>
477+
bg.postMessage({
478+
action: 'sendSnapshots',
479+
payload: tabsObj,
480+
tabId,
481+
}),
482+
);
483+
} else {
484+
console.log('background.js: portsArr.length < 0');
437485
}
438486

439487
if (portsArr.length > 0) {
@@ -496,6 +544,8 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
496544
JSON.parse(JSON.stringify(tabsObj[tabId])),
497545
);
498546

547+
console.log('background.js: recordSnap case: toggleAxRecord:', toggleAxRecord);
548+
499549
const sourceTab = tabId;
500550
tabsObj[tabId].webMetrics = metrics;
501551

0 commit comments

Comments
 (0)