1(function() { 2 if (typeof(define) === 'function') { 3 define(['messaging'], makeService); 4 } else { 5 window.messaging = makeService(); 6 } 7 8 // tokens are used to unsubscribe 9 var token = 0 10 // used to keep track of callbacks, maps channel -> token -> handler 11 var subscriptions = {} 12 13 function makeService() { 14 var messaging = { 15 send: send, 16 subscribe: subscribe, 17 unsubscribe: unsubscribe 18 }; 19 20 return messaging; 21 22 // send message 23 function send(channel, message) { 24 if (subscriptions.hasOwnProperty(channel)) { 25 Object 26 .getOwnPropertyNames(subscriptions[channel]) 27 .forEach(function (token) { 28 subscriptions[channel][token](message); 29 }) 30 } else console.log('Message emitted, but no one was listening'); 31 } 32 33 // subscribe to channel with handler 34 function subscribe(channel, handler) { 35 var thisToken = token; 36 token += 1; 37 38 if (!subscriptions.hasOwnProperty(channel)) { 39 subscriptions[channel] = {}; 40 } 41 subscriptions[channel][thisToken] = handler 42 43 return thisToken; 44 } 45 46 // unsubscribe using token returned by subscribe 47 function unsubscribe(token) { 48 Object 49 .getOwnPropertyNames(subscriptions) 50 .forEach(function (channel) { 51 if (subscriptions[channel].hasOwnProperty(token)) { 52 delete subscriptions[channel][token]; 53 } 54 if (Object.getOwnPropertyNames(subscriptions[channel]).length === 0) { 55 delete subscriptions[channel]; 56 } 57 }); 58 } 59 } 60})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.