From 3117a046c24f52190740518eca2a5170c6e3549e Mon Sep 17 00:00:00 2001 From: Thomas Wilburn Date: Sun, 24 Nov 2013 20:53:55 -0500 Subject: [PATCH] Start some work on adding a pass-through messaging API. See comments on #29 --- config/api.json | 1 + js/api.js | 19 +++++++++++++++++++ js/command.js | 5 +++-- js/main.js | 3 ++- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 config/api.json create mode 100644 js/api.js diff --git a/config/api.json b/config/api.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/config/api.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/js/api.js b/js/api.js new file mode 100644 index 0000000..854b139 --- /dev/null +++ b/js/api.js @@ -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); + }); +}); \ No newline at end of file diff --git a/js/command.js b/js/command.js index 7bef79a..4b84635 100644 --- a/js/command.js +++ b/js/command.js @@ -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); }); } diff --git a/js/main.js b/js/main.js index 8f954c7..80e3b5e 100644 --- a/js/main.js +++ b/js/main.js @@ -6,7 +6,8 @@ require([ "keys", "fileManager", "menus", - "palette" + "palette", + "api" ], function(command, Settings, dialog) { var frame = chrome.app.window.current();