aboutsummaryrefslogtreecommitdiffstats
path: root/background_page.html
diff options
context:
space:
mode:
authorilya2009-10-31 17:26:56 -0700
committerilya2009-10-31 17:26:56 -0700
commitdb7450b2cdfd9fb6cf0151d53045cc2c423cf0fa (patch)
tree9c2aa3a295f1bec188332669e367085cfd124f21 /background_page.html
parent5114f0235e455e2bc43a12c624e82849c273d9d1 (diff)
downloadvimium-db7450b2cdfd9fb6cf0151d53045cc2c423cf0fa.tar.bz2
String together some callbacks to properly execute multiple actions that are asynchronous. For example, there was a race between the tab remove call and the selection of a new tab.
Diffstat (limited to 'background_page.html')
-rw-r--r--background_page.html43
1 files changed, 34 insertions, 9 deletions
diff --git a/background_page.html b/background_page.html
index 299ab901..e3193a59 100644
--- a/background_page.html
+++ b/background_page.html
@@ -2,25 +2,53 @@
<head>
<script type="text/javascript" charset="utf-8">
var tabQueue = [];
+ var selectionChangedHandlers = [];
var keyQueue = "";
chrome.extension.onConnect.addListener(function(port, name) {
if (port.name == "keyDown")
port.onMessage.addListener(handleKeyDown);
});
-
- function createTab() { chrome.tabs.create({}); }
- function removeTab() {
+ chrome.tabs.onSelectionChanged.addListener(function (tabId, selectionInfo) {
+ if (selectionChangedHandlers.length > 0)
+ {
+ selectionChangedHandlers.pop().call();
+ }
+ });
+
+ function registerSelectionChangedHandler(handler) {
+ selectionChangedHandlers.push(handler);
+ }
+
+ function repeatFunction(func, totalCount, currentCount) {
+ if (currentCount < totalCount)
+ {
+ func(function () { repeatFunction(func, totalCount, currentCount + 1); });
+ }
+ }
+
+ // Start action functions
+ function createTab(callback) {
+ chrome.tabs.create({}, function (tab) { callback(); });
+ }
+
+ function removeTab(callback) {
chrome.tabs.getSelected(null, function(tab) {
tabQueue.push(tab.url);
chrome.tabs.remove(tab.id);
+ // We can't just call the callback here because we actually need to wait
+ // for the selection to change to consider this action done.
+ registerSelectionChangedHandler(callback);
});
}
- function restoreTab() {
- if (tabQueue.length > 0) { chrome.tabs.create({url: tabQueue.pop()}); }
+ function restoreTab(callback) {
+ if (tabQueue.length > 0) {
+ chrome.tabs.create({url: tabQueue.pop()}, function (tab) { callback(); });
+ }
}
+ // End action functions
var keyToCommandRegistry = {};
keyToCommandRegistry['j'] = 'scrollDown';
@@ -60,10 +88,7 @@
port.postMessage({command: registryEntry, count: count});
});
}
- else
- {
- for (var i = 0; i < count; i++) { registryEntry.call(); }
- }
+ else { repeatFunction(registryEntry, count, 0); }
keyQueue = "";
}