[TIMOB-28340] TiAPI: Return Promise for Ti.UI.Window open() and close()
GitHub Issue | n/a |
---|---|
Type | Story |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2021-02-08T18:59:19.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 10.0.0 |
Components | TiAPI |
Labels | engSchedule |
Reporter | Christopher Williams |
Assignee | Christopher Williams |
Created | 2021-02-08T18:58:38.000+0000 |
Updated | 2021-04-13T12:28:08.000+0000 |
Description
We have a number of basic operations that rely on event listener based usage to handle notifying when the operation is "complete". While this works, it can lead to a lot of boilerplate code for hanging simple one-time callback logic when the operation is complete.
i.e. We often have a single listener on Ti.UI.Window#close() where we want to clean up after the windows is closed. Compare:
win.addEventListener('close', function listener (e) {
win.removeEventListener('close', listener); // clean up this callback listener
// do whatever other cleanup we want
});
win.close();
// versus
window.close(e => {
// do whatever other cleanup we want
});
It would be good to review our APIs and the events we fire and see if they are explicitly tied to methods called by the developer - and if so, provide optional callback functions as the final arguments so that developers could use a more Node.JS style usage and streamline cases where you only expect a single listener/callback (though for backwards compatibility and for supporting multiple listeners we shoulder retain the event firing as well).
Note that this would also position us well to add "shims" to convert these methods calls into Promise-based APIs at the JS level.
win.close().then(e => {
// do whatever other cleanup we want
});
// or...
const closeEvent = await win.close();
// do whatever other cleanup we want
https://github.com/appcelerator/titanium_mobile/pull/12407