diff --git a/packages/puter-js/src/index.js b/packages/puter-js/src/index.js index ac27108674..df4c55ee6e 100644 --- a/packages/puter-js/src/index.js +++ b/packages/puter-js/src/index.js @@ -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, }, '*'); } diff --git a/packages/puter-js/src/modules/UI.js b/packages/puter-js/src/modules/UI.js index a8bf3c620e..7bf7e53c03 100644 --- a/packages/puter-js/src/modules/UI.js +++ b/packages/puter-js/src/modules/UI.js @@ -54,6 +54,7 @@ class AppConnection extends EventListener { this.#isOpen = false; this.emit('close', { appInstanceID: this.targetAppInstanceID, + statusCode: event.data.statusCode, }); } }); diff --git a/src/IPC.js b/src/IPC.js index a04cba01a2..28249782df 100644 --- a/src/IPC.js +++ b/src/IPC.js @@ -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, + }); } -}); \ No newline at end of file +}); diff --git a/src/UI/UIWindow.js b/src/UI/UIWindow.js index 06def551a2..f8356b2084 100644 --- a/src/UI/UIWindow.js +++ b/src/UI/UIWindow.js @@ -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(); diff --git a/src/helpers.js b/src/helpers.js index 5e15a53a2b..65f1c15599 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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, }, '*'); });