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

Commit 0cf05a7

Browse files
committed
fix(background): hydrate inspected app data on devtools connect
1 parent f2a9044 commit 0cf05a7

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

background.js

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,67 @@
22
// tabId -> devtool port
33
var inspectedTabs = {};
44

5-
// TODO: keep track of app state here
6-
// tabId -> list of buffered events
7-
var buffer = {};
5+
// tabId -> buffered data
6+
var data = {};
87

98
function bufferOrForward(message, sender) {
109
var tabId = sender.tab.id,
1110
devToolsPort = inspectedTabs[tabId];
1211

12+
if (!data[tabId] || message === 'refresh') {
13+
resetState(tabId);
14+
}
15+
16+
// TODO: not sure how I feel about special-casing `refresh`
17+
if (message !== 'refresh') {
18+
message = JSON.parse(message);
19+
}
20+
21+
bufferData(tabId, message);
1322
if (devToolsPort) {
1423
devToolsPort.postMessage(message);
1524
}
16-
if (!buffer[tabId] || message === 'refresh') {
17-
resetState(tabId);
25+
}
26+
27+
function resetState(tabId) {
28+
data[tabId] = {
29+
hints: [],
30+
scopes: {}
31+
};
32+
}
33+
34+
function bufferData(tabId, message) {
35+
var tabData = data[tabId],
36+
scope;
37+
38+
if (message.message) {
39+
return tabData.hints.push(message);
40+
}
41+
42+
if (message.event) {
43+
if (message.event === 'scope:new') {
44+
tabData.scopes[message.child] = {
45+
parent: message.parent,
46+
children: [],
47+
models: {}
48+
};
49+
if (tabData.scopes[message.parent]) {
50+
tabData.scopes[message.parent].children.push(message.child);
51+
}
52+
} else if (message.id && (scope = tabData.scopes[message.id])) {
53+
if (message.event === 'scope:destroy') {
54+
if (scope.parent) {
55+
scope.parent.children.splice(scope.parent.children.indexOf(child), 1);
56+
}
57+
delete scopes[message.id];
58+
} else if (message.event === 'model:change') {
59+
scope.models[message.path] = (typeof message.value === 'undefined') ?
60+
undefined : JSON.parse(message.value);
61+
} else if (message.event === 'scope:link') {
62+
scope.descriptor = message.descriptor;
63+
}
64+
}
1865
}
19-
buffer[tabId].push(message);
2066
}
2167

2268
// context script –> background
@@ -29,11 +75,12 @@ chrome.runtime.onConnect.addListener(function(devToolsPort) {
2975
function registerInspectedTabId(inspectedTabId) {
3076
inspectedTabs[inspectedTabId] = devToolsPort;
3177

32-
if (!buffer[inspectedTabId]) {
78+
if (!data[inspectedTabId]) {
3379
resetState(inspectedTabId);
3480
}
35-
buffer[inspectedTabId].forEach(function(msg) {
36-
devToolsPort.postMessage(msg);
81+
devToolsPort.postMessage({
82+
event: 'hydrate',
83+
data: data[inspectedTabId]
3784
});
3885

3986
devToolsPort.onDisconnect.addListener(function () {
@@ -44,7 +91,3 @@ chrome.runtime.onConnect.addListener(function(devToolsPort) {
4491
}
4592

4693
});
47-
48-
function resetState(tabId) {
49-
buffer[tabId] = [];
50-
}

panel/components/inspected-app/inspected-app.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ function inspectedAppService($rootScope, $q) {
6767
} else if (typeof msg === 'string') {
6868
var hint = JSON.parse(msg);
6969
onHintMessage(hint);
70+
} else if (typeof msg === 'object') {
71+
onHintMessage(msg);
7072
}
7173
});
7274
});
@@ -78,7 +80,14 @@ function inspectedAppService($rootScope, $q) {
7880
if (hint.message) {
7981
hints.push(hint);
8082
} else if (hint.event) {
81-
if (hint.event === 'scope:new') {
83+
if (hint.event === 'hydrate') {
84+
Object.keys(hint.data.scopes).forEach(function (scopeId) {
85+
scopes[scopeId] = hint.data.scopes[scopeId];
86+
});
87+
hint.data.hints.forEach(function (hint) {
88+
hints.push(hint);
89+
});
90+
} else if (hint.event === 'scope:new') {
8291
addNewScope(hint);
8392
} else if (hint.id && scopes[hint.id]) {
8493
var scope = scopes[hint.id];

0 commit comments

Comments
 (0)