Skip to content

Commit

Permalink
refactor: simplify module constructors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KernelDeimos committed Nov 7, 2024
1 parent 63d6573 commit 5d416e2
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 81 deletions.
77 changes: 36 additions & 41 deletions src/puter-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// --------------------------------------------
Expand All @@ -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;
Expand Down Expand Up @@ -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_ ) {
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/AI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/Apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/puter-js/src/modules/Drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/Email.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/FileSystem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/Hosting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/KV.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/puter-js/src/modules/OS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/puter-js/src/modules/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -208,7 +217,7 @@ class UI extends EventListener {
return ret;
}

constructor (appInstanceID, parentInstanceID, appID, env, util) {
constructor (context, { appInstanceID, parentInstanceID }) {
const eventNames = [
'localeChanged',
'themeChanged',
Expand All @@ -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;
Expand Down

0 comments on commit 5d416e2

Please sign in to comment.