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
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_ ) {

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} 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;
}
/**

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} 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;
}
/**

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} 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;
}
/**

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} 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,

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} 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;
}
/**

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} 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();

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} 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;
}
/**

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} 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;
}
/**

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} 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;
}
/**

View File

@ -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;