Skip to content

Commit 5caa2c0

Browse files
committed
fix: logging in AppConnection
1 parent 5d416e2 commit 5caa2c0

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

src/puter-js/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ window.puter = (function() {
104104
const context = new putility.libs.context.Context()
105105
.follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']);
106106

107+
context.puter = this;
108+
107109
this.services = new putility.system.ServiceManager({ context });
108110
this.context = context;
109111
context.services = this.services;

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

+28-29
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ class AppConnection extends EventListener {
1818
// (Closing and close events will still function.)
1919
#usesSDK;
2020

21-
static from (values, { appInstanceID, messageTarget }) {
22-
const connection = new AppConnection(
23-
messageTarget,
24-
appInstanceID,
25-
values.appInstanceID,
26-
values.usesSDK
27-
);
21+
static from (values, context) {
22+
const connection = new AppConnection(context, {
23+
target: values.appInstanceID,
24+
usesSDK: values.usesSDK,
25+
});
2826

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

36-
constructor(messageTarget, appInstanceID, targetAppInstanceID, usesSDK) {
34+
constructor(context, { target, usesSDK }) {
3735
super([
3836
'message', // The target sent us something with postMessage()
3937
'close', // The target app was closed
4038
]);
41-
this.messageTarget = messageTarget;
42-
this.appInstanceID = appInstanceID;
43-
this.targetAppInstanceID = targetAppInstanceID;
39+
this.messageTarget = context.messageTarget;
40+
this.appInstanceID = context.appInstanceID;
41+
this.targetAppInstanceID = target;
4442
this.#isOpen = true;
4543
this.#usesSDK = usesSDK;
4644

47-
this.log = globalThis.puter.log.fields({
45+
this.log = context.puter.log.fields({
4846
category: 'ipc',
4947
});
5048
this.log.fields({
51-
constructor_appInstanceID: appInstanceID,
52-
puter_appInstanceID: puter.appInstanceID,
53-
targetAppInstanceID,
54-
}).info(`AppConnection created to ${targetAppInstanceID}`, this);
49+
cons_source: context.appInstanceID,
50+
source: context.puter.appInstanceID,
51+
target,
52+
}).info(`AppConnection created to ${target}`, this);
5553

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

@@ -225,6 +223,7 @@ class UI extends EventListener {
225223
];
226224
super(eventNames);
227225
this.#eventNames = eventNames;
226+
this.context = context;
228227
this.appInstanceID = appInstanceID;
229228
this.parentInstanceID = parentInstanceID;
230229
this.appID = context.appID;
@@ -238,8 +237,17 @@ class UI extends EventListener {
238237
return;
239238
}
240239

240+
// Context to pass to AppConnection instances
241+
this.context = this.context.sub({
242+
appInstanceID: this.appInstanceID,
243+
messageTarget: this.messageTarget,
244+
});
245+
241246
if (this.parentInstanceID) {
242-
this.#parentAppConnection = new AppConnection(this.messageTarget, this.appInstanceID, this.parentInstanceID, true);
247+
this.#parentAppConnection = new AppConnection(this.context, {
248+
target: this.parentInstanceID,
249+
usesSDK: true
250+
});
243251
}
244252

245253
// 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 {
481489
}
482490
else if ( e.data.msg === 'connection' ) {
483491
e.data.usesSDK = true; // we can safely assume this
484-
const conn = AppConnection.from(e.data, {
485-
appInstanceID: this.appInstanceID,
486-
messageTarget: window.parent,
487-
});
492+
const conn = AppConnection.from(e.data, this.context);
488493
const accept = value => {
489494
this.messageTarget?.postMessage({
490495
$: 'connection-resp',
@@ -995,10 +1000,7 @@ class UI extends EventListener {
9951000
},
9961001
});
9971002

998-
return AppConnection.from(app_info, {
999-
appInstanceID: this.appInstanceID,
1000-
messageTarget: this.messageTarget,
1001-
});
1003+
return AppConnection.from(app_info, this.context);
10021004
}
10031005

10041006
connectToInstance = async function connectToInstance (app_name) {
@@ -1009,10 +1011,7 @@ class UI extends EventListener {
10091011
}
10101012
});
10111013

1012-
return AppConnection.from(app_info, {
1013-
appInstanceID: this.appInstanceID,
1014-
messageTarget: this.messageTarget,
1015-
});
1014+
return AppConnection.from(app_info, this.context);
10161015
}
10171016

10181017
parentApp() {

src/putility/src/libs/log.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ class ConsoleLogger extends AdvancedBase {
7373
// util: require('util'),
7474

7575
util: {
76-
inspect: v => {
77-
if (typeof v === 'string') return v;
78-
try {
79-
return JSON.stringify(v);
80-
} catch (e) {}
81-
return '' + v;
82-
}
76+
inspect: v => v,
77+
// inspect: v => {
78+
// if (typeof v === 'string') return v;
79+
// try {
80+
// return JSON.stringify(v);
81+
// } catch (e) {}
82+
// return '' + v;
83+
// }
8384
}
8485
}
8586
static PROPERTIES = {
@@ -113,23 +114,15 @@ class ConsoleLogger extends AdvancedBase {
113114
str += `${l.ansii}[${level.toUpperCase()}]\x1b[0m `;
114115
str += message;
115116

116-
// values
117-
if (values.length) {
118-
str += ' ';
119-
str += values
120-
.map(v => util.inspect(v))
121-
.join(' ');
122-
}
123-
124117
// fields
125118
if (Object.keys(fields).length) {
126119
str += ' ';
127120
str += Object.entries(fields)
128121
.map(([k, v]) => `\n ${k}=${util.inspect(v)}`)
129-
.join(' ');
122+
.join(' ') + '\n';
130123
}
131124

132-
(this.console ?? console)[l.err ? 'error' : 'log'](str);
125+
(this.console ?? console)[l.err ? 'error' : 'log'](str, ...values);
133126
}
134127
}
135128
}

0 commit comments

Comments
 (0)