Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-3785

Channel.prototype.subscribe to support EventListener interface

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.8.0
    • Fix Version/s: None
    • Component/s: cordova-js
    • Environment:

      All platforms

      Description

      As number of Cordova-specific events use monkey-patch for add/remove/fire listeners it would be nice if Channel functions support EventListener interface:

      I assume only a small patch to Channel module is required. Here are the modified functions to resolve the issue:

      /**
       * Subscribes the given function to the channel. Any time that
       * Channel.fire is called so too will the function.
       * Optionally specify an execution context for the function
       * and a guid that can be used to stop subscribing to the channel.
       * Returns the guid.
       */
      Channel.prototype.subscribe = function(f, c) {
      
          var func;
          if (f && "object" === typeof f) {
              // EventListener interface
              func = f["handleEvent"];
              c = f;
          } else {
              // Function interface
              func = f;
          }
      
          // need a function to call
          forceFunction(func);
          if (this.state == 2) {
              func.apply(c || this, this.fireArgs);
              return;
          }
      
          var guid = f.observer_guid;
          if (typeof c == "object") { func = utils.close(c, func); }
      
          if (!guid) {
              // first time any channel has seen this subscriber
              guid = '' + nextGuid++;
          }
          func.observer_guid = guid;
          f.observer_guid = guid;
      
          // Don't add the same handler more than once.
          if (!this.handlers[guid]) {
              this.handlers[guid] = func;
              this.numHandlers++;
              if (this.numHandlers == 1) {
                  this.onHasSubscribersChange && this.onHasSubscribersChange();
              }
          }
      };
      
      /**
       * Unsubscribes the function with the given guid from the channel.
       */
      Channel.prototype.unsubscribe = function(f) {
          var func;
          if (f && "object" === typeof f) {
              // EventListener interface
              func = f["handleEvent"];
              c = f;
          } else {
              // Function interface
              func = f;
          }
      
          // need a function to unsubscribe
          forceFunction(func);
      
          var guid = f.observer_guid,
              handler = this.handlers[guid];
          if (handler) {
              delete this.handlers[guid];
              this.numHandlers--;
              if (this.numHandlers === 0) {
                  this.onHasSubscribersChange && this.onHasSubscribersChange();
              }
          }
      };
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rayshan Ray Shan
                Reporter:
                motorro Nikolai Kotchetkov
              • Votes:
                1 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - 1h
                  1h
                  Remaining:
                  Remaining Estimate - 1h
                  1h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified