Skip to content

Commit abb3832

Browse files
More pseudocode and testing added to mainreducer.js and background.js
1 parent 717ae51 commit abb3832

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

src/app/reducers/mainReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { produce } from 'immer';
22
import _, { values } from 'lodash';
33
import * as types from '../constants/actionTypes.ts';
4-
import { current } from 'immer';
4+
// import { current } from 'immer';
55

66
export default (state, action) =>
77
produce(state, (draft) => {

src/extension/background.js

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,39 @@ function changeCurrLocation(tabObj, rootNode, index, name) {
132132
}
133133
}
134134

135+
/*
136+
The 'chrome.runtime' API allows a connection to the background service worker (background.js).
137+
This allows us to set up listener's for when we connect, message, and disconnect the script.
138+
*/
139+
135140
// Establishing incoming connection with Reactime.
136141
chrome.runtime.onConnect.addListener((port) => {
137-
// port is one end of the connection - an object
138-
// push every port connected to the ports array
139-
portsArr.push(port);
142+
/*
143+
On initial connection, there is an onConnect event emitted. The 'addlistener' method provides a communication channel object ('port') when we connect to the service worker ('background.js') and applies it as the argument to it's 1st callback parameter.
144+
145+
the 'port' (type: communication channel object) is the communication channel between different components within our Chrome extension, not to a port on the Chrome browser tab or the extension's port on the Chrome browser.
146+
147+
The port object facilitates communication between the Reactime front-end and this 'background.js' script. This allows you to:
148+
1. send messages and data
149+
(look for 'onMessage'/'postMessage' methods within this page)
150+
2. receive messages and data
151+
(look for 'addListener' methods within this page)
152+
between the front-end and the background.
153+
154+
To establish communication between different parts of your extension:
155+
for the connecting end: use chrome.runtime.connect()
156+
for the listening end: use chrome.runtime.onConnect.
157+
Once the connection is established, a port object is passed to the addListener callback function, allowing you to start exchanging data.
158+
159+
Again, this port object is used for communication within your extension, not for communication with external ports or tabs in the Chrome browser. If you need to interact with specific tabs or external ports, you would use other APIs or methods, such as chrome.tabs or other Chrome Extension APIs.
160+
*/
161+
162+
portsArr.push(port); // push each Reactime communication channel object to the portsArr
163+
140164
// On Reactime launch: make sure RT's active tab is correct
141165
if (portsArr.length > 0) {
142-
portsArr.forEach((bg) =>
143-
bg.postMessage({
166+
portsArr.forEach((bg) => // go through each port object (each Reactime instance)
167+
bg.postMessage({ // send passed in action object as a message to the current port
144168
action: 'changeTab',
145169
payload: { tabId: activeTab.id, title: activeTab.title },
146170
}),
@@ -181,56 +205,49 @@ chrome.runtime.onConnect.addListener((port) => {
181205

182206
switch (action) {
183207
case 'import': // create a snapshot property on tabId and set equal to tabs object
184-
const savedSnapshot = payload;
185-
console.log('background.js savedSnapshot:', savedSnapshot);
186208
// may need do something like filter payload from stateless
187-
tabsObj[tabId].snapshots = savedSnapshot.snapshots;
188-
189-
// reset snapshots to page last state recorded
190-
tabsObj[tabId].snapshots = savedSnapshot.snapshots;
209+
tabsObj[tabId].snapshots = payload.snapshots; // reset snapshots to page last state recorded
210+
// tabsObj[tabId].hierarchy = savedSnapshot.hierarchy; // why don't we just use hierarchy? Because it breaks everything...
211+
tabsObj[tabId].hierarchy.children = payload.hierarchy.children; // resets hierarchy to last state recorded
212+
tabsObj[tabId].hierarchy.stateSnapshot = payload.hierarchy.stateSnapshot; // resets hierarchy to last state recorded
213+
tabsObj[tabId].currLocation = payload.currLocation; // resets currLocation to last state recorded
214+
tabsObj[tabId].index = payload.index; //reset index to last state recorded
215+
tabsObj[tabId].currParent = payload.currParent; // reset currParent to last state recorded
216+
tabsObj[tabId].currBranch = payload.currBranch; // reset currBranch to last state recorded
191217

192-
// resets hierarchy
193-
tabsObj[tabId].hierarchy.children = savedSnapshot.hierarchy.children;
194-
// resets hierarchy to page last state recorded
195-
tabsObj[tabId].hierarchy.stateSnapshot = savedSnapshot.hierarchy.stateSnapshot;
196-
// resets currLocation to page last state recorded
197-
tabsObj[tabId].currLocation = savedSnapshot.currLocation;
198-
tabsObj[tabId].index = savedSnapshot.index;
199-
tabsObj[tabId].currParent = savedSnapshot.currParent;
200-
tabsObj[tabId].currBranch = savedSnapshot.currBranch;
218+
return true; // return true so that port remains open
201219

202-
return true;
203220
case 'emptySnap':
204-
// reset snapshots to page last state recorded
205-
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]];
206-
// resets hierarchy
207-
tabsObj[tabId].hierarchy.children = [];
208-
// resets hierarchy to page last state recorded
209-
tabsObj[tabId].hierarchy.stateSnapshot = {
221+
tabsObj[tabId].snapshots = [tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]]; // reset snapshots to page last state recorded
222+
tabsObj[tabId].hierarchy.children = []; // resets hierarchy
223+
tabsObj[tabId].hierarchy.stateSnapshot = { // resets hierarchy to page last state recorded
210224
...tabsObj[tabId].snapshots[0],
211225
};
212-
// resets currLocation to page last state recorded
213-
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy;
214-
tabsObj[tabId].index = 1;
215-
tabsObj[tabId].currParent = 0;
216-
tabsObj[tabId].currBranch = 1;
217-
return true;
218-
// Pause = lock on tab
219-
case 'setPause':
226+
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy; // resets currLocation to page last state recorded
227+
tabsObj[tabId].index = 1; //reset index
228+
tabsObj[tabId].currParent = 0; // reset currParent
229+
tabsObj[tabId].currBranch = 1; // reset currBranch
230+
return true; // return true so that port remains open
231+
232+
case 'setPause': // Pause = lock on tab
220233
tabsObj[tabId].mode.paused = payload;
221-
return true;
234+
return true; // return true so that port remains open
235+
222236
case 'launchContentScript':
223237
chrome.scripting.executeScript({
224238
target: { tabId },
225239
files: ['bundles/content.bundle.js'],
226240
});
227241
return true;
242+
228243
case 'jumpToSnap':
229244
chrome.tabs.sendMessage(tabId, msg);
230245
return true; // attempt to fix message port closing error, consider return Promise
246+
231247
case 'toggleRecord':
232248
chrome.tabs.sendMessage(tabId, msg);
233249
return true;
250+
234251
default:
235252
return true;
236253
}

0 commit comments

Comments
 (0)