Skip to content

Instantly share code, notes, and snippets.

@kmod-midori
Created May 30, 2015 13:22
Show Gist options
  • Select an option

  • Save kmod-midori/1b171d0871dcf2d31557 to your computer and use it in GitHub Desktop.

Select an option

Save kmod-midori/1b171d0871dcf2d31557 to your computer and use it in GitHub Desktop.
// Create a random ID for this session.
var sessionId = String.fromCharCode(Date.now() % 25 + 97) +
Math.random().toString(36).slice(2); // From uBlock
var port = chrome.runtime.connect({name: sessionId}),
channels = [],
// For receiving responses.
resp = new require('events').EventEmitter();
class Channel {
constructor(name, callback){
this.name = name;
this.listener = typeof callback === 'function' ? callback : null;
}
send(message,callback){
message = {
channel:this.name,
msg:message
};
if (callback) {
// Generate an unique ID so we can receive response later.
message.id = _.uniqueId()
resp.once(message.id,callback)
}
port.postMessage(message);
}
}
/**
* Get a messaging channel.
* @param {String} name Channel name. If not existed, it'll be created.
* @param {Function} callback
* @return {Channel}
*/
module.exports = function (name, callback){
if ( !name ) {
return;
}
if (channels.indexOf(name) == -1) {
channels[name] = new Channel(name, callback);
}
return channels[name];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment