Skip to content

Commit 7674da4

Browse files
committed
feat: Add exit status code to apps
`puter.exit()` now takes a status code, similar to the exit status on desktop OSes. This is passed to the appClosed event, so that eg a parent app can know whether its child app ran successfully.
1 parent b15dc31 commit 7674da4

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

packages/puter-js/src/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,16 @@ window.puter = (function() {
262262
this.updateSubmodules();
263263
}
264264

265-
exit = function() {
265+
exit = function(statusCode = 0) {
266+
if (statusCode && (typeof statusCode !== 'number')) {
267+
console.warn('puter.exit() requires status code to be a number. Treating it as 1');
268+
statusCode = 1;
269+
}
270+
266271
window.parent.postMessage({
267272
msg: "exit",
268273
appInstanceID: this.appInstanceID,
274+
statusCode,
269275
}, '*');
270276
}
271277

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

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class AppConnection extends EventListener {
5454
this.#isOpen = false;
5555
this.emit('close', {
5656
appInstanceID: this.targetAppInstanceID,
57+
statusCode: event.data.statusCode,
5758
});
5859
}
5960
});

src/IPC.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,15 @@ window.addEventListener('message', async (event) => {
12011201
// exit
12021202
//--------------------------------------------------------
12031203
else if(event.data.msg === 'exit'){
1204-
$(window.window_for_app_instance(event.data.appInstanceID)).close({bypass_iframe_messaging: true});
1204+
// Ensure status code is a number. Convert any truthy non-numbers to 1.
1205+
let status_code = event.data.statusCode ?? 0;
1206+
if (status_code && (typeof status_code !== 'number')) {
1207+
status_code = 1;
1208+
}
1209+
1210+
$(window.window_for_app_instance(event.data.appInstanceID)).close({
1211+
bypass_iframe_messaging: true,
1212+
status_code,
1213+
});
12051214
}
1206-
});
1215+
});

src/UI/UIWindow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2887,7 +2887,7 @@ $.fn.close = async function(options) {
28872887
$(`.window[data-parent_uuid="${window_uuid}"]`).close();
28882888

28892889
// notify other apps that we're closing
2890-
window.report_app_closed(window_uuid);
2890+
window.report_app_closed(window_uuid, options.status_code ?? 0);
28912891

28922892
// remove backdrop
28932893
$(this).closest('.window-backdrop').remove();

src/helpers.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3511,7 +3511,7 @@ window.report_app_launched = (instance_id, { uses_sdk = true }) => {
35113511
};
35123512

35133513
// Run any callbacks to say that the app has closed
3514-
window.report_app_closed = (instance_id) => {
3514+
window.report_app_closed = (instance_id, status_code) => {
35153515
const el_window = window.window_for_app_instance(instance_id);
35163516

35173517
// notify parent app, if we have one, that we're closing
@@ -3521,6 +3521,7 @@ window.report_app_closed = (instance_id) => {
35213521
parent.contentWindow.postMessage({
35223522
msg: 'appClosed',
35233523
appInstanceID: instance_id,
3524+
statusCode: status_code ?? 0,
35243525
}, '*');
35253526
}
35263527

@@ -3530,6 +3531,7 @@ window.report_app_closed = (instance_id) => {
35303531
child.contentWindow.postMessage({
35313532
msg: 'appClosed',
35323533
appInstanceID: instance_id,
3534+
statusCode: status_code ?? 0,
35333535
}, '*');
35343536
});
35353537

0 commit comments

Comments
 (0)