Add-on Communication Jetpack Module

By Erik Vold

I’ve made a module can be used to quickly facilitate communication between add-ons.

Simply put this module allows one to expose a JSON API which other add-ons can utilize.

Usage

Add-on A (with id = addon-a@example.net) code:

var { on, emit } = require('addon-communication');

on(function({ id, data }) {
  if (data.query) {
    // some add-on was asking me for results, so I should send back what I know
    emit({ id: id, data: {
      results: []   // bad example, in the real world there would be results.
    }})
  }
});

Add-on B code:

var { on, emit } = require('addon-communication');
var addonA_ID = 'addon-a@example.net';

on(function({ id, data }) {
  // is the message from addon A ?
  if (id == addonA_ID) {
    console.log(data.results.join(', '));
  }
  // some other addon is sending results, ignore that..
  else {}
});

emit({ id: addonA_ID, data: {
  query: 'I want some results from Addon A!!'
}});

Bonus Features

Example use case

If I want to make my Scriptish add-on extendable, by adding an api which other add-ons can use to install UserScripts, then I can use this module to do so, I merely have to define the type of data that I expect to recieve and document that womewhere so that other add-on developers can read it. So this use case might look like this:

Scriptish code:

var { on } = require('addon-communication');

on(function({ data }) {
  install(data.userscript);
});

Other Add-on (maybe Greasefire for example)

var { emit } = require('addon-communication');

getSourceCodeFor('http://userscripts.org/something/userscript.user.js', function(source) {
  emit({ id: 'scriptish@erikvold.com', data: {
    userscript: source
  }});
});

You can find the addon-communication Jetpack module on Github here.