Convert switchTo to use promises

This commit is contained in:
Nick Krichevsky 2018-07-24 01:53:33 -04:00
parent c6318cb8db
commit c60f3d0f4f

View file

@ -1,3 +1,5 @@
import * as util from "./util";
let windowCache: chrome.windows.Window[] = [];
/**
@ -18,12 +20,19 @@ export function getWindows(callback: (windows: chrome.windows.Window[]) => void)
*
* @param {number} windowID the tab in which the window is contained.
* @param {number} tabID The tab to switch to.
* @returns {void}
* @returns {PromiseLike<chrome.tabs.Tab>}
*/
export function switchTo(windowID: number, tabID: number): void {
chrome.windows.update(windowID, {focused: true}, () => {
chrome.tabs.update(tabID, {active: true});
});
export function switchTo(windowID: number, tabID: number): PromiseLike<chrome.tabs.Tab> {
let curriedWindowUpdate = (windowID: number, options: object) => {
return (callback: ((win: chrome.windows.Window) => void)) => chrome.windows.update(windowID, options, callback);
};
let curriedTabUpdate = (tabID: number, options: object) => {
return (callback: (tab: chrome.tabs.Tab) => void) => chrome.tabs.update(tabID, options, callback);
};
return util.callbackToPromise(curriedWindowUpdate(windowID, {focused: true}))
.then((returns: chrome.windows.Window[]) => util.callbackToPromise(curriedTabUpdate(tabID, {active: true})))
.then((returns: chrome.tabs.Tab[]) => returns[0]);
}
/**