Skip to content

Commit 7c66177

Browse files
RobbyTiptonfscgoldenjoeeparkdavidkim7773khobread
committed
Added routes.ts file which exports an instance of a custom Routes class. The routes class is imported by linkFiber where its addRoute method is invoked before sending a new snapshot to the frontend and by timeJump where its navigate method is invoked during time travel. Improved logic in timeJump that controls when mode.jumping switches back to false. Added url property to tree class.
Co-authored-by: Chris LeBrett <[email protected]> Co-authored-by: Robby Tipton <[email protected]> Co-authored-by: Joseph Park <[email protected]> Co-authored-by: David Kim <[email protected]> Co-authored-by: Kevin HoEun Lee <[email protected]>
1 parent 89c4310 commit 7c66177

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

src/backend/linkFiber.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
import Tree from './tree';
2929
// passes the data down to its components ?
3030
import componentActionsRecord from './masterState';
31+
import routes from './routes';
3132

3233
// 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
3334
// getHooksNames - helper function to grab the getters/setters from `elementType`
@@ -93,6 +94,7 @@ function sendSnapshot(snap: Snapshot, mode: Mode): void {
9394
snap.tree = new Tree('root', 'root');
9495
}
9596
const payload = snap.tree.cleanTreeCopy();
97+
payload.url = routes.addRoute(window.location.href);
9698
// if it's Recoil - run different actions
9799
if (isRecoil) {
98100
// getRecoilState()

src/backend/routes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Routes {
2+
values: string[] = [];
3+
4+
id = 0;
5+
6+
current: number | null = null;
7+
8+
addRoute(url: string): string {
9+
if (this.values[this.values.length - 1] !== url) {
10+
if (this.values.includes(url)) {
11+
this.values.push((url += this.id++));
12+
} else {
13+
this.values.push(url);
14+
}
15+
}
16+
17+
this.current = this.values.length - 1;
18+
return url;
19+
}
20+
21+
navigate(url: string): boolean {
22+
let targetIndex: number | null = null;
23+
for (let i = 0; i < this.values.length; i++) {
24+
if (this.values[i] === url) targetIndex = i;
25+
}
26+
27+
const delta: number = targetIndex - this.current;
28+
29+
this.current += delta;
30+
31+
if (delta !== 0) {
32+
window.history.go(delta);
33+
return true;
34+
}
35+
return false;
36+
}
37+
}
38+
39+
export default new Routes();

src/backend/timeJump.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Console } from 'console';
2+
import routes from './routes';
23

34
/* eslint-disable @typescript-eslint/no-unused-vars */
45
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
@@ -85,9 +86,19 @@ export default (origin, mode) => {
8586
// * Setting mode disables setState from posting messages to window
8687
mode.jumping = true;
8788
if (firstCall) circularComponentTable.clear();
88-
jump(target);
89-
setTimeout(() => {
90-
mode.jumping = false;
91-
}, 100);
89+
const navigating: boolean = routes.navigate(target.url);
90+
if (navigating) {
91+
addEventListener('popstate', event => {
92+
jump(target);
93+
document.body.onmouseover = () => {
94+
mode.jumping = false;
95+
};
96+
});
97+
} else {
98+
jump(target);
99+
document.body.onmouseover = () => {
100+
mode.jumping = false;
101+
};
102+
}
92103
};
93-
};
104+
};

src/backend/tree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Tree {
6161

6262
recoilDomNode: any;
6363

64+
url: string;
65+
6466
// Duplicate names: add a unique number ID
6567
// Create an object 'componentNames' to store each component name as a key and it's frequency of use as its value
6668
// When a new component is made on the tree, check if the new component's name already exists in 'componentNames' (possibly with the .hasOwnProperty method)

0 commit comments

Comments
 (0)