Skip to content

Commit bb8b317

Browse files
committed
updateSnapShot interface to remove unfilteredTree prop, clean up unused var in linkFiber, added documentation for routes.ts
1 parent 19c3721 commit bb8b317

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

src/backend/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { Snapshot, Status, MsgData } from './types/backendTypes';
1616
// * State snapshot object initialized here
1717
const snapShot: Snapshot = {
1818
tree: null,
19-
unfilteredTree: null,
2019
};
2120

2221
const mode: Status = {

src/backend/linkFiber.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ import {
5959
import Tree from './tree';
6060
// passes the data down to its components
6161
import componentActionsRecord from './masterState';
62-
import routes from './routes';
6362
import updateSnapShotTree from './snapShot';
6463

6564
// throttle returns a function that can be called any number of times (possibly in quick succession) but will only invoke the callback at most once every x ms
6665
// getHooksNames - helper function to grab the getters/setters from `elementType`
6766
import { throttle, getHooksNames } from './helpers';
68-
import { any } from 'prop-types';
6967

7068
// Set global variables to use in exported module and helper functions
7169
declare global {
@@ -76,7 +74,7 @@ declare global {
7674
}
7775

7876
// TODO: Determine what Component Data Type we are sending back for state, context, & props
79-
type ComponentData = {
77+
type ReactimeData = {
8078
[key: string]: any;
8179
};
8280

@@ -188,8 +186,8 @@ const exclude = new Set([
188186
*/
189187
function convertDataToString(
190188
reactDevData: { [key: string]: any },
191-
reactimeData: ComponentData = {},
192-
): ComponentData {
189+
reactimeData: ReactimeData = {},
190+
): ReactimeData {
193191
for (const key in reactDevData) {
194192
// Skip keys that are in exclude set OR if there is no value at key
195193
// Falsy values such as 0, false, null are still valid value
@@ -225,7 +223,7 @@ function trimContextData(
225223
if (ignoreComponent.has(componentName)) return;
226224

227225
// Initialize object to store state and context data of the component
228-
const reactimeData: ComponentData = {};
226+
const reactimeData: ReactimeData = {};
229227
// Initialize counter for the default naming. If user use reactHook, such as useState, react will only pass in the value, and not the variable name of the state.
230228
let stateCounter = 1;
231229
let refCounter = 1;

src/backend/routes.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,80 +23,100 @@ class Routes {
2323
/**
2424
* @property A stack of visited routes that matches the browser history stack.
2525
*/
26-
values: Route[] = [new Route(null, null)];
26+
// TODO: Think about how to remove this dummy URL
27+
routeHistory: Route[] = [new Route('dummyURL', 0)];
2728

2829
/**
29-
* @property Used to assign unique ids to routes in the values stack in case the same route is added to the stack more than once.
30+
* @property Used to assign unique ids to routes in the `routeHistory` stack in case the same route is added to the stack more than once.
3031
*/
3132
id = 0;
3233

3334
/**
34-
* @property The index of the current route in the values stack.
35+
* @property The index of the current route in the `routeHistory` stack.
3536
*/
36-
current: number | null = 0;
37+
current = 0;
3738

3839
/**
3940
* @method addRoute
4041
* @param url - A url string.
4142
* @returns Either the current route if the user has not navigated away from it or a new instance of a route constructed from the url.
4243
*
43-
* Ensures that the values stack always matches the browser history stack.
44+
* Ensures that the `routeHistory` stack always matches the browser history stack.
4445
*/
4546

4647
addRoute(url: string): Route {
47-
let route: Route = this.values[this.current];
48-
49-
if (this.values[this.current].url !== url) {
50-
if (this.current !== this.values.length - 1) {
48+
// Obtain the last visited route within routeHistory stack
49+
let route: Route = this.routeHistory[this.current];
50+
51+
// If the passed in window url does not match with the last visited route
52+
// => user has navigated to another route
53+
if (this.routeHistory[this.current].url !== url) {
54+
// If the last visited index is not the last position in routeHistory stack. This happens when user use the timeJump functionality.
55+
// => Rebuild the browserHistory
56+
if (this.current !== this.routeHistory.length - 1) {
57+
console.log('Rebuild browser history');
5158
this.rebuildHistory(url);
5259
}
53-
60+
// Create a new route instance from the passed in url.
5461
route = new Route(url, (this.id += 1));
55-
this.values.push(route);
56-
57-
this.current = this.values.length - 1;
62+
// Push the new route to routeHistory stack.
63+
this.routeHistory.push(route);
64+
// Update the last visited index.
65+
this.current = this.routeHistory.length - 1;
5866
}
59-
67+
console.log('Route History', this.routeHistory, this.current);
68+
console.log('History length', window.history.length);
6069
return route;
6170
}
6271

6372
/**
6473
* @method rebuildHistory
6574
* @param url - A url string.
6675
*
67-
* Rebuilds the browser history stack using the copy of the stack maintained in the values stack. https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState, https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
76+
* Rebuilds the browser history stack using the copy of the stack maintained in the `routeHistory` stack. https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState, https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
6877
*/
6978
private rebuildHistory(url: string): void {
70-
window.history.replaceState('', '', this.values[this.current + 1].url);
79+
window.history.replaceState('', '', this.routeHistory[this.current + 1].url);
7180

72-
for (let i = this.current + 2; i < this.values.length; i += 1) {
73-
window.history.pushState('', '', this.values[i].url);
81+
for (let i = this.current + 2; i < this.routeHistory.length; i += 1) {
82+
window.history.pushState('', '', this.routeHistory[i].url);
7483
}
7584

7685
window.history.pushState('', '', url);
7786
}
7887

7988
/**
8089
* @method navigate
81-
* @param route - The target route in the values stack that is being navigated to.
90+
* @param route - The target route in the `routeHistory` stack that is being navigated to.
8291
* @returns A boolean indicating whether or not a new route was navigated to.
8392
*
8493
* Invokes history.go passing in the delta between the current route and the target route. https://developer.mozilla.org/en-US/docs/Web/API/History/go
8594
*/
8695
navigate(route: Route): boolean {
87-
let targetIndex: number | null = null;
96+
let targetIndex: number | undefined;
8897

89-
for (let i = 0; i < this.values.length; i += 1) {
90-
if (this.values[i].url === route.url && this.values[i].id === route.id) {
98+
// Loop through the routeHistory stack
99+
for (let i = 0; i < this.routeHistory.length; i += 1) {
100+
// If within the route history, found a match of url & id from the passed in route, update `targetIndex`
101+
if (this.routeHistory[i].url === route.url && this.routeHistory[i].id === route.id) {
91102
targetIndex = i;
92103
}
93104
}
94105

106+
if (typeof targetIndex === 'undefined') {
107+
throw Error('Error at Routes.navigage: targetIndex is undefined');
108+
}
109+
/**
110+
* The position in the window history to which you want to move, relative to the current page. A negative value moves backwards, a positive value moves forwards.
111+
*/
95112
const delta: number = targetIndex - this.current;
96113

114+
// Update the position within routeHistory stack
97115
this.current += delta;
98116

117+
// if delta != 0 => need to navigate to another page
99118
if (delta !== 0) {
119+
// Navigate to that page based on delta steps
100120
window.history.go(delta);
101121
return true;
102122
}

src/backend/timeJump.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import routes from './routes';
33
/* eslint-disable @typescript-eslint/no-unused-vars */
44
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
55
/* eslint-disable max-len */
6+
/* eslint-disable no-param-reassign */
7+
import componentActionsRecord from './masterState';
8+
const circularComponentTable = new Set();
9+
610
/**
711
* This file contains necessary functionality for time-travel feature
812
*
9-
* It exports an anonymous
13+
* Default Export:
1014
* @function timeJump
1115
* @param origin The latest snapshot, linked to the fiber (changes to origin will change app)
1216
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
@@ -15,15 +19,10 @@ import routes from './routes';
1519
* The target snapshot portrays some past state we want to travel to.
1620
* `jump` recursively sets state for any stateful components.
1721
*/
18-
19-
/* eslint-disable no-param-reassign */
20-
import componentActionsRecord from './masterState';
21-
22-
const circularComponentTable = new Set();
23-
export default (mode) => {
22+
export default function timeJump(mode) {
2423
// Recursively change state of tree
2524
// Set the state of the origin tree if the component is stateful
26-
function jump(target) {
25+
function jump(target): void {
2726
if (!target) return;
2827
if (target.state === 'stateless') {
2928
target.children.forEach((child) => jump(child));
@@ -100,4 +99,4 @@ export default (mode) => {
10099
};
101100
}
102101
};
103-
};
102+
}

src/backend/types/backendTypes.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import Tree from '../tree';
66
/**
77
* @type Tree - The snapshot of the current tree
88
* @member tree - {Tree} - The tree structure to send to front end
9-
* @member unfilteredTree - {null} - The current mode (i.e. jumping, time-traveling, or paused)
109
*/
1110
export interface Snapshot {
1211
tree: Tree;
13-
unfilteredTree: null;
1412
}
1513

1614
/**
@@ -245,7 +243,7 @@ export type Fiber = {
245243

246244
/**
247245
* @type FiberRoot - The internal data structure that represents a fiberRootNode or the top-level node of a single component tree
248-
*
246+
*
249247
* FiberRoot data structure has several properties. For Reactime, we only access the `current` property which contains the tree structure made of `fiberNode`. Each `fiberNode` contains a component data in the React component tree.
250248
*/
251249
export type FiberRoot = {

0 commit comments

Comments
 (0)