From 695612c83ba9e0c7b33376d43ccb74fa72e46653 Mon Sep 17 00:00:00 2001 From: "Stephane Lacoin (aka nxmatic)" Date: Thu, 14 Mar 2024 18:51:25 +0100 Subject: [PATCH] BDE-226 bind dynamically methods in constructors --- src/main/browser-store.js | 7 +++++-- src/main/connect-locator.js | 6 +++++- src/main/declarative-net-engine.js | 5 +++++ src/main/designer-live-preview.js | 10 +++++----- src/main/desktop-notifier.js | 6 +++++- src/main/document-browser.js | 11 +++++------ src/main/json-highlighter.js | 6 +++++- src/main/repository-indexer.js | 9 +++++---- src/main/server-connector.js | 22 +++++----------------- src/main/server-locator.js | 9 +++++++++ src/main/service-worker.js | 8 ++++++-- src/main/studio-hot-reloader.js | 6 +++++- 12 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/main/browser-store.js b/src/main/browser-store.js index 20924eeb..906ce76f 100644 --- a/src/main/browser-store.js +++ b/src/main/browser-store.js @@ -32,8 +32,11 @@ class BrowserStore { }; // Bind methods - this.get = this.get.bind(this); - this.set = this.set.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } get(input) { diff --git a/src/main/connect-locator.js b/src/main/connect-locator.js index 4aa5370f..4c52ea10 100644 --- a/src/main/connect-locator.js +++ b/src/main/connect-locator.js @@ -3,7 +3,11 @@ class ConnectLocator { this.worker = worker; // Bind methods - this.url = this.url.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } url(value) { diff --git a/src/main/declarative-net-engine.js b/src/main/declarative-net-engine.js index 61014ac1..e82342cf 100644 --- a/src/main/declarative-net-engine.js +++ b/src/main/declarative-net-engine.js @@ -85,6 +85,11 @@ class DeclarativeNetEngine { this.flushed = this.flushed.bind(this); this.pending = this.pending.bind(this); this.reset = this.reset.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); // Initialize the engine this.reset(); diff --git a/src/main/designer-live-preview.js b/src/main/designer-live-preview.js index 85499c45..fa160ea0 100644 --- a/src/main/designer-live-preview.js +++ b/src/main/designer-live-preview.js @@ -31,11 +31,11 @@ class DesignerLivePreview { this.cleanupFunctions = []; // Bind methods - this.addNewRedirectionsOf = this.addNewRedirectionsOf.bind(this); - this.disable = this.disable.bind(this); - this.enable = this.enable.bind(this); - this.toggle = this.toggle.bind(this); - this.isEnabled = this.isEnabled.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } pushRedirectionsOf(rootUrl, json) { diff --git a/src/main/desktop-notifier.js b/src/main/desktop-notifier.js index cfe3bf25..4685b196 100644 --- a/src/main/desktop-notifier.js +++ b/src/main/desktop-notifier.js @@ -22,7 +22,11 @@ class DesktopNotifier { this.worker = worker; // Bind methods - this.notify = this.notify.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } notify(id, options) { diff --git a/src/main/document-browser.js b/src/main/document-browser.js index 209198b7..b7f7373b 100644 --- a/src/main/document-browser.js +++ b/src/main/document-browser.js @@ -11,12 +11,11 @@ class DocumentBrowser { this.worker = worker; // Bind methods - this.jsonOf = this.jsonOf.bind(this); - this.openDocument = this.openDocument.bind(this); - this.openDocFromId = this.openDocFromId.bind(this); - this.openDocFromPath = this.openDocFromPath.bind(this); - this.doOpenDoc = this.doOpenDoc.bind(this); - this.suggestDocument = this.suggestDocument.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } listenToChromeEvents() { diff --git a/src/main/json-highlighter.js b/src/main/json-highlighter.js index a2b750f5..4b82bf04 100644 --- a/src/main/json-highlighter.js +++ b/src/main/json-highlighter.js @@ -4,7 +4,11 @@ class JsonHighlighter { this._input = ''; // Bind methods - this.input = this.input.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } input(input) { diff --git a/src/main/repository-indexer.js b/src/main/repository-indexer.js index bc440def..db4b4f46 100644 --- a/src/main/repository-indexer.js +++ b/src/main/repository-indexer.js @@ -19,10 +19,11 @@ class RepositoryIndexer { this.worker = worker; // Bind methods - this.withNuxeo = this.withNuxeo.bind(this); - this.reindex = this.reindex.bind(this); - this.reindexNXQL = this.reindexNXQL.bind(this); - this.reindexDocId = this.reindexDocId.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } withNuxeo() { diff --git a/src/main/server-connector.js b/src/main/server-connector.js index d42f06d9..b95c5776 100644 --- a/src/main/server-connector.js +++ b/src/main/server-connector.js @@ -9,17 +9,11 @@ class ServerConnector { this.nuxeo = undefined; // Bind methods - this.onNewServer = this.onNewServer.bind(this); - this.connect = this.connect.bind(this); - this.disconnect = this.disconnect.bind(this); - this.executeOperation = this.executeOperation.bind(this); - this.executeScript = this.executeScript.bind(this); - this.isConnected = this.isConnected.bind(this); - this.handleErrors = this.handleErrors.bind(this); - this.query = this.query.bind(this); - this.restart = this.restart.bind(this); - this.runtimeInfo = this.runtimeInfo.bind(this); - this.withNuxeo = this.withNuxeo.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); // listeners this.onInputChanged = null; @@ -47,12 +41,6 @@ class ServerConnector { this.rootUrl = rootUrl; this.nuxeo = new Nuxeo({ baseURL: this.rootUrl }); this.nuxeo.login() - // .then(() => this.worker.browserStore - // .set({ serverInfo: { rootUrl, nuxeo: this.nuxeo } }) - // .then((store) => { - // console.log(`stored ${JSON.stringify(store)}`); - // resolve(); - // })) .then(() => { chrome.omnibox.onInputChanged.addListener(this.onInputChanged = this.suggestDocument); }) diff --git a/src/main/server-locator.js b/src/main/server-locator.js index 02b35d9f..588f1578 100644 --- a/src/main/server-locator.js +++ b/src/main/server-locator.js @@ -2,6 +2,15 @@ class ServerLocator { constructor(worker) { this.worker = worker; + + // Bind methods + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); + + // ... this.nuxeoUrlOf = (tabInfo) => { // eslint-disable-next-line operator-linebreak // Regular expression pattern diff --git a/src/main/service-worker.js b/src/main/service-worker.js index 470154af..927c38ca 100644 --- a/src/main/service-worker.js +++ b/src/main/service-worker.js @@ -19,8 +19,12 @@ class ServiceWorkerMessageHandler { constructor(worker) { this.worker = worker; - // binds methods to this - this.handle = this.handle.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); + }); } handle(request, sender, sendResponse) { diff --git a/src/main/studio-hot-reloader.js b/src/main/studio-hot-reloader.js index 6d88decc..1df64d66 100644 --- a/src/main/studio-hot-reloader.js +++ b/src/main/studio-hot-reloader.js @@ -44,7 +44,11 @@ class StudioHotReloader { this.worker = worker; // Bind methods - this.reload = this.reload.bind(this); + Object.getOwnPropertyNames(Object.getPrototypeOf(this)) + .filter((prop) => typeof this[prop] === 'function' && prop !== 'constructor') + .forEach((method) => { + this[method] = this[method].bind(this); + }); } dependenciesMismatch(info = []) {