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.
This commit is contained in:
Sam Atkins
2024-05-23 12:54:06 +01:00
parent b15dc316d3
commit 7674da4cd2
5 changed files with 23 additions and 5 deletions

View File

@@ -262,10 +262,16 @@ window.puter = (function() {
this.updateSubmodules();
}
exit = function() {
exit = function(statusCode = 0) {
if (statusCode && (typeof statusCode !== 'number')) {
console.warn('puter.exit() requires status code to be a number. Treating it as 1');
statusCode = 1;
}
window.parent.postMessage({
msg: "exit",
appInstanceID: this.appInstanceID,
statusCode,
}, '*');
}

View File

@@ -54,6 +54,7 @@ class AppConnection extends EventListener {
this.#isOpen = false;
this.emit('close', {
appInstanceID: this.targetAppInstanceID,
statusCode: event.data.statusCode,
});
}
});

View File

@@ -1201,6 +1201,15 @@ window.addEventListener('message', async (event) => {
// exit
//--------------------------------------------------------
else if(event.data.msg === 'exit'){
$(window.window_for_app_instance(event.data.appInstanceID)).close({bypass_iframe_messaging: true});
// Ensure status code is a number. Convert any truthy non-numbers to 1.
let status_code = event.data.statusCode ?? 0;
if (status_code && (typeof status_code !== 'number')) {
status_code = 1;
}
$(window.window_for_app_instance(event.data.appInstanceID)).close({
bypass_iframe_messaging: true,
status_code,
});
}
});
});

View File

@@ -2887,7 +2887,7 @@ $.fn.close = async function(options) {
$(`.window[data-parent_uuid="${window_uuid}"]`).close();
// notify other apps that we're closing
window.report_app_closed(window_uuid);
window.report_app_closed(window_uuid, options.status_code ?? 0);
// remove backdrop
$(this).closest('.window-backdrop').remove();

View File

@@ -3511,7 +3511,7 @@ window.report_app_launched = (instance_id, { uses_sdk = true }) => {
};
// Run any callbacks to say that the app has closed
window.report_app_closed = (instance_id) => {
window.report_app_closed = (instance_id, status_code) => {
const el_window = window.window_for_app_instance(instance_id);
// notify parent app, if we have one, that we're closing
@@ -3521,6 +3521,7 @@ window.report_app_closed = (instance_id) => {
parent.contentWindow.postMessage({
msg: 'appClosed',
appInstanceID: instance_id,
statusCode: status_code ?? 0,
}, '*');
}
@@ -3530,6 +3531,7 @@ window.report_app_closed = (instance_id) => {
child.contentWindow.postMessage({
msg: 'appClosed',
appInstanceID: instance_id,
statusCode: status_code ?? 0,
}, '*');
});