Skip to content

Commit 5d416e2

Browse files
committed
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.
1 parent 63d6573 commit 5d416e2

File tree

11 files changed

+86
-81
lines changed

11 files changed

+86
-81
lines changed

src/puter-js/src/index.js

+36-41
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ window.puter = (function() {
6060
// debug flag
6161
debugMode = false;
6262

63+
/**
64+
* Puter.js Modules
65+
*
66+
* These are the modules you see on docs.puter.com; for example:
67+
* - puter.fs
68+
* - puter.kv
69+
* - puter.ui
70+
*
71+
* initSubmodules is called from the constructor of this class.
72+
*/
73+
initSubmodules = function(){
74+
// Util
75+
this.util = new Util();
76+
77+
this.registerModule('auth', Auth);
78+
this.registerModule('os', OS);
79+
this.registerModule('fs', PuterJSFileSystemModule);
80+
this.registerModule('ui', UI, {
81+
appInstanceID: this.appInstanceID,
82+
parentInstanceID: this.parentInstanceID,
83+
});
84+
this.registerModule('hosting', Hosting);
85+
this.registerModule('email', Email);
86+
this.registerModule('apps', Apps);
87+
this.registerModule('ai', AI);
88+
this.registerModule('kv', KV);
89+
this.registerModule('drivers', Drivers);
90+
91+
// Path
92+
this.path = path;
93+
}
94+
6395
// --------------------------------------------
6496
// Constructor
6597
// --------------------------------------------
@@ -70,7 +102,8 @@ window.puter = (function() {
70102
this.modules_ = [];
71103
// "services" in puter.js are used by modules and may interact with each other
72104
const context = new putility.libs.context.Context()
73-
.follow(this, ['env', 'util']);
105+
.follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']);
106+
74107
this.services = new putility.system.ServiceManager({ context });
75108
this.context = context;
76109
context.services = this.services;
@@ -264,50 +297,12 @@ window.puter = (function() {
264297

265298
}
266299

267-
registerModule (name, instance) {
300+
registerModule (name, cls, parameters = {}) {
301+
const instance = new cls(this.context, parameters);
268302
this.modules_.push(name);
269303
this[name] = instance;
270304
}
271305

272-
// Initialize submodules
273-
initSubmodules = function(){
274-
// Util
275-
this.util = new Util();
276-
277-
// Auth
278-
this.registerModule('auth',
279-
new Auth(this.authToken, this.APIOrigin, this.appID, this.env));
280-
// OS
281-
this.registerModule('os',
282-
new OS(this.authToken, this.APIOrigin, this.appID, this.env));
283-
// FileSystem
284-
this.registerModule('fs',
285-
new PuterJSFileSystemModule(this.authToken, this.APIOrigin, this.appID, this.context));
286-
// UI
287-
this.registerModule('ui',
288-
new UI(this.appInstanceID, this.parentInstanceID, this.appID, this.env, this.util));
289-
// Hosting
290-
this.registerModule('hosting',
291-
new Hosting(this.authToken, this.APIOrigin, this.appID, this.env));
292-
// Email
293-
this.registerModule('email',
294-
new Email(this.authToken, this.APIOrigin, this.appID));
295-
// Apps
296-
this.registerModule('apps',
297-
new Apps(this.authToken, this.APIOrigin, this.appID, this.env));
298-
// AI
299-
this.registerModule('ai',
300-
new AI(this.authToken, this.APIOrigin, this.appID, this.env));
301-
// Key-Value Store
302-
this.registerModule('kv',
303-
new KV(this.authToken, this.APIOrigin, this.appID, this.env));
304-
// Drivers
305-
this.registerModule('drivers',
306-
new Drivers(this.authToken, this.APIOrigin, this.appID, this.env));
307-
// Path
308-
this.path = path;
309-
}
310-
311306
updateSubmodules() {
312307
// Update submodules with new auth token and API origin
313308
for ( const name of this.modules_ ) {

src/puter-js/src/modules/AI.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class AI{
99
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1010
* @param {string} appID - ID of the app to use.
1111
*/
12-
constructor (authToken, APIOrigin, appID) {
13-
this.authToken = authToken;
14-
this.APIOrigin = APIOrigin;
15-
this.appID = appID;
12+
constructor (context) {
13+
this.authToken = context.authToken;
14+
this.APIOrigin = context.APIOrigin;
15+
this.appID = context.appID;
1616
}
1717

1818
/**

src/puter-js/src/modules/Apps.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Apps{
99
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1010
* @param {string} appID - ID of the app to use.
1111
*/
12-
constructor (authToken, APIOrigin, appID) {
13-
this.authToken = authToken;
14-
this.APIOrigin = APIOrigin;
15-
this.appID = appID;
12+
constructor (context) {
13+
this.authToken = context.authToken;
14+
this.APIOrigin = context.APIOrigin;
15+
this.appID = context.appID;
1616
}
1717

1818
/**

src/puter-js/src/modules/Auth.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class Auth{
1414
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1515
* @param {string} appID - ID of the app to use.
1616
*/
17-
constructor (authToken, APIOrigin, appID) {
18-
this.authToken = authToken;
19-
this.APIOrigin = APIOrigin;
20-
this.appID = appID;
17+
constructor (context) {
18+
this.authToken = context.authToken;
19+
this.APIOrigin = context.APIOrigin;
20+
this.appID = context.appID;
2121
}
2222

2323
/**

src/puter-js/src/modules/Drivers.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ class Drivers {
9292
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
9393
* @param {string} appID - ID of the app to use.
9494
*/
95-
constructor (authToken, APIOrigin, appID) {
96-
this.authToken = authToken;
97-
this.APIOrigin = APIOrigin;
98-
this.appID = appID;
95+
constructor (context) {
96+
this.authToken = context.authToken;
97+
this.APIOrigin = context.APIOrigin;
98+
this.appID = context.appID;
9999

100100
// Driver-specific
101101
this.drivers_ = {};
102102

103+
// TODO: replace with `context` from constructor and test site login
103104
this.context = {};
104105
Object.defineProperty(this.context, 'authToken', {
105106
get: () => this.authToken,

src/puter-js/src/modules/Email.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Email{
99
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1010
* @param {string} appID - ID of the app to use.
1111
*/
12-
constructor (authToken, APIOrigin, appID) {
13-
this.authToken = authToken;
14-
this.APIOrigin = APIOrigin;
15-
this.appID = appID;
12+
constructor (context) {
13+
this.authToken = context.authToken;
14+
this.APIOrigin = context.APIOrigin;
15+
this.appID = context.appID;
1616
}
1717

1818
/**

src/puter-js/src/modules/FileSystem/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ export class PuterJSFileSystemModule extends AdvancedBase {
6363
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
6464
* @param {string} appID - ID of the app to use.
6565
*/
66-
constructor (authToken, APIOrigin, appID, context) {
66+
constructor (context) {
6767
super();
68-
this.authToken = authToken;
69-
this.APIOrigin = APIOrigin;
70-
this.appID = appID;
68+
this.authToken = context.authToken;
69+
this.APIOrigin = context.APIOrigin;
70+
this.appID = context.appID;
7171
this.context = context;
7272
// Connect socket.
7373
this.initializeSocket();

src/puter-js/src/modules/Hosting.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class Hosting{
1010
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1111
* @param {string} appID - ID of the app to use.
1212
*/
13-
constructor (authToken, APIOrigin, appID) {
14-
this.authToken = authToken;
15-
this.APIOrigin = APIOrigin;
16-
this.appID = appID;
13+
constructor (context) {
14+
this.authToken = context.authToken;
15+
this.APIOrigin = context.APIOrigin;
16+
this.appID = context.appID;
1717
}
1818

1919
/**

src/puter-js/src/modules/KV.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class KV{
1212
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1313
* @param {string} appID - ID of the app to use.
1414
*/
15-
constructor (authToken, APIOrigin, appID) {
16-
this.authToken = authToken;
17-
this.APIOrigin = APIOrigin;
18-
this.appID = appID;
15+
constructor (context) {
16+
this.authToken = context.authToken;
17+
this.APIOrigin = context.APIOrigin;
18+
this.appID = context.appID;
1919
}
2020

2121
/**

src/puter-js/src/modules/OS.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class OS{
99
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
1010
* @param {string} appID - ID of the app to use.
1111
*/
12-
constructor (authToken, APIOrigin, appID) {
13-
this.authToken = authToken;
14-
this.APIOrigin = APIOrigin;
15-
this.appID = appID;
12+
constructor (context) {
13+
this.authToken = context.authToken;
14+
this.APIOrigin = context.APIOrigin;
15+
this.appID = context.appID;
1616
}
1717

1818
/**

src/puter-js/src/modules/UI.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class AppConnection extends EventListener {
4444
this.#isOpen = true;
4545
this.#usesSDK = usesSDK;
4646

47+
this.log = globalThis.puter.log.fields({
48+
category: 'ipc',
49+
});
50+
this.log.fields({
51+
constructor_appInstanceID: appInstanceID,
52+
puter_appInstanceID: puter.appInstanceID,
53+
targetAppInstanceID,
54+
}).info(`AppConnection created to ${targetAppInstanceID}`, this);
55+
4756
// TODO: Set this.#puterOrigin to the puter origin
4857

4958
window.addEventListener('message', event => {
@@ -208,7 +217,7 @@ class UI extends EventListener {
208217
return ret;
209218
}
210219

211-
constructor (appInstanceID, parentInstanceID, appID, env, util) {
220+
constructor (context, { appInstanceID, parentInstanceID }) {
212221
const eventNames = [
213222
'localeChanged',
214223
'themeChanged',
@@ -218,9 +227,9 @@ class UI extends EventListener {
218227
this.#eventNames = eventNames;
219228
this.appInstanceID = appInstanceID;
220229
this.parentInstanceID = parentInstanceID;
221-
this.appID = appID;
222-
this.env = env;
223-
this.util = util;
230+
this.appID = context.appID;
231+
this.env = context.env;
232+
this.util = context.util;
224233

225234
if(this.env === 'app'){
226235
this.messageTarget = window.parent;

0 commit comments

Comments
 (0)