Start some work on adding a pass-through messaging API.

See comments on #29
This commit is contained in:
Thomas Wilburn 2013-11-24 20:53:55 -05:00
parent 98e0a4f96a
commit 3117a046c2
4 changed files with 25 additions and 3 deletions

1
config/api.json Normal file
View file

@ -0,0 +1 @@
{}

19
js/api.js Normal file
View file

@ -0,0 +1,19 @@
define(["command", "settings!api"], function(command, Settings) {
//handles sending custom messages based on Caret commands (builds, plugins, etc)
var targets = Settings.get("api");
command.on("api:execute", function(id) {
if (!id in targets) return;
var config = targets[id];
chrome.runtime.sendMessage(config.id, config.message);
});
command.on("init:restart", function() {
targets = Settings.get("api");
})
//External apps can send messages by matching Caret's command/argument config objects
chrome.runtime.onMessageExternal.addListener(function(message, sender, c) {
command.fire(message.command, message.argument, c);
});
});

View file

@ -8,11 +8,12 @@ define(["dom2"], function() {
var commands = {};
var fire = function(command, argument) {
//commands can pass a callback, although most don't respond that way
var fire = function(command, argument, callback) {
if (!commands[command]) return;
var registry = commands[command].slice();
registry.forEach(function(entry) {
entry.callback.apply(entry.scope || null, argument instanceof Array ? argument : [argument] );
entry.callback.apply(entry.scope || null, argument instanceof Array ? argument : [argument], callback);
});
}

View file

@ -6,7 +6,8 @@ require([
"keys",
"fileManager",
"menus",
"palette"
"palette",
"api"
], function(command, Settings, dialog) {
var frame = chrome.app.window.current();