Skip to content

Commit

Permalink
BDE-226 shared service worker bridge bootstrap logic
Browse files Browse the repository at this point in the history
- allow to bind a service workspace bridge and
    to bootstrap an entry point with.
  - used in main and about popup pages
  • Loading branch information
nxmatic committed Mar 26, 2024
1 parent 995758e commit 61ae8ef
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<div id="version" class="version"></div>
<div id="copyright">&#169; 2016-</div>
</div>
<div class ="nxrow about">
<div class="license">
<div class="nxrow license">
<div id="licenses" class="license">
<div>
Nuxeo Extension is released under the
</div>
Expand Down
55 changes: 29 additions & 26 deletions src/about/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,32 @@ import $ from 'jquery';
import ServiceWorkerBridge from '../service-worker-bridge';

new ServiceWorkerBridge()
.bootstrap()
.then((worker) => worker.tabActivator)
.then((tabActivator) => $(() => {
const date = new Date().getFullYear();
$('#copyright').append(`${date} Nuxeo`);
$('#apache').click(() => {
tabActivator.loadNewExtensionTab(
'http://www.apache.org/licenses/LICENSE-2.0'
);
});
$('#feedback').click(() => {
tabActivator.loadNewExtensionTab(
'https://portal.prodpad.com/40c295d6-739d-11e7-9e52-06df22ffaf6f'
);
});
$('#apache').click(() => {
tabActivator.loadNewExtensionTab(
'http://www.apache.org/licenses/LICENSE-2.0'
);
});
$('#feedback').click(() => {
tabActivator.loadNewExtensionTab(
'https://portal.prodpad.com/40c295d6-739d-11e7-9e52-06df22ffaf6f'
);
});
}));
.bootstrap({
name: 'about',
entrypoint: (worker) => {
const tabActivator = worker.tabActivator;
const date = new Date().getFullYear();
$('#copyright').append(`${date} Nuxeo`);
$('#version').append(chrome.runtime.getManifest().version_name);
$('#apache').click(() => {
tabActivator.loadNewExtensionTab(
'http://www.apache.org/licenses/LICENSE-2.0'
);
});
$('#feedback').click(() => {
tabActivator.loadNewExtensionTab(
'https://portal.prodpad.com/40c295d6-739d-11e7-9e52-06df22ffaf6f'
);
});
$('#apache').click(() => {
tabActivator.loadNewExtensionTab(
'http://www.apache.org/licenses/LICENSE-2.0'
);
});
$('#feedback').click(() => {
tabActivator.loadNewExtensionTab(
'https://portal.prodpad.com/40c295d6-739d-11e7-9e52-06df22ffaf6f'
);
});
}
});
3 changes: 2 additions & 1 deletion src/popup/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--
Copyright 2016-2024 Nuxeo
Expand All @@ -17,7 +18,7 @@
limitations under the License.
-->
<title>Nuxeo Dev Tools</title>
<link rel=stylesheet href=/styles/main.css>
<link rel="stylesheet" href="/styles/main.css">
<style>
body {
display: none;
Expand Down
25 changes: 5 additions & 20 deletions src/popup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,9 @@ function loadPage(worker) {
});
return Promise
.all(pendingPromises)
.then(() => $('body').show())
.then(() => {
$('body').show(); // show the body after all promises are resolved
})
.then(() => worker)
.catch((error) => console.error(error));
})
Expand All @@ -902,22 +904,5 @@ function loadPage(worker) {
}

new ServiceWorkerBridge()
.bootstrap()
.then((worker) => {
worker.developmentMode
.asPromise()
.then(() => {
// can be used in development mode from the console for now
worker.reloadPopup = () => worker.asPromise().then(loadPage);
// Check if 'window' is defined, otherwise use 'window'
// eslint-disable-next-line no-restricted-globals, no-undef
const globalScope = typeof self !== 'undefined' ? self : window;
globalScope.nuxeoWebExtension = worker;
})
.catch((error) => console.error(error));
return worker;
})
.then(loadPage)
.catch((e) => {
console.error(e);
});
.bootstrap({ name: 'reloadPopup', entrypoint: loadPage })
.catch((error) => console.error(error));
24 changes: 22 additions & 2 deletions src/service-worker-bridge.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable comma-dangle */

class ServiceWorkerBridge {
bootstrap() {
bootstrap({ name, entrypoint }) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(
{
Expand All @@ -21,7 +21,27 @@ class ServiceWorkerBridge {
return resolve(this);
}
);
});
})
// eslint-disable-next-line no-return-assign, no-sequences
.then((worker) => {
worker[name] = entrypoint;
return worker.developmentMode
.asPromise()
.then(() => {
// Check if 'window' is defined, otherwise use 'window'
// eslint-disable-next-line no-restricted-globals, no-undef
const globalScope = typeof self !== 'undefined' ? self : window;
// eslint-disable-next-line no-return-assign
return globalScope.nuxeoWebExtension = worker;
})
// eslint-disable-next-line no-sequences
.catch(() => (worker));
})
.then((worker) => (
// eslint-disable-next-line no-sequences
entrypoint(worker),
worker
));
}

createProxies(components) {
Expand Down

0 comments on commit 61ae8ef

Please sign in to comment.