forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-reloading.ts
32 lines (29 loc) · 1.2 KB
/
auto-reloading.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import signale from 'signale'
import io from 'socket.io'
/**
* Returns a trigger function that notifies the extension to reload itself.
*/
export const initializeServer = (): (() => void) => {
const logger = new signale.Signale({ scope: 'Auto reloading' })
logger.config({ displayTimestamp: true })
// Since this port is hard-coded, it must match background.ts
const socketIOServer = io.listen(8890)
logger.await('Ready for a browser extension to connect')
socketIOServer.on('connect', () => {
logger.info('Browser extension connected')
})
socketIOServer.on('disconnect', () => {
logger.info('Browser extension disconnected')
})
return () => {
if (Object.keys(socketIOServer.clients().connected).length === 0) {
logger.warn('No browser extension has connected yet, so no reload was triggered')
logger.warn("- Make sure it's enabled")
logger.warn("- Make sure it's in developer mode (unpacked extension)")
logger.warn('- Try manually reloading it 🔄')
} else {
logger.info('Triggering a reload of browser extensions')
socketIOServer.emit('file.change', {})
}
}
}