fix: logging in AppConnection

This commit is contained in:
KernelDeimos 2024-11-07 14:26:30 -05:00
parent 5d416e2316
commit 5caa2c0e3a
3 changed files with 40 additions and 46 deletions

View File

@ -104,6 +104,8 @@ window.puter = (function() {
const context = new putility.libs.context.Context() const context = new putility.libs.context.Context()
.follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']); .follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']);
context.puter = this;
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;

View File

@ -18,13 +18,11 @@ class AppConnection extends EventListener {
// (Closing and close events will still function.) // (Closing and close events will still function.)
#usesSDK; #usesSDK;
static from (values, { appInstanceID, messageTarget }) { static from (values, context) {
const connection = new AppConnection( const connection = new AppConnection(context, {
messageTarget, target: values.appInstanceID,
appInstanceID, usesSDK: values.usesSDK,
values.appInstanceID, });
values.usesSDK
);
// When a connection is established the app is able to // When a connection is established the app is able to
// provide some additional information about itself // provide some additional information about itself
@ -33,25 +31,25 @@ class AppConnection extends EventListener {
return connection; return connection;
} }
constructor(messageTarget, appInstanceID, targetAppInstanceID, usesSDK) { constructor(context, { target, usesSDK }) {
super([ super([
'message', // The target sent us something with postMessage() 'message', // The target sent us something with postMessage()
'close', // The target app was closed 'close', // The target app was closed
]); ]);
this.messageTarget = messageTarget; this.messageTarget = context.messageTarget;
this.appInstanceID = appInstanceID; this.appInstanceID = context.appInstanceID;
this.targetAppInstanceID = targetAppInstanceID; this.targetAppInstanceID = target;
this.#isOpen = true; this.#isOpen = true;
this.#usesSDK = usesSDK; this.#usesSDK = usesSDK;
this.log = globalThis.puter.log.fields({ this.log = context.puter.log.fields({
category: 'ipc', category: 'ipc',
}); });
this.log.fields({ this.log.fields({
constructor_appInstanceID: appInstanceID, cons_source: context.appInstanceID,
puter_appInstanceID: puter.appInstanceID, source: context.puter.appInstanceID,
targetAppInstanceID, target,
}).info(`AppConnection created to ${targetAppInstanceID}`, this); }).info(`AppConnection created to ${target}`, this);
// TODO: Set this.#puterOrigin to the puter origin // TODO: Set this.#puterOrigin to the puter origin
@ -225,6 +223,7 @@ class UI extends EventListener {
]; ];
super(eventNames); super(eventNames);
this.#eventNames = eventNames; this.#eventNames = eventNames;
this.context = context;
this.appInstanceID = appInstanceID; this.appInstanceID = appInstanceID;
this.parentInstanceID = parentInstanceID; this.parentInstanceID = parentInstanceID;
this.appID = context.appID; this.appID = context.appID;
@ -238,8 +237,17 @@ class UI extends EventListener {
return; return;
} }
// Context to pass to AppConnection instances
this.context = this.context.sub({
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});
if (this.parentInstanceID) { 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, // Tell the host environment that this app is using the Puter SDK and is ready to receive messages,
@ -481,10 +489,7 @@ class UI extends EventListener {
} }
else if ( e.data.msg === 'connection' ) { else if ( e.data.msg === 'connection' ) {
e.data.usesSDK = true; // we can safely assume this e.data.usesSDK = true; // we can safely assume this
const conn = AppConnection.from(e.data, { const conn = AppConnection.from(e.data, this.context);
appInstanceID: this.appInstanceID,
messageTarget: window.parent,
});
const accept = value => { const accept = value => {
this.messageTarget?.postMessage({ this.messageTarget?.postMessage({
$: 'connection-resp', $: 'connection-resp',
@ -995,10 +1000,7 @@ class UI extends EventListener {
}, },
}); });
return AppConnection.from(app_info, { return AppConnection.from(app_info, this.context);
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});
} }
connectToInstance = async function connectToInstance (app_name) { connectToInstance = async function connectToInstance (app_name) {
@ -1009,10 +1011,7 @@ class UI extends EventListener {
} }
}); });
return AppConnection.from(app_info, { return AppConnection.from(app_info, this.context);
appInstanceID: this.appInstanceID,
messageTarget: this.messageTarget,
});
} }
parentApp() { parentApp() {

View File

@ -73,13 +73,14 @@ class ConsoleLogger extends AdvancedBase {
// util: require('util'), // util: require('util'),
util: { util: {
inspect: v => { inspect: v => v,
if (typeof v === 'string') return v; // inspect: v => {
try { // if (typeof v === 'string') return v;
return JSON.stringify(v); // try {
} catch (e) {} // return JSON.stringify(v);
return '' + v; // } catch (e) {}
} // return '' + v;
// }
} }
} }
static PROPERTIES = { static PROPERTIES = {
@ -113,23 +114,15 @@ class ConsoleLogger extends AdvancedBase {
str += `${l.ansii}[${level.toUpperCase()}]\x1b[0m `; str += `${l.ansii}[${level.toUpperCase()}]\x1b[0m `;
str += message; str += message;
// values
if (values.length) {
str += ' ';
str += values
.map(v => util.inspect(v))
.join(' ');
}
// fields // fields
if (Object.keys(fields).length) { if (Object.keys(fields).length) {
str += ' '; str += ' ';
str += Object.entries(fields) str += Object.entries(fields)
.map(([k, v]) => `\n ${k}=${util.inspect(v)}`) .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);
} }
} }
} }