Skip to content

Commit 83fc768

Browse files
committed
Add reconnecting logic to content script (#337)
1 parent add003f commit 83fc768

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

.changeset/shaggy-beds-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solid-devtools/extension": patch
3+
---
4+
5+
Add reconnecting logic to content script (#337)

extension/src/background.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function toggle_action_icon(tab_id: Tab_Id) {
9999

100100
function on_connected(port: Port) {
101101

102-
DEV: {place_log(Place_Name.Background, 'Port connected', port)}
102+
place_log(Place_Name.Background, 'Port connected', port)
103103

104104
switch (port.name) {
105105
case ConnectionName.Popup: {
@@ -164,7 +164,7 @@ function on_connected(port: Port) {
164164

165165
function on_disconnected(port: Port) {
166166

167-
DEV: {place_log(Place_Name.Background, 'Port disconnected', port)}
167+
place_log(Place_Name.Background, 'Port disconnected', port)
168168

169169
switch (port.name) {
170170
case ConnectionName.Popup: {

extension/src/content.ts

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ This script is injected into every page and is responsible for:
1111

1212
import {
1313
Place_Name, ConnectionName,
14-
port_on_message, port_post_message_obj, port_post_message,
14+
port_on_message, port_post_message_obj, message,
1515
window_post_message_obj, window_on_message, window_post_message,
1616
place_error, place_log,
17+
type Message,
1718
} from './shared.ts'
1819

1920
// @ts-expect-error ?script&module query ensures output in ES module format and only import the script path
@@ -66,23 +67,55 @@ function on_loaded() {
6667

6768
const extension_version = chrome.runtime.getManifest().version
6869

69-
const port = chrome.runtime.connect({name: ConnectionName.Content})
70-
70+
let bg_port: chrome.runtime.Port | null = null
7171
let devtools_opened = false
72+
let message_queue: Message[] = []
7273

73-
/* From Background */
74-
port_on_message(port, e => {
75-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
76-
switch (e.kind) {
77-
case 'DevtoolsOpened':
78-
devtools_opened = e.data
79-
window_post_message_obj(e)
80-
break
81-
default:
82-
/* Background -> Client */
83-
window_post_message_obj(e)
74+
let connecting = false
75+
function connect_port() {
76+
if (connecting) return
77+
78+
connecting = true
79+
DEV: {place_log(Place_Name.Content, 'Attempting to connect port...')}
80+
81+
try {
82+
let new_port = chrome.runtime.connect({name: ConnectionName.Content})
83+
bg_port = new_port
84+
DEV: {place_log(Place_Name.Content, 'Port connected successfully')}
85+
86+
// Flush queued messages
87+
for (let m of message_queue.splice(0, message_queue.length)) {
88+
port_post_message_obj(new_port, m)
89+
}
90+
91+
/* From Background */
92+
port_on_message(new_port, e => {
93+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
94+
switch (e.kind) {
95+
case 'DevtoolsOpened':
96+
devtools_opened = e.data
97+
window_post_message_obj(e)
98+
break
99+
default:
100+
/* Background -> Client */
101+
window_post_message_obj(e)
102+
}
103+
})
104+
105+
new_port.onDisconnect.addListener(() => {
106+
if (bg_port === new_port) {
107+
bg_port = null
108+
setTimeout(connect_port, 100)
109+
}
110+
})
111+
} catch (err) {
112+
place_error(Place_Name.Content, 'Failed to connect port:', err)
84113
}
85-
})
114+
115+
connecting = false
116+
}
117+
118+
connect_port()
86119

87120
/* From Client / Detector_Real_World */
88121
window_on_message(e => {
@@ -97,13 +130,20 @@ window_on_message(e => {
97130
'color: #e38b1b',
98131
)
99132

100-
port_post_message(port, 'Versions', {
133+
let versions_message = message('Versions', {
101134
client: e.data.client,
102135
solid: e.data.solid,
103136
extension: extension_version,
104137
client_expected: import.meta.env.EXPECTED_CLIENT,
105138
})
106139

140+
if (bg_port) {
141+
port_post_message_obj(bg_port, versions_message)
142+
} else {
143+
message_queue.push(versions_message)
144+
connect_port()
145+
}
146+
107147
if (devtools_opened) {
108148
window_post_message('DevtoolsOpened', devtools_opened)
109149
}
@@ -112,7 +152,11 @@ window_on_message(e => {
112152
}
113153
default:
114154
/* Client -> Background */
115-
port_post_message_obj(port, e)
155+
if (bg_port) {
156+
port_post_message_obj(bg_port, e)
157+
} else {
158+
message_queue.push(e)
159+
connect_port()
160+
}
116161
}
117162
})
118-

extension/src/shared.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export function port_on_message(port: Port, cb: (e: Message) => void) {
112112
})
113113
}
114114

115+
export function message<K extends keyof Channels>(
116+
kind: K, data: Channels[K],
117+
): Message {
118+
return {kind, data} as any
119+
}
115120
export function port_post_message<K extends keyof Channels>(
116121
port: Port, kind: K, data: Channels[K],
117122
) {

0 commit comments

Comments
 (0)