Skip to content

Commit

Permalink
BDE-226 introduce a base class for dynamically introspecting service …
Browse files Browse the repository at this point in the history
…worker components
  • Loading branch information
nxmatic committed Mar 19, 2024
1 parent da69164 commit e883610
Show file tree
Hide file tree
Showing 21 changed files with 525 additions and 375 deletions.
53 changes: 27 additions & 26 deletions src/about/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@ limitations under the License.
import $ from 'jquery';
import ServiceWorkerBridge from '../service-worker-bridge';

const tabActivator = new ServiceWorkerBridge().serverLocatoor;

$(() => {
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'
);
});
});
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'
);
});
}));
15 changes: 8 additions & 7 deletions src/content/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ limitations under the License.

import ServiceWorkerBridge from '../service-worker-bridge';

const serviceWorkerBridge = new ServiceWorkerBridge();

class Content {
constructor() {
this.browserStore = serviceWorkerBridge.browserStore;
constructor(store) {
this.browserStore = store;

// fetch back the server url from the browser store
this.browserStore.get(['server']).then((data) => {
Expand Down Expand Up @@ -60,7 +58,10 @@ class ContentMessageHandler {
}
}

const content = new Content();
const handler = new ContentMessageHandler(content);
new ServiceWorkerBridge().bootstrap().then((worker) => {
const content = new Content(worker.browserStore);
const handler = new ContentMessageHandler(content);

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => handler.handle(request, sender, sendResponse));
chrome.runtime.onMessage
.addListener((request, sender, sendResponse) => handler.handle(request, sender, sendResponse));
});
27 changes: 15 additions & 12 deletions src/json/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import $ from 'jquery';
import jQuery from 'jquery';
import 'bootstrap';
import hljs from 'highlight.js';
import '../../public/styles/mono-blue.css';

import ServiceWorkerBridge from '../service-worker-bridge';

$(() => {
const jsonHighlighter = new ServiceWorkerBridge().jsonHighlighter;
jsonHighlighter.input().then((input) => {
document.getElementById('json-string').textContent = input;
try {
hljs.highlightAll();
} catch (e) {
console.log('Sorry! JSON highlighting only available in Chrome.');
}
});
});
new ServiceWorkerBridge()
.bootstrap()
.then((worker) => jQuery(() => {
worker.jsonHighlighter
.input()
.then((input) => {
document.getElementById('json-string').textContent = input;
try {
hljs.highlightAll();
} catch (e) {
console.log('Sorry! JSON highlighting only available in Chrome.');
}
});
}));
23 changes: 14 additions & 9 deletions src/main/browser-store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
class BrowserStore {
import ServiceWorkerComponent from './service-worker-component';

class BrowserStore extends ServiceWorkerComponent {
constructor(worker, namespace = 'nuxeo-browser-extension.') {
this.worker = worker;
super(worker);

// Define propertie
this.namespace = namespace;

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
.filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor')
.forEach((method) => {
this[method] = this[method].bind(this);
});

// Define functors
this.keysOf = (data) => {
if (Array.isArray(data)) {
return data;
Expand Down Expand Up @@ -31,13 +43,6 @@ class BrowserStore {
// call the function to get the default value
return data[key]();
};

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
.filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor')
.forEach((method) => {
this[method] = this[method].bind(this);
});
}

get(input) {
Expand Down
21 changes: 12 additions & 9 deletions src/main/connect-locator.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import CryptoJS from 'crypto-js';
import ServiceWorkerComponent from './service-worker-component';

class ConnectLocator {
class ConnectLocator extends ServiceWorkerComponent {
constructor(worker) {
this.worker = worker;
// Declare private methods
this.credentialsKeyOf = (location) => {
const hash = CryptoJS
.SHA512(location.toString())
.toString();
return `connect-locator.${hash}`;
};
super(worker);

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
.filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor')
.forEach((method) => {
this[method] = this[method].bind(this);
});

// Declare functors
this.credentialsKeyOf = (location) => {
const hash = CryptoJS
.SHA512(location.toString())
.toString();
return `connect-locator.${hash}`;
};
}

withUrl(input) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/declarative-net-engine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-classes-per-file */
import ServiceWorkerComponent from './service-worker-component';

class BaseRule {
constructor() {
Expand Down Expand Up @@ -146,9 +147,11 @@ class RedirectRule extends BaseRule {
}
}

class DeclarativeNetEngine {
class DeclarativeNetEngine extends ServiceWorkerComponent {
constructor(worker) {
this.worker = worker;
super(worker);

// Define properties
this.rules = new Map();
this.rulesToAdd = [];
this.rulesToRemove = [];
Expand All @@ -160,7 +163,7 @@ class DeclarativeNetEngine {
this[method] = this[method].bind(this);
});

// Private functions
// Define functors
this.requestOf = (rulesToAdd, rulesToRemove) => ({
addRules: rulesToAdd.map((rule) => rule.toJson()),
removeRuleIds: rulesToRemove.map((rule) => rule.hashCode()),
Expand Down
10 changes: 5 additions & 5 deletions src/main/designer-live-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { resolve } from 'nuxeo/lib/deps/promise';
import DeclarativeNetComponents from './declarative-net-engine';
import ServiceWorkerComponent from './service-worker-component';

const BasicAuthenticationHeaderRule = DeclarativeNetComponents.BasicAuthenticationHeaderRule;
const RedirectRule = DeclarativeNetComponents.RedirectRule;

class DesignerLivePreview {
class DesignerLivePreview extends ServiceWorkerComponent {
// eslint-disable-next-line no-unused-vars
constructor(worker) {
this.worker = worker;
super(worker);

// Set defaukt properties for the class
this.undoByProjectNames = new Map();
Expand Down Expand Up @@ -124,12 +124,12 @@ class DesignerLivePreview {
modifyUrlForUIPath(url) {
const fragments = url.pathname.split('/');
if (fragments[2] !== 'ui') {
return resolve(url);
return Promise.resolve(url);
}
fragments.splice(3, 0, ''); // Insert an empty string after 'ui'
const newUrl = new URL(url);
newUrl.pathname = fragments.join('/');
return resolve(newUrl);
return Promise.resolve(newUrl);
}

toggle(projectName) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/desktop-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import ServiceWorkerComponent from './service-worker-component';

const namespacedIdOf = (id) => `nuxeo-web-extension-${id}`;

class DesktopNotifier {
class DesktopNotifier extends ServiceWorkerComponent {
constructor(worker) {
this.worker = worker;
super(worker);

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
Expand Down
Loading

0 comments on commit e883610

Please sign in to comment.