diff --git a/src/PluginContainer.js b/src/PluginContainer.js index 6ae6435ae..cd6400c59 100644 --- a/src/PluginContainer.js +++ b/src/PluginContainer.js @@ -107,7 +107,6 @@ class PluginContainer extends Component { const {target} = this.state; if (ref && target) { activateMenuItems(ref); - ref._setup(target); ref._init(); this.plugin = ref; } @@ -135,6 +134,7 @@ class PluginContainer extends Component { logger: this.props.logger, persistedState: pluginStates[pluginKey] || {}, setPersistedState: state => setPluginState({pluginKey, state}), + target, ref: this.refChanged, })} diff --git a/src/plugin.js b/src/plugin.js index aa723ec4b..c83c288ca 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -28,6 +28,7 @@ export type Props = { logger: Logger, persistedState: T, setPersistedState: (state: $Shape) => void, + target: PluginTarget, }; export class SonarBasePlugin< @@ -56,7 +57,7 @@ export class SonarBasePlugin< onKeyboardAction: ?(action: string) => void; toJSON() { - return this.constructor.title; + return `<${this.constructor.name}#${this.constructor.title}>`; } // methods to be overriden by plugins @@ -65,7 +66,6 @@ export class SonarBasePlugin< // methods to be overridden by subclasses _init(): void {} _teardown(): void {} - _setup(target: PluginTarget) {} dispatchAction(actionData: Actions) { // $FlowFixMe @@ -91,12 +91,9 @@ export class SonarDevicePlugin extends SonarBasePlugin< > { device: BaseDevice; - _setup(target: PluginTarget) { - invariant(target instanceof BaseDevice, 'expected instanceof Client'); - const device: BaseDevice = target; - - this.device = device; - super._setup(device); + constructor(props: Props<*>) { + super(props); + this.device = props.target; } _init() { @@ -105,9 +102,23 @@ export class SonarDevicePlugin extends SonarBasePlugin< } export class SonarPlugin extends SonarBasePlugin { - constructor() { - super(); + constructor(props: Props<*>) { + super(props); + const {id} = this.constructor; this.subscriptions = []; + // $FlowFixMe props.target will be instance of Client + this.realClient = props.target; + this.client = { + call: (method, params) => this.realClient.call(id, method, params), + send: (method, params) => this.realClient.send(id, method, params), + subscribe: (method, callback) => { + this.subscriptions.push({ + method, + callback, + }); + this.realClient.subscribe(id, method, callback); + }, + }; } subscriptions: Array<{ @@ -140,35 +151,11 @@ export class SonarPlugin extends SonarBasePlugin { return device; } - _setup(target: any) { - /* We have to type the above as `any` since if we import the actual Client we have an - unresolvable dependency cycle */ - - const realClient: Client = target; - const id: string = this.constructor.id; - - this.realClient = realClient; - this.client = { - call: (method, params) => realClient.call(id, method, params), - send: (method, params) => realClient.send(id, method, params), - subscribe: (method, callback) => { - this.subscriptions.push({ - method, - callback, - }); - realClient.subscribe(id, method, callback); - }, - }; - - super._setup(realClient); - } - _teardown() { // automatically unsubscribe subscriptions for (const {method, callback} of this.subscriptions) { this.realClient.unsubscribe(this.constructor.id, method, callback); } - // run plugin teardown this.teardown(); if (this.realClient.connected) {