Skip to content

Commit 0c7bac4

Browse files
committed
ready for merge
1 parent 8a9acfd commit 0c7bac4

File tree

6 files changed

+60
-69
lines changed

6 files changed

+60
-69
lines changed

package/timeJump.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// traverses given tree by accessing children through coords array
22
function traverseTree(tree, coords) {
33
let curr = tree;
4-
coords.forEach((coord) => {
4+
coords.forEach(coord => {
55
curr = curr.children[coord];
66
});
77
return curr;
@@ -21,8 +21,7 @@ module.exports = (origin, mode) => {
2121
});
2222
}
2323

24-
25-
return (target) => {
24+
return target => {
2625
// setting mode disables setState from posting messages to window
2726
mode.jumping = true;
2827
jump(target);

src/app/components/SwitchApp.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const SwitchAppDropdown = () => {
99
const tabsArray = [];
1010

1111
Object.keys(tabs).forEach(tab => {
12-
if (tab !== 'sourceTab') tabsArray.push({ value: tab, label: tabs[tab].title });
12+
tabsArray.push({ value: tab, label: tabs[tab].title });
1313
});
1414

1515
const currTab = {

src/app/containers/MainContainer.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function MainContainer() {
2424

2525
// listen for a message containing snapshots from the background script
2626
port.onMessage.addListener(message => {
27-
const { action, payload } = message;
27+
const { action, payload, sourceTab } = message;
2828
switch (action) {
2929
case 'sendSnapshots': {
30-
if (payload.sourceTab !== currentTab) dispatch(setTab(payload.sourceTab));
30+
dispatch(setTab(sourceTab));
3131
// set state with the information received from the background script
3232
dispatch(addNewSnapshots(payload));
3333
break;
@@ -53,6 +53,8 @@ function MainContainer() {
5353
dispatch(setPort(port));
5454
});
5555

56+
console.log(store);
57+
5658
if (!npmExists) return <div style={{ color: 'black' }}>please install our npm package in your app</div>;
5759
const { viewIndex, sliderIndex, snapshots } = tabs[currentTab];
5860

src/app/reducers/mainReducer.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,42 +111,36 @@ export default (state, action) => produce(state, draft => {
111111
const { payload } = action;
112112
Object.keys(payload).forEach(tab => {
113113
// check if tab exists in memory
114-
if (!tabs[tab]) {
115-
// add new tab
116-
tabs[tab] = {
117-
...payload[tab],
118-
sliderIndex: 0,
119-
viewIndex: -1,
120-
intervalId: null,
121-
playing: false,
122-
};
123-
}
114+
// add new tab
115+
tabs[tab] = {
116+
...payload[tab],
117+
sliderIndex: 0,
118+
viewIndex: -1,
119+
intervalId: null,
120+
playing: false,
121+
};
124122
});
125123

126124
// only set first tab if current tab is non existent
127125
const firstTab = parseInt(Object.keys(payload)[0], 10);
128-
draft.currentTab = currentTab === null ? firstTab : currentTab;
129-
126+
if (currentTab === undefined || currentTab === null) draft.currentTab = firstTab;
130127
break;
131128
}
132129
case types.NEW_SNAPSHOTS: {
133130
const { payload } = action;
134131

135132
Object.keys(payload).forEach(tab => {
136-
if (tab !== 'sourceTab') {
137-
const { snapshots: newSnaps } = payload[tab];
138-
tabs[tab] = {
139-
...tabs[tab],
140-
...payload[tab],
141-
sliderIndex: newSnaps.length - 1,
142-
};
143-
}
133+
const { snapshots: newSnaps } = payload[tab];
134+
tabs[tab] = {
135+
...tabs[tab],
136+
...payload[tab],
137+
sliderIndex: newSnaps.length - 1,
138+
};
144139
});
145140

146141
// only set first tab if current tab is non existent
147142
const firstTab = parseInt(Object.keys(payload)[0], 10);
148-
draft.currentTab = currentTab === null ? firstTab : currentTab;
149-
143+
if (currentTab === undefined || currentTab === null) draft.currentTab = firstTab;
150144
break;
151145
}
152146
case types.SET_TAB: {

src/extension/background.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let bg;
2-
const tabsObj = {
3-
sourceTab: null,
4-
};
2+
const tabsObj = {};
3+
4+
const reloaded = {};
55

66
function createTabObj(title) {
77
return {
@@ -20,13 +20,10 @@ function createTabObj(title) {
2020
chrome.runtime.onConnect.addListener(port => {
2121
bg = port;
2222

23-
// send it to devtools as soon as connection to devtools is made
24-
if (Object.keys(tabsObj).length > 0) {
25-
bg.postMessage({
26-
action: 'initialConnectSnapshots',
27-
payload: tabsObj,
28-
});
29-
}
23+
bg.postMessage({
24+
action: 'initialConnectSnapshots',
25+
payload: tabsObj,
26+
});
3027

3128
// receive snapshot from devtools and send it to contentScript
3229
port.onMessage.addListener(msg => {
@@ -74,7 +71,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
7471
// Filter out tabs that don't have react-time-travel
7572
if (action === 'tabReload' || action === 'recordSnap') {
7673
isReactTimeTravel = true;
77-
}
74+
} else return;
7875

7976
// everytime we get a new tabid, add it to the object
8077
if (isReactTimeTravel && !(tabId in tabsObj)) {
@@ -84,53 +81,45 @@ chrome.runtime.onMessage.addListener((request, sender) => {
8481
const { persist } = tabsObj[tabId].mode;
8582

8683
switch (action) {
87-
case 'tabReload':
84+
case 'tabReload': {
8885
tabsObj[tabId].firstSnapshot = true;
8986
tabsObj[tabId].mode.locked = false;
9087
tabsObj[tabId].mode.paused = false;
91-
if (!persist) tabsObj[tabId].snapshots = [];
88+
if (!persist) {
89+
tabsObj[tabId].snapshots.splice(1);
90+
reloaded[tabId] = true;
91+
}
92+
93+
bg.postMessage({
94+
action: 'initialConnectSnapshots',
95+
payload: tabsObj,
96+
});
97+
9298
break;
93-
case 'recordSnap':
99+
}
100+
case 'recordSnap': {
101+
const sourceTab = tabId;
102+
94103
if (tabsObj[tabId].firstSnapshot) {
95104
tabsObj[tabId].firstSnapshot = false;
96-
// don't add anything to snapshot storage if mode is persisting for the initial snapshot
97-
if (!persist) tabsObj[tabId].snapshots.push(request.payload);
98-
if (bg) {
99-
bg.postMessage({
100-
action: 'initialConnectSnapshots',
101-
payload: tabsObj,
102-
});
103-
}
104105
break;
105106
}
106-
107107
tabsObj[tabId].snapshots.push(request.payload);
108-
tabsObj.sourceTab = tabId;
109-
110108
// send message to devtools
111109
if (bg) {
112110
bg.postMessage({
113111
action: 'sendSnapshots',
114112
payload: tabsObj,
113+
sourceTab,
115114
});
116115
}
117116
break;
117+
}
118118
default:
119119
break;
120120
}
121121
});
122122

123-
// chrome.tabs.onActivated.addListener((info) => {
124-
// console.log('this is activated', info);
125-
// if (bg) {
126-
// console.log('hello', bg);
127-
// bg.postMessage({
128-
// action: 'activatedTab',
129-
// payload: info.tabId,
130-
// });
131-
// }
132-
// });
133-
134123
// when tab is closed, remove the tabid from the tabsObj
135124
chrome.tabs.onRemoved.addListener(tabId => {
136125
delete tabsObj[tabId];

src/extension/contentScript.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
let reloaded = true;
2+
13
// listening for messages from npm package
24
window.addEventListener('message', msg => {
35
// post initial Message to npm package
46
const { action } = msg.data;
5-
if (action === 'recordSnap') chrome.runtime.sendMessage(msg.data);
7+
if (action === 'recordSnap') {
8+
if (reloaded) {
9+
reloaded = false;
10+
// since contentScript is run everytime a page is refreshed
11+
// tell the background script that the tab has reloaded
12+
chrome.runtime.sendMessage({ action: 'tabReload' });
13+
}
14+
15+
chrome.runtime.sendMessage(msg.data);
16+
}
617
});
718

819
// listening for messages from background.js
@@ -20,8 +31,4 @@ chrome.runtime.onMessage.addListener(request => {
2031
}
2132
});
2233

23-
// since contentScript is run everytime a page is refreshed
24-
// tell the background script that the tab has reloaded
25-
chrome.runtime.sendMessage({ action: 'tabReload' });
26-
2734
window.postMessage({ action: 'contentScriptStarted' });

0 commit comments

Comments
 (0)