Skip to content

Commit 4f5f25e

Browse files
committed
initial hooks implementation
2 parents 55777fb + 013cee4 commit 4f5f25e

File tree

9 files changed

+65
-12
lines changed

9 files changed

+65
-12
lines changed

package/__tests__/linkFiber.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react/jsx-filename-extension */
2-
import React from 'react';
2+
import React, { Component, useState } from 'react';
33
import { render } from 'react-dom';
44

55
const linkFiberRequire = require('../linkFiber');
@@ -8,7 +8,7 @@ let linkFiber;
88
let mode;
99
let snapShot;
1010

11-
class App extends React.Component {
11+
class App extends Component {
1212
constructor(props) {
1313
super(props);
1414
this.state = { foo: 'bar' };
@@ -20,6 +20,17 @@ class App extends React.Component {
2020
}
2121
}
2222

23+
// Need to create a functioanl component instance to test
24+
// Would need to be revised but here's the gist
25+
// const funcComponent = () => {
26+
// const [ number, setNumber ] = useState(0);
27+
// const newNumber = setNumber(1);
28+
29+
// return (
30+
// <div>{newNumber}</div>
31+
// )
32+
// }
33+
2334
describe('unit test for linkFiber', () => {
2435
beforeEach(() => {
2536
snapShot = { tree: null };

package/__tests__/timeJump.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class FiberNode {
1919
}
2020
}
2121

22+
// MVP FEATURE: Additional Testing
23+
// Testing for useState and useContext
24+
25+
2226
describe('unit testing for timeJump.js', () => {
2327
let timeJump;
2428
let snapShot;

package/linkFiber.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (snap, mode) => {
1212
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1313
if (mode.jumping || mode.paused) return;
1414
const payload = snap.tree.getCopy();
15-
console.log('payload', payload);
15+
// console.log('payload', payload);
1616
window.postMessage({
1717
action: 'recordSnap',
1818
payload,
@@ -43,10 +43,34 @@ module.exports = (snap, mode) => {
4343
component.setState.linkFiberChanged = true;
4444
}
4545

46+
// Helper function to traverse through the memoized state
47+
function traverseHooks(memoizedState) {
48+
// Declare variables and assigned to 0th index and an empty object, respectively
49+
let index = 0;
50+
let memoizedObj = {};
51+
// while memoizedState is truthy, save the value to the object
52+
while (memoizedState) {
53+
memoizedObj[index] = memoizedState.memoizedState;
54+
// Reassign memoizedState to its next value
55+
memoizedState = memoizedState.next;
56+
// Increment the index by 1
57+
index += 1;
58+
}
59+
return memoizedObj;
60+
}
61+
4662
function createTree(currentFiber, tree = new Tree('root')) {
4763
if (!currentFiber) return tree;
48-
49-
const { sibling, stateNode, child } = currentFiber;
64+
// We have to figure out which properties to destructure from currentFiber
65+
// To support hooks and Context API
66+
const { sibling, stateNode, child, memoizedState } = currentFiber;
67+
// TODO: Refactor the conditionals - think about the edge case where a stateful component might have a key called 'baseState' in the state
68+
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
69+
// console.log('The hook element is:', currentFiber);
70+
// console.log('The memoizedState is: ', memoizedState);
71+
const result = traverseHooks(memoizedState);
72+
console.log('This is the result of calling traverseHooks:', result);
73+
}
5074

5175
let nextTree = tree;
5276
// check if stateful component
@@ -56,7 +80,8 @@ module.exports = (snap, mode) => {
5680
// change setState functionality
5781
changeSetState(stateNode);
5882
}
59-
83+
// Need to check if functional component AND uses hooks
84+
6085
// iterate through siblings
6186
createTree(sibling, tree);
6287
// iterate through children
@@ -66,19 +91,19 @@ module.exports = (snap, mode) => {
6691
}
6792

6893
function updateSnapShotTree() {
69-
console.log('fiberRoot', fiberRoot);
94+
// console.log('fiberRoot', fiberRoot);
7095
const { current } = fiberRoot;
71-
console.log('current', current);
96+
// console.log('current', current);
7297
snap.tree = createTree(current);
7398
}
7499

75100
return container => {
76-
console.log('Container', container);
101+
// console.log('Container', container);
77102
const {
78103
_reactRootContainer: { _internalRoot },
79104
_reactRootContainer,
80105
} = container;
81-
console.log('Root container', _reactRootContainer);
106+
// console.log('Root container', _reactRootContainer);
82107
// only assign internal root if it actually exists
83108
fiberRoot = _internalRoot || _reactRootContainer;
84109
updateSnapShotTree();

package/timeJump.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module.exports = (origin, mode) => {
1212
// recursively change state of tree
1313
function jump(target, coords = []) {
1414
const originNode = traverseTree(origin.tree, coords);
15-
1615
// set the state of the origin tree
1716
originNode.component.setState(target.state, () => {
1817
// iterate through new children once state has been set

package/tree.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Tree {
1515
}
1616
this.children = [];
1717
// DEV: Added print() for debugging purposes
18-
this.print();
18+
// this.print();
1919
}
2020

2121
appendChild(component) {
@@ -38,6 +38,8 @@ class Tree {
3838

3939
// print out the tree in the console
4040
// DEV: Process may be different for useState components
41+
// BUG FIX: Don't print the Router as a component
42+
// Change how the children are printed
4143
print() {
4244
const children = ['children: '];
4345
// DEV: What should we push instead for components using hooks (it wouldn't be state)

src/app/components/Action.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import PropTypes from 'prop-types';
33

44
import { changeView, changeSlider } from '../actions/actions';
55

6+
// BUG FIX:
7+
// changeSlider should also respond to the click event on the div
8+
69
const Action = props => {
710
const {
811
selected, index, sliderIndex, dispatch,

src/app/components/Diff.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ReactHtmlParser from 'react-html-parser';
55

66
import { useStoreContext } from '../store';
77

8+
// FIX: Update the div copy to something more explanatory
9+
810
function Diff({ snapshot, show }) {
911
const [mainState] = useStoreContext();
1012
const { currentTab, tabs } = mainState;

src/app/components/SwitchApp.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Select from 'react-select';
33
import { useStoreContext } from '../store';
44
import { setTab } from '../actions/actions';
55

6+
7+
// BUG FIX: Fix the dropdown styling to make it more distinguishable
8+
69
const SwitchAppDropdown = () => {
710
const [{ currentTab, tabs }, dispatch] = useStoreContext();
811

src/app/containers/ActionContainer.jsx

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

8+
// MVP Feature: Include a dropdown functionality
9+
// to show stateful/functional/Context API differentiation
10+
// May want to add another child component to the container
11+
812
function ActionContainer() {
913
const [{ tabs, currentTab }, dispatch] = useStoreContext();
1014
const { snapshots, sliderIndex, viewIndex } = tabs[currentTab];

0 commit comments

Comments
 (0)