Skip to content

Commit 30feca1

Browse files
authored
Merge pull request #4 from oslabs-beta/chris-10-29
some hooks updates and main slider fix 11/4
2 parents 780096e + a668ac2 commit 30feca1

File tree

8 files changed

+75
-61
lines changed

8 files changed

+75
-61
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
"contributors": [
2323
"Andy Wong",
2424
"Bryan Lee",
25+
"Chris Flannery",
2526
"David Chai",
2627
"Josh Kim",
28+
"Prasanna Malla",
29+
"Rajeeb Banstola",
30+
"Rocky Lin",
2731
"Ruth Anam",
2832
"Ryan Dang",
2933
"Sierra Swaby",

package/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const mode = {
99
const linkFiber = require('./linkFiber')(snapShot, mode);
1010
const timeJump = require('./timeJump')(snapShot, mode);
1111

12-
window.addEventListener('message', ({ data: { action, payload } }) => {
12+
window.addEventListener('message', ({ data: { action, payload } }) => { //runs automatically twice per second with inspectedElement
1313
switch (action) {
1414
case 'jumpToSnap':
1515
timeJump(payload);

package/linkFiber.js

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
/* eslint-disable func-names */
22
/* eslint-disable no-use-before-define */
33
/* eslint-disable no-param-reassign */
4-
54
// links component state tree to library
65
// changes the setState method to also update our snapshot
7-
86
const Tree = require('./tree');
97
const astParser = require('./astParser');
10-
const { saveState } = require('./masterState'); // saves AST state as array for later use
8+
const { saveState } = require('./masterState');
119

1210
module.exports = (snap, mode) => {
13-
// snap is the current tree
14-
// mode is {jumping: bool, locked: bool, paused: bool}
15-
1611
let fiberRoot = null;
1712
let astHooks;
1813

19-
function sendSnapshot() { // send snapshot of current fiber tree to chrome extension ?
14+
function sendSnapshot() {
2015
// don't send messages while jumping or while paused
21-
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
16+
// DEV: So that when we are jumping to an old snapshot it
17+
// wouldn't think we want to create new snapshots
2218
if (mode.jumping || mode.paused) return;
23-
const payload = snap.tree.getCopy(); // copy of current react fiber tree
19+
const payload = snap.tree.getCopy();
2420
// console.log('payload', payload);
25-
window.postMessage({ // send to window
21+
window.postMessage({
2622
action: 'recordSnap',
2723
payload,
2824
});
2925
}
3026

31-
function changeSetState(component) { // if invoked, change setState functionality so that it also updates our snapshot
32-
// console.log("what is component?", component);
27+
function changeSetState(component) {
3328
// check that setState hasn't been changed yet
34-
if (component.setState.linkFiberChanged) {
35-
// console.log("setState has already been changed for", component);
36-
return;
37-
};
29+
if (component.setState.linkFiberChanged) return;
3830
// make a copy of setState
3931
const oldSetState = component.setState.bind(component);
4032
// replace component's setState so developer doesn't change syntax
@@ -43,30 +35,29 @@ module.exports = (snap, mode) => {
4335
// don't do anything if state is locked
4436
// UNLESS we are currently jumping through time
4537
if (mode.locked && !mode.jumping) return;
46-
// continue normal setState functionality, except add sending message (to chrome extension) middleware
38+
// continue normal setState functionality, except add sending message middleware
4739
oldSetState(state, () => {
48-
updateSnapShotTree(); // this doubles the actions in reactime for star wars app, also invokes changeSetState twice, also invokes changeSetState with Route and Characters
49-
sendSnapshot(); //runs once on page load, after event listener: message (line 145)
50-
callback.bind(component)(); // WHY DO WE NEED THIS ?
40+
updateSnapShotTree();
41+
sendSnapshot();
42+
callback.bind(component)();
5143
});
5244
};
53-
component.setState.linkFiberChanged = true; // we changed setState.
45+
component.setState.linkFiberChanged = true;
5446
}
5547

56-
function changeUseState(component) { // if invoked, change useState dispatch functionality so that it also updates our snapshot
57-
//check that changeUseState hasn't been changed yet
48+
function changeUseState(component) {
5849
if (component.queue.dispatch.linkFiberChanged) return;
5950
// store the original dispatch function definition
6051
const oldDispatch = component.queue.dispatch.bind(component.queue);
6152
// redefine the dispatch function so we can inject our code
6253
component.queue.dispatch = (fiber, queue, action) => {
63-
// don't do anything if state is locked, UNLESS we are currently jumping through time
54+
// don't do anything if state is locked
6455
if (mode.locked && !mode.jumping) return;
6556
oldDispatch(fiber, queue, action);
66-
setTimeout(() => {
67-
updateSnapShotTree();
68-
sendSnapshot();
69-
}, 100);
57+
// setTimeout(() => {
58+
updateSnapShotTree();
59+
sendSnapshot();
60+
// }, 100);
7061
};
7162
component.queue.dispatch.linkFiberChanged = true;
7263
}
@@ -79,8 +70,10 @@ module.exports = (snap, mode) => {
7970
let index = 0;
8071
astHooks = Object.values(astHooks);
8172
// while memoizedState is truthy, save the value to the object
82-
while (memoizedState) {
83-
changeUseState(memoizedState);
73+
while (memoizedState && memoizedState.queue) { // prevents useEffect from crashing on load
74+
if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
75+
changeUseState(memoizedState);
76+
}
8477
// memoized[astHooks[index]] = memoizedState.memoizedState;
8578
memoized[astHooks[index]] = memoizedState.memoizedState;
8679
// Reassign memoizedState to its next value
@@ -92,22 +85,21 @@ module.exports = (snap, mode) => {
9285
}
9386

9487
function createTree(currentFiber, tree = new Tree('root')) {
95-
// if there is no current fiber just return the new tree as-is
9688
if (!currentFiber) return tree;
97-
// console.log("what is currentFiber", currentFiber);
89+
9890
const {
9991
sibling,
10092
stateNode,
10193
child,
10294
memoizedState,
10395
elementType,
104-
} = currentFiber; // extract properties of current fiber
96+
} = currentFiber;
10597

106-
let childTree = tree; // initialize child fiber tree as current fiber tree
98+
let nextTree = tree;
10799
// check if stateful component
108100
if (stateNode && stateNode.state) {
109101
// add component to tree
110-
childTree = tree.appendChild(stateNode); // returns newly appended tree
102+
nextTree = tree.appendChild(stateNode);
111103
// change setState functionality
112104
changeSetState(stateNode);
113105
}
@@ -119,43 +111,34 @@ module.exports = (snap, mode) => {
119111
// Create a traversed property and assign to the evaluated result of
120112
// invoking traverseHooks with memoizedState
121113
memoizedState.traversed = traverseHooks(memoizedState);
122-
childTree = tree.appendChild(memoizedState);
114+
nextTree = tree.appendChild(memoizedState);
123115
}
124116
// iterate through siblings
125117
createTree(sibling, tree);
126118
// iterate through children
127-
createTree(child, childTree);
119+
createTree(child, nextTree);
128120

129121
return tree;
130122
}
131-
132-
// runs when page initially loads and on subsequent state changes
123+
// runs when page initially loads
133124
// but skips 1st hook click
134125
function updateSnapShotTree() {
135-
const { current } = fiberRoot; // on initial page load, current - fiberNode is tag type HostRoot (entire fiber tree)
136-
console.log("current", current);
126+
const { current } = fiberRoot;
137127
snap.tree = createTree(current);
138128
}
139129

140-
// RUNS ONCE, ON INITIAL PAGE LOAD ?
141130
return container => {
142-
// on first page load, container is entire html hierarchy of top level div
143-
// _reactRootContainer is that invisible top level object which wraps the top level div
144-
// _reactRootContainer._internalRoot is an object with property .current which includes HostRoot fiberNode (entire fiber tree)
145131
const {
146132
_reactRootContainer: { _internalRoot },
147133
_reactRootContainer,
148134
} = container;
149-
// only assign internal root if it actually exists
135+
// only assign internal rootp if it actually exists
150136
fiberRoot = _internalRoot || _reactRootContainer;
151137

152138
updateSnapShotTree();
153139
// send the initial snapshot once the content script has started up
154140
window.addEventListener('message', ({ data: { action } }) => {
155-
if (action === 'contentScriptStarted') { // runs once on initial page load
156-
// console.log("in window.addEL")
157-
sendSnapshot()
158-
};
141+
if (action === 'contentScriptStarted') sendSnapshot();
159142
});
160143
};
161144
};

package/timeJump.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = (origin, mode) => {
2828
let index = 0;
2929
const hooks = returnState();
3030
// while loop through the memoize tree
31-
while (current) {
31+
while (current && current.queue) { // allows time travel with useEffect
3232
current.queue.dispatch(target.state[hooks[index]]);
3333
// Reassign the current value
3434
current = current.next;

src/app/containers/ActionContainer.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import Action from '../components/Action';
55
import { emptySnapshots } from '../actions/actions';
66
import { useStoreContext } from '../store';
77

8+
const resetSlider = () => {
9+
console.log('in reset slider');
10+
const slider = document.querySelector('.rc-slider-handle');
11+
console.log('my slider', slider);
12+
slider.setAttribute('style', 'left: 0');
13+
};
14+
815
function ActionContainer() {
916
const [{ tabs, currentTab }, dispatch] = useStoreContext();
1017
const { snapshots, sliderIndex, viewIndex } = tabs[currentTab];
@@ -28,7 +35,15 @@ function ActionContainer() {
2835
return (
2936
<div className="action-container">
3037
<div className="action-component exclude">
31-
<button className="empty-button" onClick={() => dispatch(emptySnapshots())} type="button">
38+
<button
39+
className="empty-button"
40+
onClick={() => {
41+
dispatch(emptySnapshots());
42+
// set slider back to zero
43+
resetSlider();
44+
}}
45+
type="button"
46+
>
3247
Empty
3348
</button>
3449
</div>

src/extension/background.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/* eslint-disable max-len */
22
/* eslint-disable no-param-reassign */
3+
34
// store ports in an array
45
const portsArr = [];
56
const reloaded = {};
67
const firstSnapshotReceived = {};
78
// there will be the same number of objects in here as there are reactime tabs open for each user application being worked on
89
const tabsObj = {};
910

11+
console.log("background scripts");
12+
1013
function createTabObj(title) {
14+
1115
// update tabsObj
1216
return {
1317
title,
@@ -65,7 +69,8 @@ function changeCurrLocation(tabObj, rootNode, index) {
6569

6670

6771
// establishing connection with devtools
68-
chrome.runtime.onConnect.addListener(port => {
72+
chrome.runtime.onConnect.addListener(port => { // port is one end of the connection - an object
73+
6974
// push every port connected to the ports array
7075
portsArr.push(port);
7176

@@ -87,8 +92,9 @@ chrome.runtime.onConnect.addListener(port => {
8792
}
8893
});
8994

90-
// receive snapshot from devtools and send it to contentScript
91-
port.onMessage.addListener(msg => {
95+
// receive snapshot from devtools and send it to contentScript -
96+
port.onMessage.addListener(msg => { // msg is action denoting a time jump in devtools
97+
9298
// ---------------------------------------------------------------
9399
// message incoming from devTools should look like this:
94100
// {
@@ -99,7 +105,7 @@ chrome.runtime.onConnect.addListener(port => {
99105
// ---------------------------------------------------------------
100106
const { action, payload, tabId } = msg;
101107
switch (action) {
102-
case 'import':
108+
case 'import': // create a snapshot property on tabId and set equal to tabs object
103109
tabsObj[tabId].snapshots = payload;
104110
return;
105111
case 'emptySnap':
@@ -247,6 +253,7 @@ chrome.runtime.onInstalled.addListener(() => {
247253
// when context menu is clicked, listen for the menuItemId,
248254
// if user clicked on reactime, open the devtools window
249255
chrome.contextMenus.onClicked.addListener(({ menuItemId }) => {
256+
console.log("clicked menu");
250257
const options = {
251258
type: 'panel',
252259
left: 0,

src/extension/contentScript.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let firstMessage = true;
22

33
// listening for messages from npm package
4-
window.addEventListener('message', msg => {
4+
window.addEventListener('message', msg => { // runs automatically every second
55
// window listener picks up the message it sends, so we should filter
66
// messages sent by contentscript
77
if (msg.data.action !== 'contentScriptStarted' && firstMessage) {
@@ -13,11 +13,15 @@ window.addEventListener('message', msg => {
1313

1414
// post initial Message to background.js
1515
const { action } = msg.data;
16-
if (action === 'recordSnap') chrome.runtime.sendMessage(msg.data);
16+
17+
if (action === 'recordSnap') { // this is firing on page load
18+
chrome.runtime.sendMessage(msg.data);
19+
}
1720
});
1821

1922
// listening for messages from the UI
20-
chrome.runtime.onMessage.addListener(request => {
23+
chrome.runtime.onMessage.addListener(request => { // seems to never fire
24+
2125
// send the message to npm package
2226
const { action } = request;
2327
switch (action) {

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const path = require('path');
2-
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader');
2+
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader'); //enable hot reloading while developing a chrome extension
33

44
const config = {
5+
// use a "multi-main entry" to inject multiple dependent files together and graph their dependencies into one "chunk"
56
entry: {
67
app: './src/app/index.js',
78
background: './src/extension/background.js',

0 commit comments

Comments
 (0)