Make Client strict

Summary:
Could use a closer look. *Shouldn't* change semantics,
but there are some assumptions baked into the code
which I don't fully grasp.

Reviewed By: jknoxville

Differential Revision: D17282310

fbshipit-source-id: af8e6bcd188bd12180a7b2eeafee7ced4f44d1aa
This commit is contained in:
Pascal Hartig
2019-09-10 10:31:44 -07:00
committed by Facebook Github Bot
parent b19f08ba54
commit c2e2915471

View File

@@ -14,6 +14,7 @@ import {setPluginState} from './reducers/pluginStates';
import {RSocketClientSocket} from 'rsocket-core/RSocketClient'; import {RSocketClientSocket} from 'rsocket-core/RSocketClient';
import {performance} from 'perf_hooks'; import {performance} from 'perf_hooks';
import {reportPlatformFailures, reportPluginFailures} from './utils/metrics'; import {reportPlatformFailures, reportPluginFailures} from './utils/metrics';
import {notNull} from './utils/typeUtils';
import {default as isProduction} from './utils/isProduction'; import {default as isProduction} from './utils/isProduction';
import {registerPlugins} from './reducers/plugins'; import {registerPlugins} from './reducers/plugins';
import createTableNativePlugin from './plugins/TableNativePlugin'; import createTableNativePlugin from './plugins/TableNativePlugin';
@@ -77,11 +78,14 @@ const handleError = (
reason: JSON.stringify(error), reason: JSON.stringify(error),
}; };
const newPluginState = crashReporterPlugin.persistedStateReducer( const newPluginState =
persistedState, crashReporterPlugin.persistedStateReducer == null
'flipper-crash-report', ? persistedState
payload, : crashReporterPlugin.persistedStateReducer(
); persistedState,
'flipper-crash-report',
payload,
);
if (persistedState !== newPluginState) { if (persistedState !== newPluginState) {
store.dispatch( store.dispatch(
setPluginState({ setPluginState({
@@ -98,20 +102,20 @@ export const SAVED_PLUGINS_COUNT =
MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN; MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN;
export default class Client extends EventEmitter { export default class Client extends EventEmitter {
app: App; app: App | undefined;
connected: boolean; connected: boolean;
id: string; id: string;
query: ClientQuery; query: ClientQuery;
sdkVersion: number; sdkVersion: number;
messageIdCounter: number; messageIdCounter: number;
plugins: Plugins; plugins: Plugins;
lessPlugins: Plugins; lessPlugins: Plugins | undefined;
showAllPlugins: boolean; showAllPlugins: boolean;
connection: RSocketClientSocket<any, any> | null | undefined; connection: RSocketClientSocket<any, any> | null | undefined;
responder: Partial<Responder<string, any>>; responder: Partial<Responder<string, any>>;
store: Store; store: Store;
activePlugins: Set<string>; activePlugins: Set<string>;
device: Promise<BaseDevice>; device: Promise<BaseDevice> | undefined;
logger: Logger; logger: Logger;
lastSeenDeviceList: Array<BaseDevice>; lastSeenDeviceList: Array<BaseDevice>;
broadcastCallbacks: Map<string, Map<string, Set<Function>>>; broadcastCallbacks: Map<string, Map<string, Set<Function>>>;
@@ -154,7 +158,7 @@ export default class Client extends EventEmitter {
// node.js doesn't support requestIdleCallback // node.js doesn't support requestIdleCallback
const rIC = const rIC =
typeof window === 'undefined' typeof window === 'undefined'
? (cb, _) => { ? (cb: Function, _: any) => {
cb(); cb();
} }
: window.requestIdleCallback; : window.requestIdleCallback;
@@ -187,7 +191,7 @@ export default class Client extends EventEmitter {
b: typeof FlipperPlugin, b: typeof FlipperPlugin,
): number { ): number {
// Sanity check // Sanity check
if (this.lessPlugins !== null) { if (this.lessPlugins != null) {
const showPluginsCount = const showPluginsCount =
pluginsCount >= MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN pluginsCount >= MAX_MINIMUM_PLUGINS + SHOW_REMAINING_PLUGIN_IF_LESS_THAN
? MAX_MINIMUM_PLUGINS ? MAX_MINIMUM_PLUGINS
@@ -265,7 +269,7 @@ export default class Client extends EventEmitter {
this.plugins = plugins; this.plugins = plugins;
const nativeplugins = plugins const nativeplugins = plugins
.map(plugin => /_nativeplugin_([^_]+)_([^_]+)/.exec(plugin)) .map(plugin => /_nativeplugin_([^_]+)_([^_]+)/.exec(plugin))
.filter(Boolean) .filter(notNull)
.map(([id, type, title]) => { .map(([id, type, title]) => {
// TODO put this in another component, and make the "types" registerable // TODO put this in another component, and make the "types" registerable
switch (type) { switch (type) {
@@ -290,6 +294,10 @@ export default class Client extends EventEmitter {
async deviceSerial(): Promise<string> { async deviceSerial(): Promise<string> {
try { try {
const device = await this.device; const device = await this.device;
if (!device) {
console.error('Using "" for deviceId device is not ready');
return '';
}
return device.serial; return device.serial;
} catch (e) { } catch (e) {
console.error( console.error(
@@ -339,12 +347,13 @@ export default class Client extends EventEmitter {
} else if (method === 'refreshPlugins') { } else if (method === 'refreshPlugins') {
this.refreshPlugins(); this.refreshPlugins();
} else if (method === 'execute') { } else if (method === 'execute') {
const params = data.params; const params: Params = data.params as Params;
invariant(params, 'expected params'); invariant(params, 'expected params');
const persistingPlugin: const persistingPlugin:
| typeof FlipperPlugin | typeof FlipperPlugin
| typeof FlipperDevicePlugin = | typeof FlipperDevicePlugin
| undefined =
this.store.getState().plugins.clientPlugins.get(params.api) || this.store.getState().plugins.clientPlugins.get(params.api) ||
this.store.getState().plugins.devicePlugins.get(params.api); this.store.getState().plugins.devicePlugins.get(params.api);
if (persistingPlugin && persistingPlugin.persistedStateReducer) { if (persistingPlugin && persistingPlugin.persistedStateReducer) {
@@ -405,11 +414,11 @@ export default class Client extends EventEmitter {
success?: Object; success?: Object;
error?: ErrorType; error?: ErrorType;
}, },
resolve: (a: Object) => any, resolve: ((a: any) => any) | undefined,
reject: (error: ErrorType) => any, reject: (error: ErrorType) => any,
) { ) {
if (data.success) { if (data.success) {
resolve(data.success); resolve && resolve(data.success);
} else if (data.error) { } else if (data.error) {
reject(data.error); reject(data.error);
const {error} = data; const {error} = data;
@@ -427,11 +436,7 @@ export default class Client extends EventEmitter {
return {id: this.id, query: this.query}; return {id: this.id, query: this.query};
} }
subscribe( subscribe(api: string, method: string, callback: (params: Object) => void) {
api: string | null = null,
method: string,
callback: (params: Object) => void,
) {
let apiCallbacks = this.broadcastCallbacks.get(api); let apiCallbacks = this.broadcastCallbacks.get(api);
if (!apiCallbacks) { if (!apiCallbacks) {
apiCallbacks = new Map(); apiCallbacks = new Map();
@@ -446,7 +451,7 @@ export default class Client extends EventEmitter {
methodCallbacks.add(callback); methodCallbacks.add(callback);
} }
unsubscribe(api: string | null = null, method: string, callback: Function) { unsubscribe(api: string, method: string, callback: Function) {
const apiCallbacks = this.broadcastCallbacks.get(api); const apiCallbacks = this.broadcastCallbacks.get(api);
if (!apiCallbacks) { if (!apiCallbacks) {
return; return;