@@ -10,6 +10,7 @@ const external = require('./external')
10
10
const embedded = require ( './embedded' )
11
11
const brave = require ( './brave' )
12
12
const { precache } = require ( '../precache' )
13
+ const { prepareReloadExtensions, WebUiReloader, LocalGatewayReloader, InternalTabReloader } = require ( './reloaders' )
13
14
14
15
// ensure single client at all times, and no overlap between init and destroy
15
16
let client
@@ -61,40 +62,54 @@ async function destroyIpfsClient (browser) {
61
62
}
62
63
}
63
64
64
- function _isWebuiTab ( url ) {
65
- const bundled = ! url . startsWith ( 'http' ) && url . includes ( '/webui/index.html#/' )
66
- const ipns = url . includes ( '/webui.ipfs.io/#/' )
67
- return bundled || ipns
68
- }
69
-
70
- function _isInternalTab ( url , extensionOrigin ) {
71
- return url . startsWith ( extensionOrigin )
72
- }
73
-
74
- async function _reloadIpfsClientDependents ( browser , instance , opts ) {
65
+ /**
66
+ * Reloads pages dependant on ipfs to be online
67
+ *
68
+ * @typedef {embedded|brave|external } Browser
69
+ * @param {Browser } browser
70
+ * @param {import('ipfs-http-client').default } instance
71
+ * @param {Object } opts
72
+ * @param {Array.[InternalTabReloader|LocalGatewayReloader|WebUiReloader]= } reloadExtensions
73
+ * @returns {void }
74
+ */
75
+ async function _reloadIpfsClientDependents (
76
+ browser , instance , opts , reloadExtensions = [ WebUiReloader , LocalGatewayReloader , InternalTabReloader ] ) {
75
77
// online || offline
76
78
if ( browser . tabs && browser . tabs . query ) {
77
79
const tabs = await browser . tabs . query ( { } )
78
80
if ( tabs ) {
79
- const extensionOrigin = browser . runtime . getURL ( '/' )
80
- tabs . forEach ( ( tab ) => {
81
- // detect bundled webui in any of open tabs
82
- if ( _isWebuiTab ( tab . url ) ) {
83
- log ( `reloading webui at ${ tab . url } ` )
84
- browser . tabs . reload ( tab . id )
85
- } else if ( _isInternalTab ( tab . url , extensionOrigin ) ) {
86
- log ( `reloading internal extension page at ${ tab . url } ` )
87
- browser . tabs . reload ( tab . id )
88
- }
89
- } )
81
+ try {
82
+ const reloadExtensionInstances = await prepareReloadExtensions ( reloadExtensions , browser , log )
83
+ // the reload process is async, fire and forget.
84
+ reloadExtensionInstances . forEach ( ext => ext . reload ( tabs ) )
85
+ } catch ( e ) {
86
+ log ( 'Failed to trigger reloaders' )
87
+ }
90
88
}
91
89
}
90
+
92
91
// online only
93
92
if ( client && instance && opts ) {
94
93
// add important data to local ipfs repo for instant load
95
94
setTimeout ( ( ) => precache ( instance , opts ) , 5000 )
96
95
}
97
96
}
98
97
99
- exports . initIpfsClient = initIpfsClient
100
- exports . destroyIpfsClient = destroyIpfsClient
98
+ /**
99
+ * Reloads local gateway pages dependant on ipfs to be online
100
+ *
101
+ * @typedef {embedded|brave|external } Browser
102
+ * @param {Browser } browser
103
+ * @param {import('ipfs-http-client').default } instance
104
+ * @param {Object } opts
105
+ * @returns {void }
106
+ */
107
+ function reloadIpfsClientOfflinePages ( browser , instance , opts ) {
108
+ _reloadIpfsClientDependents ( browser , instance , opts , [ LocalGatewayReloader ] )
109
+ }
110
+
111
+ module . exports = {
112
+ initIpfsClient,
113
+ destroyIpfsClient,
114
+ reloadIpfsClientOfflinePages
115
+ }
0 commit comments