remove setup method in plugins
Summary: Plugins had their custom setup method which needed to be called externally. That is what consturctors are for. This removes the setup method and moves ths logic to the constructor. The setup method was called to late which caused the graphQL plugin to crash. With the logic now being in the constructor, it is ensured that it is called at the initialization. Reviewed By: jknoxville Differential Revision: D8769807 fbshipit-source-id: 7b4ab4815bbe397c80998adcb89ca361df6970d3
This commit is contained in:
committed by
Facebook Github Bot
parent
c8c2cfa16f
commit
c948e50c10
@@ -107,7 +107,6 @@ class PluginContainer extends Component<Props, State> {
|
|||||||
const {target} = this.state;
|
const {target} = this.state;
|
||||||
if (ref && target) {
|
if (ref && target) {
|
||||||
activateMenuItems(ref);
|
activateMenuItems(ref);
|
||||||
ref._setup(target);
|
|
||||||
ref._init();
|
ref._init();
|
||||||
this.plugin = ref;
|
this.plugin = ref;
|
||||||
}
|
}
|
||||||
@@ -135,6 +134,7 @@ class PluginContainer extends Component<Props, State> {
|
|||||||
logger: this.props.logger,
|
logger: this.props.logger,
|
||||||
persistedState: pluginStates[pluginKey] || {},
|
persistedState: pluginStates[pluginKey] || {},
|
||||||
setPersistedState: state => setPluginState({pluginKey, state}),
|
setPersistedState: state => setPluginState({pluginKey, state}),
|
||||||
|
target,
|
||||||
ref: this.refChanged,
|
ref: this.refChanged,
|
||||||
})}
|
})}
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export type Props<T> = {
|
|||||||
logger: Logger,
|
logger: Logger,
|
||||||
persistedState: T,
|
persistedState: T,
|
||||||
setPersistedState: (state: $Shape<T>) => void,
|
setPersistedState: (state: $Shape<T>) => void,
|
||||||
|
target: PluginTarget,
|
||||||
};
|
};
|
||||||
|
|
||||||
export class SonarBasePlugin<
|
export class SonarBasePlugin<
|
||||||
@@ -56,7 +57,7 @@ export class SonarBasePlugin<
|
|||||||
onKeyboardAction: ?(action: string) => void;
|
onKeyboardAction: ?(action: string) => void;
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
return this.constructor.title;
|
return `<${this.constructor.name}#${this.constructor.title}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// methods to be overriden by plugins
|
// methods to be overriden by plugins
|
||||||
@@ -65,7 +66,6 @@ export class SonarBasePlugin<
|
|||||||
// methods to be overridden by subclasses
|
// methods to be overridden by subclasses
|
||||||
_init(): void {}
|
_init(): void {}
|
||||||
_teardown(): void {}
|
_teardown(): void {}
|
||||||
_setup(target: PluginTarget) {}
|
|
||||||
|
|
||||||
dispatchAction(actionData: Actions) {
|
dispatchAction(actionData: Actions) {
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
@@ -91,12 +91,9 @@ export class SonarDevicePlugin<S = *, A = *, P = *> extends SonarBasePlugin<
|
|||||||
> {
|
> {
|
||||||
device: BaseDevice;
|
device: BaseDevice;
|
||||||
|
|
||||||
_setup(target: PluginTarget) {
|
constructor(props: Props<*>) {
|
||||||
invariant(target instanceof BaseDevice, 'expected instanceof Client');
|
super(props);
|
||||||
const device: BaseDevice = target;
|
this.device = props.target;
|
||||||
|
|
||||||
this.device = device;
|
|
||||||
super._setup(device);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_init() {
|
_init() {
|
||||||
@@ -105,9 +102,23 @@ export class SonarDevicePlugin<S = *, A = *, P = *> extends SonarBasePlugin<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SonarPlugin<S = *, A = *, P = *> extends SonarBasePlugin<S, A, P> {
|
export class SonarPlugin<S = *, A = *, P = *> extends SonarBasePlugin<S, A, P> {
|
||||||
constructor() {
|
constructor(props: Props<*>) {
|
||||||
super();
|
super(props);
|
||||||
|
const {id} = this.constructor;
|
||||||
this.subscriptions = [];
|
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<{
|
subscriptions: Array<{
|
||||||
@@ -140,35 +151,11 @@ export class SonarPlugin<S = *, A = *, P = *> extends SonarBasePlugin<S, A, P> {
|
|||||||
return device;
|
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() {
|
_teardown() {
|
||||||
// automatically unsubscribe subscriptions
|
// automatically unsubscribe subscriptions
|
||||||
for (const {method, callback} of this.subscriptions) {
|
for (const {method, callback} of this.subscriptions) {
|
||||||
this.realClient.unsubscribe(this.constructor.id, method, callback);
|
this.realClient.unsubscribe(this.constructor.id, method, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
// run plugin teardown
|
// run plugin teardown
|
||||||
this.teardown();
|
this.teardown();
|
||||||
if (this.realClient.connected) {
|
if (this.realClient.connected) {
|
||||||
|
|||||||
Reference in New Issue
Block a user