Skip to content

Commit 18e3d94

Browse files
committed
fixed multi tab issue
1 parent a4542b2 commit 18e3d94

File tree

6 files changed

+90
-33
lines changed

6 files changed

+90
-33
lines changed

src/app/actions/actions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ export const setTab = tab => ({
6767
type: types.SET_TAB,
6868
payload: tab,
6969
});
70+
71+
export const deleteTab = tab => ({
72+
type: types.DELETE_TAB,
73+
payload: tab,
74+
});

src/app/constants/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export const PLAY = 'PLAY';
1111
export const INITIAL_CONNECT = 'INITIAL_CONNECT';
1212
export const NEW_SNAPSHOTS = 'NEW_SNAPSHOTS';
1313
export const SET_TAB = 'SET_TAB';
14+
export const DELETE_TAB = 'DELETE_TAB';

src/app/containers/MainContainer.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TravelContainer from './TravelContainer';
66
import ButtonsContainer from './ButtonsContainer';
77

88
import {
9-
addNewSnapshots, initialConnect, setPort, setTab,
9+
addNewSnapshots, initialConnect, setPort, setTab, deleteTab,
1010
} from '../actions/actions';
1111
import { useStoreContext } from '../store';
1212

@@ -26,6 +26,15 @@ function MainContainer() {
2626
port.onMessage.addListener(message => {
2727
const { action, payload } = message;
2828
switch (action) {
29+
case 'deleteTab': {
30+
console.log('maincontainer', action, payload);
31+
dispatch(deleteTab(payload));
32+
33+
// if (payload === currentTab) dispatch(setTab(Object.keys(tabs)[0]));
34+
35+
break;
36+
}
37+
2938
case 'sendSnapshots': {
3039
if (payload.sourceTab !== currentTab) dispatch(setTab(payload.sourceTab));
3140
// set state with the information received from the background script
@@ -53,7 +62,10 @@ function MainContainer() {
5362
dispatch(setPort(port));
5463
});
5564

56-
if (!npmExists) return <div style={{ color: 'black' }}>please install our npm package in your app</div>;
65+
if (!npmExists) return <div style={{ color: 'black' }}>Please install our npm package in your app</div>;
66+
// if (!tabs[currentTab]) return <div style={{ color: 'black' }}>Please select another tab</div>;
67+
console.log('maincontainer => tabs', tabs);
68+
console.log('maincontainer => currentTab', currentTab);
5769
const { viewIndex, sliderIndex, snapshots } = tabs[currentTab];
5870

5971
// if viewIndex is -1, then use the sliderIndex instead

src/app/reducers/mainReducer.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,21 @@ export default (state, action) => produce(state, draft => {
132132
case types.NEW_SNAPSHOTS: {
133133
const { payload } = action;
134134

135-
Object.keys(payload).forEach(tab => {
135+
Object.keys(tabs).forEach(tab => {
136136
if (tab !== 'sourceTab') {
137-
const { snapshots: newSnaps } = payload[tab];
138-
tabs[tab] = {
139-
...tabs[tab],
140-
...payload[tab],
141-
sliderIndex: newSnaps.length - 1,
142-
};
137+
console.log('reducer => payload', payload);
138+
console.log('reducer => tabs[tab]', tabs[tab]);
139+
console.log('reducer => state', state);
140+
if (!payload[tab]) {
141+
delete tabs[tab];
142+
} else {
143+
const { snapshots: newSnaps } = payload[tab];
144+
tabs[tab] = {
145+
...tabs[tab],
146+
...payload[tab],
147+
sliderIndex: newSnaps.length - 1,
148+
};
149+
}
143150
}
144151
});
145152

@@ -153,6 +160,17 @@ export default (state, action) => produce(state, draft => {
153160
draft.currentTab = action.payload;
154161
break;
155162
}
163+
case types.DELETE_TAB: {
164+
delete draft.tabs[action.payload];
165+
if (draft.currentTab === action.payload) {
166+
// if the deleted tab was set to currentTab, replace currentTab with
167+
// the first tabId within tabs obj
168+
const newCurrentTab = Object.keys(draft.tabs)[0];
169+
draft.currentTab = newCurrentTab;
170+
}
171+
console.log('reducer => delete tab', action.payload);
172+
break;
173+
}
156174
default:
157175
throw new Error(`nonexistent action: ${action.type}`);
158176
}

src/extension/background.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let bg;
1+
const bgArr = [];
22
const tabsObj = {
33
sourceTab: null,
44
};
@@ -18,16 +18,30 @@ function createTabObj(title) {
1818

1919
// establishing connection with devtools
2020
chrome.runtime.onConnect.addListener(port => {
21-
bg = port;
21+
bgArr.push(port);
2222

23+
console.log('bgArr', bgArr);
2324
// send it to devtools as soon as connection to devtools is made
2425
if (Object.keys(tabsObj).length > 0) {
25-
bg.postMessage({
26+
port.postMessage({
2627
action: 'initialConnectSnapshots',
2728
payload: tabsObj,
2829
});
2930
}
3031

32+
port.onDisconnect.addListener(e => {
33+
console.log('port disconnected => e', e);
34+
console.log('port disconnected => port', port);
35+
for (let i = 0; i < bgArr.length; i += 1) {
36+
if (bgArr[i] === e) {
37+
console.log('inside if statement');
38+
bgArr.splice(i, 1);
39+
break;
40+
}
41+
}
42+
console.log(bgArr);
43+
});
44+
3145
// receive snapshot from devtools and send it to contentScript
3246
port.onMessage.addListener(msg => {
3347
// ---------------------------------------------------------------
@@ -95,11 +109,11 @@ chrome.runtime.onMessage.addListener((request, sender) => {
95109
tabsObj[tabId].firstSnapshot = false;
96110
// don't add anything to snapshot storage if mode is persisting for the initial snapshot
97111
if (!persist) tabsObj[tabId].snapshots.push(request.payload);
98-
if (bg) {
99-
bg.postMessage({
112+
if (bgArr.length > 0) {
113+
bgArr.forEach(bg => bg.postMessage({
100114
action: 'initialConnectSnapshots',
101115
payload: tabsObj,
102-
});
116+
}));
103117
}
104118
break;
105119
}
@@ -108,30 +122,28 @@ chrome.runtime.onMessage.addListener((request, sender) => {
108122
tabsObj.sourceTab = tabId;
109123

110124
// send message to devtools
111-
if (bg) {
112-
bg.postMessage({
125+
if (bgArr.length > 0) {
126+
bgArr.forEach(bg => bg.postMessage({
113127
action: 'sendSnapshots',
114128
payload: tabsObj,
115-
});
129+
}));
116130
}
117131
break;
118132
default:
119133
break;
120134
}
121135
});
122136

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-
134137
// when tab is closed, remove the tabid from the tabsObj
135138
chrome.tabs.onRemoved.addListener(tabId => {
139+
// after deleting the tab, send the updated tabs object to devtools
140+
if (bgArr.length > 0) {
141+
console.log('background => delete tab');
142+
bgArr.forEach(bg => bg.postMessage({
143+
action: 'deleteTab',
144+
payload: tabId,
145+
}));
146+
}
147+
136148
delete tabsObj[tabId];
137149
});

src/extension/contentScript.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
let firstMessage = true;
2+
13
// listening for messages from npm package
24
window.addEventListener('message', msg => {
5+
// window listener picks up the message it sends, so we should filter
6+
// messages sent by contentscript
7+
if (msg.data.action !== 'contentScriptStarted' && firstMessage) {
8+
// since contentScript is run everytime a page is refreshed
9+
// tell the background script that the tab has reloaded
10+
console.log('before sending tabReload');
11+
chrome.runtime.sendMessage({ action: 'tabReload' });
12+
firstMessage = false;
13+
}
14+
315
// post initial Message to npm package
416
const { action } = msg.data;
17+
console.log('before sending recordsnap');
518
if (action === 'recordSnap') chrome.runtime.sendMessage(msg.data);
619
});
720

@@ -20,8 +33,4 @@ chrome.runtime.onMessage.addListener(request => {
2033
}
2134
});
2235

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-
2736
window.postMessage({ action: 'contentScriptStarted' });

0 commit comments

Comments
 (0)