Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-28340] TiAPI: Return Promise for Ti.UI.Window open() and close()

GitHub Issuen/a
TypeStory
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2021-02-08T18:59:19.000+0000
Affected Version/sn/a
Fix Version/sRelease 10.0.0
ComponentsTiAPI
LabelsengSchedule
ReporterChristopher Williams
AssigneeChristopher Williams
Created2021-02-08T18:58:38.000+0000
Updated2021-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

Comments

  1. Christopher Williams 2021-02-08

    https://github.com/appcelerator/titanium_mobile/pull/12407

JSON Source