@samitouri / QOS-React-2 / commits / 0b817777b4

Remove Agent addBridge: there can only be one bridge, add in constructor

Let's make impossible states truly impossible, and fix Flow types, too. All three usages of Agent called addBridge right after constructing it. Agent has one field `_bridge` which is force-typed as not-null despite there's a temporary zone between the constructor end and addBridge start where `_bridge` is null.

Ivan Babak committed Apr 28, 2019 at 03:31 UTC 0b817777b480fc53e4080a36787b2c7a86b9c55e
4 files changed +6 -11
shells/browser/shared/src/backend.js
+1 -2
@@ -54,8 +54,7 @@ function setup(hook) {
54 },
55 });
56
57 - const agent = new Agent();
58 - agent.addBridge(bridge);
57 + const agent = new Agent(bridge);
58 agent.addListener('shutdown', () => {
59 hook.emit('shutdown');
60 listeners.forEach(fn => {
shells/dev/src/backend.js
+1 -2
@@ -25,7 +25,6 @@ bridge.addListener('captureScreenshot', ({ commitIndex }) => {
25 });
26 });
27
28 -const agent = new Agent();
29 -agent.addBridge(bridge);
28 +const agent = new Agent(bridge);
29
30 initBackend(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, agent, window.parent);
src/__tests__/setupTests.js
+1 -2
@@ -38,8 +38,7 @@ env.beforeEach(() => {
38 },
39 });
40
41 - const agent = new Agent();
42 - agent.addBridge(bridge);
41 + const agent = new Agent(bridge);
42
43 const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
44
src/backend/agent.js
+3 -5
@@ -61,13 +61,13 @@ type PersistedSelection = {|
61 |};
62
63 export default class Agent extends EventEmitter {
64 - _bridge: Bridge = ((null: any): Bridge);
64 + _bridge: Bridge;
65 _isProfiling: boolean = false;
66 _rendererInterfaces: { [key: RendererID]: RendererInterface } = {};
67 _persistedSelection: PersistedSelection | null = null;
68 _persistedSelectionMatch: PathMatch | null = null;
69
70 - constructor() {
70 + constructor(bridge: Bridge) {
71 super();
72
73 if (localStorage.getItem(LOCAL_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') {
@@ -84,9 +84,7 @@ export default class Agent extends EventEmitter {
84 this._persistedSelection = JSON.parse(persistedSelectionString);
85 }
86 }
87 - }
87
89 - addBridge(bridge: Bridge) {
88 this._bridge = bridge;
89
90 bridge.addListener('captureScreenshot', this.captureScreenshot);
@@ -123,7 +121,7 @@ export default class Agent extends EventEmitter {
121 bridge.addListener('viewElementSource', this.viewElementSource);
122
123 if (this._isProfiling) {
126 - this._bridge.send('profilingStatus', true);
124 + bridge.send('profilingStatus', true);
125 }
126 }
127