Home | History | Annotate | Download | only in core-signals
      1 
      2 (function(){
      3   
      4   Polymer('core-signals',{
      5     attached: function() {
      6       signals.push(this);
      7     },
      8     removed: function() {
      9       var i = signals.indexOf(this);
     10       if (i >= 0) {
     11         signals.splice(i, 1);
     12       }
     13     }
     14   });
     15 
     16   // private shared database
     17   var signals = [];
     18 
     19   // signal dispatcher
     20   function notify(name, data) {
     21     // convert generic-signal event to named-signal event
     22     var signal = new CustomEvent('core-signal-' + name, {
     23       // if signals bubble, it's easy to get confusing duplicates
     24       // (1) listen on a container on behalf of local child
     25       // (2) some deep child ignores the event and it bubbles 
     26       //     up to said container
     27       // (3) local child event bubbles up to container
     28       // also, for performance, we avoid signals flying up the
     29       // tree from all over the place
     30       bubbles: false,
     31       detail: data
     32     });
     33     // dispatch named-signal to all 'signals' instances,
     34     // only interested listeners will react
     35     signals.forEach(function(s) {
     36       s.dispatchEvent(signal);
     37     });
     38   }
     39 
     40   // signal listener at document
     41   document.addEventListener('core-signal', function(e) {
     42     notify(e.detail.name, e.detail.data);
     43   });
     44   
     45 })();
     46