Skip to content

Commit

Permalink
fix: logging in AppConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Nov 7, 2024
1 parent 5d416e2 commit 5caa2c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/puter-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ window.puter = (function() {
const context = new putility.libs.context.Context()
.follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']);

context.puter = this;

this.services = new putility.system.ServiceManager({ context });
this.context = context;
context.services = this.services;
Expand Down
57 changes: 28 additions & 29 deletions src/puter-js/src/modules/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ class AppConnection extends EventListener {
// (Closing and close events will still function.)
#usesSDK;

static from (values, { appInstanceID, messageTarget }) {
const connection = new AppConnection(
messageTarget,
appInstanceID,
values.appInstanceID,
values.usesSDK
);
static from (values, context) {
const connection = new AppConnection(context, {
target: values.appInstanceID,
usesSDK: values.usesSDK,
});

// When a connection is established the app is able to
// provide some additional information about itself
Expand All @@ -33,25 +31,25 @@ class AppConnection extends EventListener {
return connection;
}

constructor(messageTarget, appInstanceID, targetAppInstanceID, usesSDK) {
constructor(context, { target, usesSDK }) {
super([
'message', // The target sent us something with postMessage()
'close', // The target app was closed
]);
this.messageTarget = messageTarget;
this.appInstanceID = appInstanceID;
this.targetAppInstanceID = targetAppInstanceID;
this.messageTarget = context.messageTarget;
this.appInstanceID = context.appInstanceID;
this.targetAppInstanceID = target;
this.#isOpen = true;
this.#usesSDK = usesSDK;

this.log = globalThis.puter.log.fields({
this.log = context.puter.log.fields({
category: 'ipc',
});
this.log.fields({
constructor_appInstanceID: appInstanceID,
puter_appInstanceID: puter.appInstanceID,
targetAppInstanceID,
}).info(`AppConnection created to ${targetAppInstanceID}`, this);
cons_source: context.appInstanceID,
source: context.puter.appInstanceID,
target,
}).info(`AppConnection created to ${target}`, this);

// TODO: Set this.#puterOrigin to the puter origin

Expand Down Expand Up @@ -225,6 +223,7 @@ class UI extends EventListener {
];
super(eventNames);
this.#eventNames = eventNames;
this.context = context;
this.appInstanceID = appInstanceID;
this.parentInstanceID = parentInstanceID;
this.appID = context.appID;
Expand All @@ -238,8 +237,17 @@ class UI extends EventListener {
return;
}

// Context to pass to AppConnection instances
this.context = this.context.sub({
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});

if (this.parentInstanceID) {
this.#parentAppConnection = new AppConnection(this.messageTarget, this.appInstanceID, this.parentInstanceID, true);
this.#parentAppConnection = new AppConnection(this.context, {
target: this.parentInstanceID,
usesSDK: true
});
}

// Tell the host environment that this app is using the Puter SDK and is ready to receive messages,
Expand Down Expand Up @@ -481,10 +489,7 @@ class UI extends EventListener {
}
else if ( e.data.msg === 'connection' ) {
e.data.usesSDK = true; // we can safely assume this
const conn = AppConnection.from(e.data, {
appInstanceID: this.appInstanceID,
messageTarget: window.parent,
});
const conn = AppConnection.from(e.data, this.context);
const accept = value => {
this.messageTarget?.postMessage({
$: 'connection-resp',
Expand Down Expand Up @@ -995,10 +1000,7 @@ class UI extends EventListener {
},
});

return AppConnection.from(app_info, {
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});
return AppConnection.from(app_info, this.context);
}

connectToInstance = async function connectToInstance (app_name) {
Expand All @@ -1009,10 +1011,7 @@ class UI extends EventListener {
}
});

return AppConnection.from(app_info, {
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});
return AppConnection.from(app_info, this.context);
}

parentApp() {
Expand Down
27 changes: 10 additions & 17 deletions src/putility/src/libs/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ class ConsoleLogger extends AdvancedBase {
// util: require('util'),

util: {
inspect: v => {
if (typeof v === 'string') return v;
try {
return JSON.stringify(v);
} catch (e) {}
return '' + v;
}
inspect: v => v,
// inspect: v => {
// if (typeof v === 'string') return v;
// try {
// return JSON.stringify(v);
// } catch (e) {}
// return '' + v;
// }
}
}
static PROPERTIES = {
Expand Down Expand Up @@ -113,23 +114,15 @@ class ConsoleLogger extends AdvancedBase {
str += `${l.ansii}[${level.toUpperCase()}]\x1b[0m `;
str += message;

// values
if (values.length) {
str += ' ';
str += values
.map(v => util.inspect(v))
.join(' ');
}

// fields
if (Object.keys(fields).length) {
str += ' ';
str += Object.entries(fields)
.map(([k, v]) => `\n ${k}=${util.inspect(v)}`)
.join(' ');
.join(' ') + '\n';
}

(this.console ?? console)[l.err ? 'error' : 'log'](str);
(this.console ?? console)[l.err ? 'error' : 'log'](str, ...values);
}
}
}
Expand Down

0 comments on commit 5caa2c0

Please sign in to comment.