Skip to content

Commit

Permalink
feat: Add exit status code to apps
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
AtkinsSJ committed May 23, 2024
1 parent b15dc31 commit 7674da4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/puter-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}, '*');
}

Expand Down
1 change: 1 addition & 0 deletions packages/puter-js/src/modules/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AppConnection extends EventListener {
this.#isOpen = false;
this.emit('close', {
appInstanceID: this.targetAppInstanceID,
statusCode: event.data.statusCode,
});
}
});
Expand Down
13 changes: 11 additions & 2 deletions src/IPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
});
});
2 changes: 1 addition & 1 deletion src/UI/UIWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -3521,6 +3521,7 @@ window.report_app_closed = (instance_id) => {
parent.contentWindow.postMessage({
msg: 'appClosed',
appInstanceID: instance_id,
statusCode: status_code ?? 0,
}, '*');
}

Expand All @@ -3530,6 +3531,7 @@ window.report_app_closed = (instance_id) => {
child.contentWindow.postMessage({
msg: 'appClosed',
appInstanceID: instance_id,
statusCode: status_code ?? 0,
}, '*');
});

Expand Down

0 comments on commit 7674da4

Please sign in to comment.