Uploaded image for project: 'Appcelerator - INBOX'
  1. Appcelerator - INBOX
  2. AC-3252

Adding the same event listener twice should not be allowed

    Details

      Description

      As the following test shows:

      • Calling addEventListener on exact the same object, for the same event and using the same callback will cause the callback to get fired for as many times it has been added.
      • Calling removeEventListener on that same object, event and with the same callback will only remove one of the previous times it was added.

      I'd expect the same callback to be only added one, which should be doable by just checking if the object the callback param refers to is already a listener for that event.

      var count = 0;
      var fired = 0;
       
      function listener() {
          fired++;
          firedLabel.text = 'Fired: ' + fired;
      }
       
      var win = Ti.UI.createWindow({
          backgroundColor:'#fff'
      });
       
      var remove = Ti.UI.createButton({
          top: 100,
          left: 25,
          title: 'Remove'
      });
       
      var countLabel = Ti.UI.createLabel({
          top: 100,
          text: 'Added: ' + count
      });
       
      var add = Ti.UI.createButton({
          top: 100,
          right: 25,
          title: 'Add'
      });
       
      var fire = Titanium.UI.createButton({
          top: 300,
          left: 25,
          title: 'Fire'
      });
       
      var firedLabel = Ti.UI.createLabel({
          top: 300,
          right: 25,
          text: 'Fired: ' + 0
      });
       
      add.addEventListener('click', function () {
          count++;
          countLabel.text = 'Added: ' + count;
          Ti.App.addEventListener('app:test', listener);
      });
       
      remove.addEventListener('click', function () {
          count--;
          countLabel.text = 'Added: ' + count;
          Ti.App.removeEventListener('app:test', listener);
      });
       
      fire.addEventListener('click', function () {
          fired = 0;
          firedLabel.text = 'Fired: ' + fired;
          Ti.App.fireEvent('app:test');
      });
       
      win.add(countLabel);
      win.add(remove);
      win.add(add);
      win.add(firedLabel);
      win.add(fire);
       
      win.open();
      

        Attachments

          Activity

            People

            • Assignee:
              mpmiranda Mauro Parra-Miranda
              Reporter:
              fokkezb Fokke Zandbergen
            • Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Git Integration