From 5d416e2316f150f8acd4eb53092f040f7dc26708 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 7 Nov 2024 13:22:18 -0500 Subject: [PATCH] refactor: simplify module constructors This was a really small refactor - about 30mins - that moves the concern of common constructor args for modules outside of each individual call. A Context object is now used for common constructor arguments. Some of the values on this object - such as APIOrigin and authToken - are following values on the instance of the Puter class. This means that for some modules it is already possible to eliminate the setAuthToken and setAPIOrigin listeners (out of scope for this commit). Any which remain could eventually be replaced with a listener on the Context object itself. This commit also moves the initSubmodules method to the top of the class so that it's easier for new devs to find, in case they're looking into an issue on a specific module rather than the Puter class itself. --- src/puter-js/src/index.js | 77 +++++++++----------- src/puter-js/src/modules/AI.js | 8 +- src/puter-js/src/modules/Apps.js | 8 +- src/puter-js/src/modules/Auth.js | 8 +- src/puter-js/src/modules/Drivers.js | 9 ++- src/puter-js/src/modules/Email.js | 8 +- src/puter-js/src/modules/FileSystem/index.js | 8 +- src/puter-js/src/modules/Hosting.js | 8 +- src/puter-js/src/modules/KV.js | 8 +- src/puter-js/src/modules/OS.js | 8 +- src/puter-js/src/modules/UI.js | 17 ++++- 11 files changed, 86 insertions(+), 81 deletions(-) diff --git a/src/puter-js/src/index.js b/src/puter-js/src/index.js index 9c9b47a8a..05c550e68 100644 --- a/src/puter-js/src/index.js +++ b/src/puter-js/src/index.js @@ -60,6 +60,38 @@ window.puter = (function() { // debug flag debugMode = false; + /** + * Puter.js Modules + * + * These are the modules you see on docs.puter.com; for example: + * - puter.fs + * - puter.kv + * - puter.ui + * + * initSubmodules is called from the constructor of this class. + */ + initSubmodules = function(){ + // Util + this.util = new Util(); + + this.registerModule('auth', Auth); + this.registerModule('os', OS); + this.registerModule('fs', PuterJSFileSystemModule); + this.registerModule('ui', UI, { + appInstanceID: this.appInstanceID, + parentInstanceID: this.parentInstanceID, + }); + this.registerModule('hosting', Hosting); + this.registerModule('email', Email); + this.registerModule('apps', Apps); + this.registerModule('ai', AI); + this.registerModule('kv', KV); + this.registerModule('drivers', Drivers); + + // Path + this.path = path; + } + // -------------------------------------------- // Constructor // -------------------------------------------- @@ -70,7 +102,8 @@ window.puter = (function() { this.modules_ = []; // "services" in puter.js are used by modules and may interact with each other const context = new putility.libs.context.Context() - .follow(this, ['env', 'util']); + .follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']); + this.services = new putility.system.ServiceManager({ context }); this.context = context; context.services = this.services; @@ -264,50 +297,12 @@ window.puter = (function() { } - registerModule (name, instance) { + registerModule (name, cls, parameters = {}) { + const instance = new cls(this.context, parameters); this.modules_.push(name); this[name] = instance; } - // Initialize submodules - initSubmodules = function(){ - // Util - this.util = new Util(); - - // Auth - this.registerModule('auth', - new Auth(this.authToken, this.APIOrigin, this.appID, this.env)); - // OS - this.registerModule('os', - new OS(this.authToken, this.APIOrigin, this.appID, this.env)); - // FileSystem - this.registerModule('fs', - new PuterJSFileSystemModule(this.authToken, this.APIOrigin, this.appID, this.context)); - // UI - this.registerModule('ui', - new UI(this.appInstanceID, this.parentInstanceID, this.appID, this.env, this.util)); - // Hosting - this.registerModule('hosting', - new Hosting(this.authToken, this.APIOrigin, this.appID, this.env)); - // Email - this.registerModule('email', - new Email(this.authToken, this.APIOrigin, this.appID)); - // Apps - this.registerModule('apps', - new Apps(this.authToken, this.APIOrigin, this.appID, this.env)); - // AI - this.registerModule('ai', - new AI(this.authToken, this.APIOrigin, this.appID, this.env)); - // Key-Value Store - this.registerModule('kv', - new KV(this.authToken, this.APIOrigin, this.appID, this.env)); - // Drivers - this.registerModule('drivers', - new Drivers(this.authToken, this.APIOrigin, this.appID, this.env)); - // Path - this.path = path; - } - updateSubmodules() { // Update submodules with new auth token and API origin for ( const name of this.modules_ ) { diff --git a/src/puter-js/src/modules/AI.js b/src/puter-js/src/modules/AI.js index 6b287021e..84d9b3e47 100644 --- a/src/puter-js/src/modules/AI.js +++ b/src/puter-js/src/modules/AI.js @@ -9,10 +9,10 @@ class AI{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/Apps.js b/src/puter-js/src/modules/Apps.js index 2a9d12b04..c3cf3457d 100644 --- a/src/puter-js/src/modules/Apps.js +++ b/src/puter-js/src/modules/Apps.js @@ -9,10 +9,10 @@ class Apps{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/Auth.js b/src/puter-js/src/modules/Auth.js index 0e4efc928..d218d8ee7 100644 --- a/src/puter-js/src/modules/Auth.js +++ b/src/puter-js/src/modules/Auth.js @@ -14,10 +14,10 @@ class Auth{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/Drivers.js b/src/puter-js/src/modules/Drivers.js index cb29a0964..1d37b4b40 100644 --- a/src/puter-js/src/modules/Drivers.js +++ b/src/puter-js/src/modules/Drivers.js @@ -92,14 +92,15 @@ class Drivers { * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; // Driver-specific this.drivers_ = {}; + // TODO: replace with `context` from constructor and test site login this.context = {}; Object.defineProperty(this.context, 'authToken', { get: () => this.authToken, diff --git a/src/puter-js/src/modules/Email.js b/src/puter-js/src/modules/Email.js index 4f754d335..d1720abbe 100644 --- a/src/puter-js/src/modules/Email.js +++ b/src/puter-js/src/modules/Email.js @@ -9,10 +9,10 @@ class Email{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/FileSystem/index.js b/src/puter-js/src/modules/FileSystem/index.js index eabe0325d..86ef2b558 100644 --- a/src/puter-js/src/modules/FileSystem/index.js +++ b/src/puter-js/src/modules/FileSystem/index.js @@ -63,11 +63,11 @@ export class PuterJSFileSystemModule extends AdvancedBase { * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID, context) { + constructor (context) { super(); - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; this.context = context; // Connect socket. this.initializeSocket(); diff --git a/src/puter-js/src/modules/Hosting.js b/src/puter-js/src/modules/Hosting.js index 349257e2f..a8bc410b6 100644 --- a/src/puter-js/src/modules/Hosting.js +++ b/src/puter-js/src/modules/Hosting.js @@ -10,10 +10,10 @@ class Hosting{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/KV.js b/src/puter-js/src/modules/KV.js index 5fe958757..7cc51f3a3 100644 --- a/src/puter-js/src/modules/KV.js +++ b/src/puter-js/src/modules/KV.js @@ -12,10 +12,10 @@ class KV{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/OS.js b/src/puter-js/src/modules/OS.js index a436e58a3..4b66b0f30 100644 --- a/src/puter-js/src/modules/OS.js +++ b/src/puter-js/src/modules/OS.js @@ -9,10 +9,10 @@ class OS{ * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} appID - ID of the app to use. */ - constructor (authToken, APIOrigin, appID) { - this.authToken = authToken; - this.APIOrigin = APIOrigin; - this.appID = appID; + constructor (context) { + this.authToken = context.authToken; + this.APIOrigin = context.APIOrigin; + this.appID = context.appID; } /** diff --git a/src/puter-js/src/modules/UI.js b/src/puter-js/src/modules/UI.js index 01e2596b7..1b395540a 100644 --- a/src/puter-js/src/modules/UI.js +++ b/src/puter-js/src/modules/UI.js @@ -44,6 +44,15 @@ class AppConnection extends EventListener { this.#isOpen = true; this.#usesSDK = usesSDK; + this.log = globalThis.puter.log.fields({ + category: 'ipc', + }); + this.log.fields({ + constructor_appInstanceID: appInstanceID, + puter_appInstanceID: puter.appInstanceID, + targetAppInstanceID, + }).info(`AppConnection created to ${targetAppInstanceID}`, this); + // TODO: Set this.#puterOrigin to the puter origin window.addEventListener('message', event => { @@ -208,7 +217,7 @@ class UI extends EventListener { return ret; } - constructor (appInstanceID, parentInstanceID, appID, env, util) { + constructor (context, { appInstanceID, parentInstanceID }) { const eventNames = [ 'localeChanged', 'themeChanged', @@ -218,9 +227,9 @@ class UI extends EventListener { this.#eventNames = eventNames; this.appInstanceID = appInstanceID; this.parentInstanceID = parentInstanceID; - this.appID = appID; - this.env = env; - this.util = util; + this.appID = context.appID; + this.env = context.env; + this.util = context.util; if(this.env === 'app'){ this.messageTarget = window.parent;