Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-27710

TiAPI: Support optional callback functions for async methods that typically fire events

    Details

    • Type: Story
    • Status: In Progress
    • Priority: High
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Release 9.1.0
    • Component/s: TiAPI
    • Labels:

      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
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                cwilliams Christopher Williams
                Reporter:
                cwilliams Christopher Williams
              • Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Backbone Issue Sync

                  • Backbone Issue Sync is enabled for your project, but we do not have any synchronization info for this issue.

                    Git Source Code