From 0b817777b480fc53e4080a36787b2c7a86b9c55e Mon Sep 17 00:00:00 2001 From: Ivan Babak Date: Sun, 28 Apr 2019 03:31:16 -0700 Subject: [PATCH] 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. --- shells/browser/shared/src/backend.js | 3 +-- shells/dev/src/backend.js | 3 +-- src/__tests__/setupTests.js | 3 +-- src/backend/agent.js | 8 +++----- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/shells/browser/shared/src/backend.js b/shells/browser/shared/src/backend.js index c86a1a5d4b..88723ff563 100644 --- a/shells/browser/shared/src/backend.js +++ b/shells/browser/shared/src/backend.js @@ -54,8 +54,7 @@ function setup(hook) { }, }); - const agent = new Agent(); - agent.addBridge(bridge); + const agent = new Agent(bridge); agent.addListener('shutdown', () => { hook.emit('shutdown'); listeners.forEach(fn => { diff --git a/shells/dev/src/backend.js b/shells/dev/src/backend.js index 02ff78fd19..bfc403bb6d 100644 --- a/shells/dev/src/backend.js +++ b/shells/dev/src/backend.js @@ -25,7 +25,6 @@ bridge.addListener('captureScreenshot', ({ commitIndex }) => { }); }); -const agent = new Agent(); -agent.addBridge(bridge); +const agent = new Agent(bridge); initBackend(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, agent, window.parent); diff --git a/src/__tests__/setupTests.js b/src/__tests__/setupTests.js index 2c22aade07..33fa159c5e 100644 --- a/src/__tests__/setupTests.js +++ b/src/__tests__/setupTests.js @@ -38,8 +38,7 @@ env.beforeEach(() => { }, }); - const agent = new Agent(); - agent.addBridge(bridge); + const agent = new Agent(bridge); const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__; diff --git a/src/backend/agent.js b/src/backend/agent.js index 976b838c88..5364d048c5 100644 --- a/src/backend/agent.js +++ b/src/backend/agent.js @@ -61,13 +61,13 @@ type PersistedSelection = {| |}; export default class Agent extends EventEmitter { - _bridge: Bridge = ((null: any): Bridge); + _bridge: Bridge; _isProfiling: boolean = false; _rendererInterfaces: { [key: RendererID]: RendererInterface } = {}; _persistedSelection: PersistedSelection | null = null; _persistedSelectionMatch: PathMatch | null = null; - constructor() { + constructor(bridge: Bridge) { super(); if (localStorage.getItem(LOCAL_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') { @@ -84,9 +84,7 @@ export default class Agent extends EventEmitter { this._persistedSelection = JSON.parse(persistedSelectionString); } } - } - addBridge(bridge: Bridge) { this._bridge = bridge; bridge.addListener('captureScreenshot', this.captureScreenshot); @@ -123,7 +121,7 @@ export default class Agent extends EventEmitter { bridge.addListener('viewElementSource', this.viewElementSource); if (this._isProfiling) { - this._bridge.send('profilingStatus', true); + bridge.send('profilingStatus', true); } }