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

Commit 5d09a17

Browse files
gary-bSomeKittens
authored andcommitted
fix(inspectedApp) detect hints properly
Once Batarang had loaded the hints tab would never update. inspectedApp was unable to track hints sent to it after model hydration - there was a bug in the code it used to detect when a message was a hint. Updated background.js, which does the identification process already, to add an isHint flag to messages.
1 parent 8c6cecc commit 5d09a17

File tree

3 files changed

+125
-47
lines changed

3 files changed

+125
-47
lines changed

background.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function bufferOrForward(message, sender) {
1717
}
1818

1919
if (message !== 'refresh') {
20+
setIsHintFlag(message);
2021
bufferData(tabId, message);
2122
}
2223
if (devToolsPort) {
@@ -31,18 +32,21 @@ function resetState(tabId) {
3132
};
3233
}
3334

34-
function bufferData(tabId, message) {
35-
var tabData = data[tabId],
36-
scope;
37-
35+
function setIsHintFlag(message) {
3836
var hintables = [
3937
'Controllers',
4038
'general',
4139
'Modules',
4240
'Events'
4341
];
42+
message.isHint = (hintables.indexOf(message.module) > -1);
43+
}
44+
45+
function bufferData(tabId, message) {
46+
var tabData = data[tabId],
47+
scope;
4448

45-
if (hintables.indexOf(message.module) > -1) {
49+
if (message.isHint) {
4650
tabData.hints.push(message);
4751
}
4852

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,34 @@ function inspectedAppService($rootScope, $q) {
7575
console.log(a);
7676
});
7777

78-
function onHintMessage(hint) {
79-
if (hint.message) {
80-
hints.push(hint);
81-
} else if (hint.event) {
82-
if (hint.event === 'hydrate') {
83-
Object.keys(hint.data.scopes).forEach(function (scopeId) {
84-
scopes[scopeId] = hint.data.scopes[scopeId];
78+
function onHintMessage(message) {
79+
if (message.isHint) {
80+
hints.push(message);
81+
} else if (message.event) {
82+
if (message.event === 'hydrate') {
83+
Object.keys(message.data.scopes).forEach(function (scopeId) {
84+
scopes[scopeId] = message.data.scopes[scopeId];
8585
});
86-
hint.data.hints.forEach(function (hint) {
86+
message.data.hints.forEach(function (hint) {
8787
hints.push(hint);
8888
});
89-
} else if (hint.event === 'scope:new') {
90-
addNewScope(hint);
91-
} else if (hint.data.id && scopes[hint.data.id]) {
92-
var scope = scopes[hint.data.id];
93-
if (hint.event === 'scope:destroy') {
89+
} else if (message.event === 'scope:new') {
90+
addNewScope(message);
91+
} else if (message.data.id && scopes[message.data.id]) {
92+
var scope = scopes[message.data.id];
93+
if (message.event === 'scope:destroy') {
9494
if (scope.parent) {
9595
scope.parent.children.splice(scope.parent.children.indexOf(child), 1);
9696
}
97-
delete scopes[hint.data.id];
98-
} else if (hint.event === 'model:change') {
99-
scope.models[hint.data.path] = (typeof hint.data.value === 'undefined') ?
100-
undefined : JSON.parse(hint.data.value);
101-
} else if (hint.event === 'scope:link') {
102-
scope.descriptor = hint.data.descriptor;
97+
delete scopes[message.data.id];
98+
} else if (message.event === 'model:change') {
99+
scope.models[message.data.path] = (typeof message.data.value === 'undefined') ?
100+
undefined : JSON.parse(message.data.value);
101+
} else if (message.event === 'scope:link') {
102+
scope.descriptor = message.data.descriptor;
103103
}
104104
}
105-
$rootScope.$broadcast(hint.event, hint.data);
105+
$rootScope.$broadcast(message.event, message.data);
106106
}
107107
}
108108

@@ -111,14 +111,14 @@ function inspectedAppService($rootScope, $q) {
111111
hints.length = 0;
112112
}
113113

114-
function addNewScope (hint) {
115-
scopes[hint.data.child] = {
116-
parent: hint.data.parent,
114+
function addNewScope (message) {
115+
scopes[message.data.child] = {
116+
parent: message.data.parent,
117117
children: [],
118118
models: {}
119119
};
120-
if (scopes[hint.data.parent]) {
121-
scopes[hint.data.parent].children.push(hint.data.child);
120+
if (scopes[message.data.parent]) {
121+
scopes[message.data.parent].children.push(message.data.child);
122122
}
123123
}
124124

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

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,107 @@ describe('inspectedApp', function() {
2121
});
2222

2323
describe('messaging', function () {
24-
it('should track hints', inject(function ($browser) {
25-
port.onMessage.trigger(JSON.stringify({ message: 'hi' }));
26-
$browser.defer.flush();
27-
expect(inspectedApp.hints).toEqual([{message: 'hi'}]);
28-
}));
24+
var $browser;
25+
beforeEach(function(){
26+
inject(function(_$browser_) {
27+
$browser = _$browser_;
28+
});
29+
});
2930

30-
it('should track new scopes', inject(function ($browser) {
31-
port.onMessage.trigger(JSON.stringify({ event: 'scope:new', data: { child: 1 } }));
31+
function triggerAndFlush(object){
32+
port.onMessage.trigger(JSON.stringify(object));
3233
$browser.defer.flush();
34+
}
35+
36+
it('should track hints', function () {
37+
var hint = { isHint: true };
38+
triggerAndFlush(hint);
39+
40+
expect(inspectedApp.hints).toEqual([hint]);
41+
});
42+
43+
it('should hydrate the model', function () {
44+
var scopes = { 1: 'a', 2: 'b' },
45+
hints = [ 'h1', 'h2' ];
46+
triggerAndFlush({ event: 'hydrate', data: { scopes: scopes, hints: hints } });
47+
48+
expect(inspectedApp.scopes[1]).toEqual(scopes[1]);
49+
expect(inspectedApp.scopes[2]).toEqual(scopes[2]);
50+
expect(inspectedApp.hints[0]).toEqual(hints[0]);
51+
expect(inspectedApp.hints[1]).toEqual(hints[1]);
52+
});
53+
54+
it('should track new scopes', function () {
55+
var id = 1, parentId = 2;
56+
inspectedApp.scopes[parentId] = { children: [] };
57+
triggerAndFlush({ event: 'scope:new', data: { child: id, parent: parentId } });
58+
59+
expect(inspectedApp.scopes[id]).toEqual({ parent: parentId, children: [], models: {} });
60+
expect(inspectedApp.scopes[parentId].children).toEqual([ id ]);
61+
});
62+
63+
it('should track new scopes without throwing exception when parent scope not present', function () {
64+
var id = 1, parentId = 2;
65+
port.onMessage.trigger({ event: 'scope:new', data: { child: id, parent: parentId } });
66+
67+
expect($browser.defer.flush).not.toThrow();
68+
expect(inspectedApp.scopes[id]).toEqual({ parent: parentId, children: [], models: {} });
69+
});
3370

34-
expect(inspectedApp.scopes).toEqual({ 1: { parent: undefined, children: [], models: {} } });
35-
}));
71+
it('should track destruction of scopes without throwing error when parent scope not present', function () {
72+
var id = 1;
73+
inspectedApp.scopes[id] = true;
74+
port.onMessage.trigger({ event: 'scope:destroy', data: { id: id } });
3675

37-
it('should track updates to scope descriptors', inject(function ($browser) {
76+
expect($browser.defer.flush).not.toThrow();
77+
expect(inspectedApp.scopes.hasOwnProperty(id)).toBeFalsy();
78+
});
79+
80+
it('should track model changes', function () {
81+
var id = 1;
82+
inspectedApp.scopes[id] = { models: {} };
83+
84+
triggerAndFlush({
85+
event: 'model:change',
86+
data: {
87+
id: id,
88+
path: '',
89+
value: '"jsonHere"'
90+
}
91+
});
92+
93+
expect(inspectedApp.scopes[id].models['']).toEqual("jsonHere");
94+
});
95+
96+
it('should track model changes without throwing exception when values are missing', function () {
97+
var id = 1;
98+
inspectedApp.scopes[id] = { models: { '': true} };
99+
expect(inspectedApp.scopes[id].models['']).toBeTruthy();
100+
port.onMessage.trigger(JSON.stringify({
101+
event: 'model:change',
102+
data: {
103+
id:id,
104+
path: ''
105+
}
106+
}));
107+
108+
expect($browser.defer.flush).not.toThrow();
109+
expect(inspectedApp.scopes[id].models['']).toBeUndefined();
110+
});
111+
112+
it('should track updates to scope descriptors', function () {
38113
port.onMessage.trigger(JSON.stringify({ event: 'scope:new', data: { child: 1 } }));
39-
port.onMessage.trigger(JSON.stringify({ event: 'scope:link', data: { id: 1, descriptor: 'pasta' } }));
40-
$browser.defer.flush();
114+
triggerAndFlush({ event: 'scope:link', data: { id: 1, descriptor: 'pasta' }});
41115

42116
expect(inspectedApp.scopes[1].descriptor).toBe('pasta');
43-
}));
44-
it('should broadcast message from $rootScope', inject(function ($browser) {
117+
});
118+
119+
it('should broadcast message from $rootScope', function () {
45120
var message = { event: 'scope:new', data: { child: 1 } };
46-
port.onMessage.trigger(JSON.stringify(message));
47-
$browser.defer.flush();
121+
triggerAndFlush(message);
48122

49123
expect(rootScope.$broadcast).toHaveBeenCalledWith(message.event, message.data);
50-
}));
124+
});
51125
});
52126

53127
describe('watch', function () {

0 commit comments

Comments
 (0)