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 1cb6d37
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 189 deletions.
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
10 changes: 6 additions & 4 deletions src/main/document-browser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable comma-dangle */
import DOMPurify from 'dompurify';
import ServiceWorkerComponent from './service-worker-component';

const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const pathPattern = /^\//;
const selectFromPattern = /SELECT .* FROM /i;
const webuiPattern = /nuxeo\/ui\/#!\//;

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

// Bind methods
Object.getOwnPropertyNames(Object.getPrototypeOf(this))
Expand All @@ -18,7 +19,8 @@ class DocumentBrowser {
});
}

listenToChromeEvents() {
// eslint-disable-next-line no-unused-vars
activate(self) {
const cleanupFunctions = [];

chrome.omnibox.onInputEntered.addListener(this.openDocument);
Expand Down Expand Up @@ -73,7 +75,7 @@ class DocumentBrowser {
}
return `nxdoc/default/${uid}/view_documents`;
}
this.browserNavigator.loadNewExtensionTab(pathOf(doc.uid), true);
this.tabNavigationHandler.loadNewExtensionTab(pathOf(doc.uid), true);
})
.catch((error) => {
console.log(error);
Expand Down
6 changes: 5 additions & 1 deletion src/main/json-highlighter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class JsonHighlighter {
import ServiceWorkerComponent from './service-worker-component';

class JsonHighlighter extends ServiceWorkerComponent {
// eslint-disable-next-line no-unused-vars
constructor(worker) {
super(worker);

this._input = '';

// Bind methods
Expand Down
2 changes: 1 addition & 1 deletion src/main/main-firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ console.log(`Service Worker: ${buildTime} - ${buildVersion} - ${browserVendor} -
const serviceWorker = new ServiceWorker(developmentMode, buildTime, buildVersion, browserVendor);

// eslint-disable-next-line no-unused-vars
const undoListeningHandler = serviceWorker.listenToChromeEvents();
const undoListeningHandler = serviceWorker.activate();
2 changes: 1 addition & 1 deletion src/main/manifest-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"permissions": [
"storage",
"cookies",
"contextMenus",
"tabs",
"activeTab",
"notifications",
Expand Down
7 changes: 5 additions & 2 deletions src/main/repository-indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

class RepositoryIndexer {
import ServiceWorkerComponent from './service-worker-component';

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

this.waiting = undefined;

// Bind methods
Expand Down
23 changes: 14 additions & 9 deletions src/main/runtime-build-info.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable max-classes-per-file */
import ServiceWorkerComponent from './service-worker-component';

class RuntimeBuildInfo extends ServiceWorkerComponent {
constructor(worker, buildTime, buildVersion, browserVendor) {
super(worker);

class RuntimeBuildInfo {
constructor(buildTime, buildVersion, browserVendor) {
this._developer = 'NOS Team <nuxeo>';
this._browserVendor = browserVendor;
this._buildTime = buildTime;
this._buildVersion = buildVersion;

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

developer() {
Expand All @@ -31,8 +35,9 @@ class RuntimeBuildInfo {
}
}

class DevelopmentMode {
constructor(isEnabled) {
class DevelopmentMode extends ServiceWorkerComponent {
constructor(worker, isEnabled) {
super(worker);
this._isEnabled = isEnabled;

// bind this methods
Expand Down
Loading

0 comments on commit 1cb6d37

Please sign in to comment.