Skip to content

Commit 84fa865

Browse files
authored
Merge pull request #16 from oslabs-beta/oliver/toggle-ax
Oliver/toggle ax
2 parents 10b2796 + d17ea61 commit 84fa865

File tree

3 files changed

+96
-55
lines changed

3 files changed

+96
-55
lines changed

src/backend/controllers/timeJump.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import componentActionsRecord from '../models/masterState';
22
import { Status } from '../types/backendTypes';
33
import Tree from '../models/tree';
4+
import { update } from 'lodash';
45

56
// THIS FILE CONTAINS NECCESSARY FUNCTIONALITY FOR TIME-TRAVEL FEATURE
67

@@ -48,6 +49,12 @@ async function updateReactFiberTree(
4849
targetSnapshot,
4950
circularComponentTable: Set<any> = new Set(),
5051
): Promise<void> {
52+
console.log(
53+
'updateReactFiberTree: targetSnapshot:',
54+
targetSnapshot,
55+
'circularComponentTable:',
56+
circularComponentTable,
57+
);
5158
if (!targetSnapshot) return;
5259
// Base Case: if has visited, return
5360
if (circularComponentTable.has(targetSnapshot)) {

src/backend/models/masterState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111
* @function clear - Clears componentActionsRecord
1212
*/
1313
clear: (): void => {
14+
// console.log(componentActionsRecord);
1415
componentActionsRecord = [];
1516
},
1617

src/extension/background.js

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const portsArr = [];
77
const reloaded = {};
88
const firstSnapshotReceived = {};
99

10+
// Toggle for recording accessibility snapshots
11+
let toggleAxRecord = false;
12+
1013
// There will be the same number of objects in here as there are
1114
// Reactime tabs open for each user application being worked on.
1215
let activeTab;
@@ -57,6 +60,55 @@ const pruneAxTree = (axTree) => {
5760
return axArr;
5861
};
5962

63+
function attachDebugger(tabId, version) {
64+
return new Promise((resolve, reject) => {
65+
chrome.debugger.attach({ tabId: tabId }, version, () => {
66+
if (chrome.runtime.lastError) {
67+
reject(chrome.runtime.lastError);
68+
} else {
69+
resolve();
70+
}
71+
});
72+
});
73+
}
74+
75+
function sendDebuggerCommand(tabId, command, params = {}) {
76+
return new Promise((resolve, reject) => {
77+
chrome.debugger.sendCommand({ tabId: tabId }, command, params, (response) => {
78+
if (chrome.runtime.lastError) {
79+
reject(chrome.runtime.lastError);
80+
} else {
81+
resolve(response);
82+
}
83+
});
84+
});
85+
}
86+
87+
function detachDebugger(tabId) {
88+
return new Promise((resolve, reject) => {
89+
chrome.debugger.detach({ tabId: tabId }, () => {
90+
if (chrome.runtime.lastError) {
91+
reject(chrome.runtime.lastError);
92+
} else {
93+
resolve();
94+
}
95+
});
96+
});
97+
}
98+
99+
async function axRecord(tabId) {
100+
try {
101+
await attachDebugger(tabId, '1.3');
102+
await sendDebuggerCommand(tabId, 'Accessibility.enable');
103+
const response = await sendDebuggerCommand(tabId, 'Accessibility.getFullAXTree');
104+
const pruned = pruneAxTree(response.nodes);
105+
await detachDebugger(tabId);
106+
return pruned;
107+
} catch (error) {
108+
console.error('axRecord debugger command failed:', error);
109+
}
110+
}
111+
60112
// This function will create the first instance of the test app's tabs object
61113
// which will hold test app's snapshots, link fiber tree info, chrome tab info, etc.
62114
function createTabObj(title) {
@@ -318,6 +370,10 @@ chrome.runtime.onConnect.addListener((port) => {
318370
chrome.tabs.sendMessage(tabId, msg);
319371
return true;
320372

373+
case 'toggleAxRecord':
374+
toggleAxRecord = !toggleAxRecord;
375+
return true;
376+
321377
case 'reinitialize':
322378
chrome.tabs.sendMessage(tabId, msg);
323379
return true;
@@ -369,6 +425,17 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
369425
}
370426
case 'jumpToSnap': {
371427
changeCurrLocation(tabsObj[tabId], tabsObj[tabId].hierarchy, index, name);
428+
// hack to test without message from mainSlice
429+
toggleAxRecord = true;
430+
// 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;
437+
}
438+
372439
if (portsArr.length > 0) {
373440
portsArr.forEach((bg) =>
374441
bg.postMessage({
@@ -379,6 +446,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
379446
}
380447
break;
381448
}
449+
382450
// Confirmed React Dev Tools installed, send this info to frontend
383451
case 'devToolsInstalled': {
384452
tabsObj[tabId].status.reactDevToolsInstalled = true;
@@ -427,60 +495,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
427495
'background.js: top of recordSnap: tabsObj[tabId]:',
428496
JSON.parse(JSON.stringify(tabsObj[tabId])),
429497
);
430-
function addAxSnap(snap) {
431-
const pruned = pruneAxTree(snap);
432-
tabsObj[tabId].axSnapshots.push(pruned);
433-
return pruned;
434-
}
435-
436-
function attachDebugger(tabId, version) {
437-
return new Promise((resolve, reject) => {
438-
chrome.debugger.attach({ tabId: tabId }, version, () => {
439-
if (chrome.runtime.lastError) {
440-
reject(chrome.runtime.lastError);
441-
} else {
442-
resolve();
443-
}
444-
});
445-
});
446-
}
447-
448-
function sendDebuggerCommand(tabId, command, params = {}) {
449-
return new Promise((resolve, reject) => {
450-
chrome.debugger.sendCommand({ tabId: tabId }, command, params, (response) => {
451-
if (chrome.runtime.lastError) {
452-
reject(chrome.runtime.lastError);
453-
} else {
454-
resolve(response);
455-
}
456-
});
457-
});
458-
}
459-
460-
function detachDebugger(tabId) {
461-
return new Promise((resolve, reject) => {
462-
chrome.debugger.detach({ tabId: tabId }, () => {
463-
if (chrome.runtime.lastError) {
464-
reject(chrome.runtime.lastError);
465-
} else {
466-
resolve();
467-
}
468-
});
469-
});
470-
}
471498

472-
async function axRecord(tabId) {
473-
try {
474-
await attachDebugger(tabId, '1.3');
475-
await sendDebuggerCommand(tabId, 'Accessibility.enable');
476-
const response = await sendDebuggerCommand(tabId, 'Accessibility.getFullAXTree');
477-
const addedAxSnap = addAxSnap(response.nodes);
478-
await detachDebugger(tabId);
479-
return addedAxSnap;
480-
} catch (error) {
481-
console.error('axRecord debugger command failed:', error);
482-
}
483-
}
484499
const sourceTab = tabId;
485500
tabsObj[tabId].webMetrics = metrics;
486501

@@ -489,7 +504,16 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
489504
reloaded[tabId] = false;
490505
tabsObj[tabId].webMetrics = metrics;
491506
tabsObj[tabId].snapshots.push(request.payload);
492-
const addedAxSnap = await axRecord(tabId);
507+
508+
// check if accessibility recording has been toggled on
509+
let addedAxSnap;
510+
if (toggleAxRecord === true) {
511+
addedAxSnap = await axRecord(tabId);
512+
tabsObj[tabId].axSnapshots.push(addedAxSnap);
513+
} else {
514+
addedAxSnap = 'emptyAxSnap';
515+
tabsObj[tabId].axSnapshots.push(addedAxSnap);
516+
}
493517
sendToHierarchy(
494518
tabsObj[tabId],
495519
new HistoryNode(tabsObj[tabId], request.payload, addedAxSnap),
@@ -530,7 +554,16 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
530554
tabsObj[tabId].snapshots.push(request.payload);
531555
// INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN
532556
if (!tabsObj[tabId][index]) {
533-
const addedAxSnap = await axRecord(tabId);
557+
// check if accessibility recording has been toggled on
558+
let addedAxSnap;
559+
if (toggleAxRecord === true) {
560+
addedAxSnap = await axRecord(tabId);
561+
tabsObj[tabId].axSnapshots.push(addedAxSnap);
562+
} else {
563+
addedAxSnap = 'emptyAxSnap';
564+
tabsObj[tabId].axSnapshots.push(addedAxSnap);
565+
}
566+
534567
sendToHierarchy(
535568
tabsObj[tabId],
536569
new HistoryNode(tabsObj[tabId], request.payload, addedAxSnap),

0 commit comments

Comments
 (0)