Skip to content

Commit 720ff2f

Browse files
committed
Fixed: Messaging between sidebar and background
May be the cause of the broken tabs tree and wrong panels on init: #633, #104, #524, #549, #262, #267, #496, #319, ...etc
1 parent afecf35 commit 720ff2f

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

addon/actions/msg.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function initGlobalMessaging() {
3030
*/
3131
function initMessaging() {
3232
browser.runtime.onConnect.addListener(port => {
33+
const sidebarListener = msg => onSidebarMsg(msg, port)
34+
3335
// Setup message handling
3436
let info = JSON.parse(port.name)
3537
if (info.instanceType === 'sidebar') {
@@ -39,9 +41,8 @@ function initMessaging() {
3941
if (this.settings.markWindow) {
4042
browser.windows.update(win.id, { titlePreface: this.settings.markWindowPreface })
4143
}
42-
4344
connectedSidebars[info.windowId] = port
44-
port.onMessage.addListener(onSidebarMsg)
45+
port.onMessage.addListener(sidebarListener)
4546

4647
if (firstSidebarInitHandlers) {
4748
for (let handler of firstSidebarInitHandlers) {
@@ -61,7 +62,7 @@ function initMessaging() {
6162

6263
if (this.settings.markWindow) browser.windows.update(info.windowId, { titlePreface: '' })
6364

64-
targetPort.onMessage.removeListener(onSidebarMsg)
65+
targetPort.onMessage.removeListener(sidebarListener)
6566
delete connectedSidebars[info.windowId]
6667
}
6768
})
@@ -71,8 +72,9 @@ function initMessaging() {
7172
/**
7273
* Handle message from sidebar
7374
*/
74-
function onSidebarMsg(msg) {
75+
function onSidebarMsg(msg, port) {
7576
if (!Actions.initialized) return
77+
if (port) port.postMessage(msg.id)
7678
if (msg.action !== undefined && Actions[msg.action]) {
7779
if (msg.arg) Actions[msg.action](msg.arg)
7880
else if (msg.args) Actions[msg.action](...msg.args)

src/actions/favicons.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ function setFavicon(url, icon) {
3535
if (index > -1) Vue.set(this.state.favUrls, url, index)
3636

3737
if (this.state.private || alreadyCached) return
38-
if (this.state.bg && !this.state.bg.error) {
39-
this.state.bg.postMessage({ action: 'saveFavicon', args: [url, icon] })
40-
} else {
41-
browser.runtime.sendMessage({
42-
action: 'saveFavicon',
43-
instanceType: 'bg',
44-
args: [url, icon],
45-
})
46-
}
38+
this.actions.msgToBg('saveFavicon', [url, icon])
4739
}
4840

4941
export default {

src/sidebar/actions/misc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,42 @@ async function loadWindowInfo() {
1818
/**
1919
* Connect to background script
2020
*/
21+
const msgsWaitingForConfirm = {}
22+
let msgCounter = 1
23+
let reconnectTryCount = 0
2124
function connectToBG() {
2225
const connectInfo = JSON.stringify({
2326
instanceType: this.state.instanceType,
2427
windowId: this.state.windowId,
2528
})
2629
this.bgConnectTryCount = 0
2730
this.state.bg = browser.runtime.connect({ name: connectInfo })
31+
this.state.bg.onMessage.addListener(msg => {
32+
clearTimeout(msgsWaitingForConfirm[msg])
33+
delete msgsWaitingForConfirm[msg]
34+
})
2835
this.state.bg.onDisconnect.addListener(this.handlers.onBgDisconnect)
2936
}
3037

38+
function msgToBg(action, args) {
39+
const id = msgCounter++
40+
41+
if (!this.state.bg || this.state.bg.error) {
42+
browser.runtime.sendMessage({ instanceType: 'bg', action, args })
43+
return
44+
}
45+
46+
this.state.bg.postMessage({ id, action, args })
47+
48+
msgsWaitingForConfirm[id] = setTimeout(() => {
49+
browser.runtime.sendMessage({ instanceType: 'bg', action, args })
50+
delete msgsWaitingForConfirm[id]
51+
52+
this.state.bg = null
53+
if (reconnectTryCount++ < 3) this.actions.connectToBG()
54+
}, 1000)
55+
}
56+
3157
/**
3258
* Show window-select panel
3359
*/
@@ -374,6 +400,7 @@ function finishProgress(notification, delay = 120) {
374400
export default {
375401
loadWindowInfo,
376402
connectToBG,
403+
msgToBg,
377404
chooseWin,
378405
loadPermissions,
379406
getAllWindows,

src/sidebar/actions/tabs.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,7 @@ function saveTabsData(delay = 300) {
538538
return
539539
}
540540

541-
if (this.state.bg && !this.state.bg.error) {
542-
this.state.bg.postMessage({
543-
action: 'saveTabsData',
544-
args: [this.state.windowId, data],
545-
})
546-
} else {
547-
browser.runtime.sendMessage({
548-
instanceType: 'bg',
549-
action: 'saveTabsData',
550-
args: [this.state.windowId, data],
551-
})
552-
}
541+
this.actions.msgToBg('saveTabsData', [this.state.windowId, data])
553542
}, delay)
554543
}
555544

0 commit comments

Comments
 (0)