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.
This commit is contained in:
KernelDeimos 2024-11-07 13:22:18 -05:00
parent 63d6573fba
commit 5d416e2316
11 changed files with 86 additions and 81 deletions

View File

@ -60,6 +60,38 @@ window.puter = (function() {
// debug flag // debug flag
debugMode = false; 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 // Constructor
// -------------------------------------------- // --------------------------------------------
@ -70,7 +102,8 @@ window.puter = (function() {
this.modules_ = []; this.modules_ = [];
// "services" in puter.js are used by modules and may interact with each other // "services" in puter.js are used by modules and may interact with each other
const context = new putility.libs.context.Context() 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.services = new putility.system.ServiceManager({ context });
this.context = context; this.context = context;
context.services = this.services; 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.modules_.push(name);
this[name] = instance; 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() { updateSubmodules() {
// Update submodules with new auth token and API origin // Update submodules with new auth token and API origin
for ( const name of this.modules_ ) { for ( const name of this.modules_ ) {

View File

@ -9,10 +9,10 @@ class AI{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -9,10 +9,10 @@ class Apps{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -14,10 +14,10 @@ class Auth{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -92,14 +92,15 @@ class Drivers {
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
// Driver-specific // Driver-specific
this.drivers_ = {}; this.drivers_ = {};
// TODO: replace with `context` from constructor and test site login
this.context = {}; this.context = {};
Object.defineProperty(this.context, 'authToken', { Object.defineProperty(this.context, 'authToken', {
get: () => this.authToken, get: () => this.authToken,

View File

@ -9,10 +9,10 @@ class Email{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -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} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID, context) { constructor (context) {
super(); super();
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
this.context = context; this.context = context;
// Connect socket. // Connect socket.
this.initializeSocket(); this.initializeSocket();

View File

@ -10,10 +10,10 @@ class Hosting{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -12,10 +12,10 @@ class KV{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -9,10 +9,10 @@ class OS{
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs. * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
* @param {string} appID - ID of the app to use. * @param {string} appID - ID of the app to use.
*/ */
constructor (authToken, APIOrigin, appID) { constructor (context) {
this.authToken = authToken; this.authToken = context.authToken;
this.APIOrigin = APIOrigin; this.APIOrigin = context.APIOrigin;
this.appID = appID; this.appID = context.appID;
} }
/** /**

View File

@ -44,6 +44,15 @@ class AppConnection extends EventListener {
this.#isOpen = true; this.#isOpen = true;
this.#usesSDK = usesSDK; 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 // TODO: Set this.#puterOrigin to the puter origin
window.addEventListener('message', event => { window.addEventListener('message', event => {
@ -208,7 +217,7 @@ class UI extends EventListener {
return ret; return ret;
} }
constructor (appInstanceID, parentInstanceID, appID, env, util) { constructor (context, { appInstanceID, parentInstanceID }) {
const eventNames = [ const eventNames = [
'localeChanged', 'localeChanged',
'themeChanged', 'themeChanged',
@ -218,9 +227,9 @@ class UI extends EventListener {
this.#eventNames = eventNames; this.#eventNames = eventNames;
this.appInstanceID = appInstanceID; this.appInstanceID = appInstanceID;
this.parentInstanceID = parentInstanceID; this.parentInstanceID = parentInstanceID;
this.appID = appID; this.appID = context.appID;
this.env = env; this.env = context.env;
this.util = util; this.util = context.util;
if(this.env === 'app'){ if(this.env === 'app'){
this.messageTarget = window.parent; this.messageTarget = window.parent;