Skip to content
This repository has been archived by the owner on Feb 9, 2020. It is now read-only.

Commit

Permalink
feature(background): refactor background file to use observables
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinmombay committed Aug 26, 2015
1 parent 398a87b commit ba796af
Show file tree
Hide file tree
Showing 5 changed files with 12,398 additions and 1,710 deletions.
80 changes: 52 additions & 28 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ var inspectedTabs = {};
// tabId -> buffered data
var data = {};

function bufferOrForward(message, sender) {
var tabId = sender.tab.id,
devToolsPort = inspectedTabs[tabId];
var runtimeMessage = createRxFrom(chrome.runtime.onMessage, messageSelector);
var runtimeConnect = createRxFrom(chrome.runtime.onConnect);
var tabsRemoved = createRxFrom(chrome.tabs.onRemoved);

runtimeMessage.forEach(bufferOrForward);
runtimeConnect.forEach(onConnect);
tabsRemoved.forEach(onTabsRemoved);

function bufferOrForward(event) {
var message = event.message;
var sender = event.sender;
var tabId = sender.tab.id;
var devToolsPort = inspectedTabs[tabId];

if (!data[tabId] || message === 'refresh') {
resetState(tabId);
Expand Down Expand Up @@ -94,39 +104,37 @@ function bufferData(tabId, message) {
// TODO: Handle digest timings
}

// context script –> background
chrome.runtime.onMessage.addListener(bufferOrForward);

chrome.runtime.onConnect.addListener(function(devToolsPort) {

devToolsPort.onMessage.addListener(registerInspectedTabId);

function registerInspectedTabId(inspectedTabId) {
inspectedTabs[inspectedTabId] = devToolsPort;

if (!data[inspectedTabId]) {
resetState(inspectedTabId);
}
devToolsPort.postMessage({
event: 'hydrate',
data: data[inspectedTabId]
});
function onConnect(devToolsPort) {
var devToolsMessage = createRxFrom(devToolsPort.onMessage);
devToolsMessage.forEach(registerInspectedTabId.bind(null, devToolsPort));
return event;
}

devToolsPort.onDisconnect.addListener(function () {
delete inspectedTabs[inspectedTabId];
});
function registerInspectedTabId(devToolsPort, inspectedTabId) {
inspectedTabs[inspectedTabId] = devToolsPort;

//devToolsPort.onMessage.removeListener(registerInspectedTabId);
if (!data[inspectedTabId]) {
resetState(inspectedTabId);
}
devToolsPort.postMessage({
event: 'hydrate',
data: data[inspectedTabId]
});

});
var devToolsDisconnect = createRxFrom(devToolsPort.onDisconnect);
devToolsDisconnect.forEach(onDisconnect);
return event;
}

chrome.tabs.onRemoved.addListener(function (tabId) {
function onDisconnect(inspectedTabId) {
delete inspectedTabs[inspectedTabId];
}

function onTabsRemoved(tabId) {
if (data[tabId]) {
delete data[tabId];
}
});

}

function showPageAction(tabId) {
chrome.pageAction.show(tabId);
Expand All @@ -135,3 +143,19 @@ function showPageAction(tabId) {
title: 'Batarang Active'
});
}

function createRxFrom(target, selector) {
return Rx.Observable.fromEventPattern(
function(handler) { target.addListener(handler); },
function(handler) { target.removeListener(handler); },
selector
);
}

function messageSelector() {
return {
message: arguments[0],
sender: arguments[1],
sendResponse: arguments[2]
};
}
Loading

0 comments on commit ba796af

Please sign in to comment.