Make client.device fulfill type constraints
Summary: This is an interesting invariant that TS caught here. We expect `getDevice()` to always return a device but it cannot because it's set lazily. John Knox suggested we instead set up a promise in the constructor and resolve it instead of overriding the stateful promise later. Reviewed By: jknoxville Differential Revision: D17313468 fbshipit-source-id: 8fd75f2720546abf67beead23db56216f1a5e0df
This commit is contained in:
committed by
Facebook Github Bot
parent
a35fd7389f
commit
f48fe21eaa
@@ -115,7 +115,9 @@ export default class Client extends EventEmitter {
|
|||||||
responder: Partial<Responder<string, any>>;
|
responder: Partial<Responder<string, any>>;
|
||||||
store: Store;
|
store: Store;
|
||||||
activePlugins: Set<string>;
|
activePlugins: Set<string>;
|
||||||
device: Promise<BaseDevice> | undefined;
|
device: Promise<BaseDevice>;
|
||||||
|
_deviceResolve: (device: BaseDevice) => void = _ => {};
|
||||||
|
_deviceSet: boolean = false;
|
||||||
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,6 +156,10 @@ export default class Client extends EventEmitter {
|
|||||||
this.activePlugins = new Set();
|
this.activePlugins = new Set();
|
||||||
this.lastSeenDeviceList = [];
|
this.lastSeenDeviceList = [];
|
||||||
|
|
||||||
|
this.device = new Promise((resolve, _reject) => {
|
||||||
|
this._deviceResolve = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
const client = this;
|
const client = this;
|
||||||
// node.js doesn't support requestIdleCallback
|
// node.js doesn't support requestIdleCallback
|
||||||
const rIC =
|
const rIC =
|
||||||
@@ -211,11 +217,11 @@ export default class Client extends EventEmitter {
|
|||||||
However, clients can connect before a device is registered, so wait a
|
However, clients can connect before a device is registered, so wait a
|
||||||
while for the device to be registered if it isn't already. */
|
while for the device to be registered if it isn't already. */
|
||||||
setMatchingDevice(): void {
|
setMatchingDevice(): void {
|
||||||
if (this.device) {
|
if (this._deviceSet) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.device = reportPlatformFailures(
|
reportPlatformFailures(
|
||||||
new Promise((resolve, reject) => {
|
new Promise<BaseDevice>((resolve, reject) => {
|
||||||
const device = this.store
|
const device = this.store
|
||||||
.getState()
|
.getState()
|
||||||
.connections.devices.find(
|
.connections.devices.find(
|
||||||
@@ -248,7 +254,10 @@ export default class Client extends EventEmitter {
|
|||||||
}, 5000);
|
}, 5000);
|
||||||
}),
|
}),
|
||||||
'client-setMatchingDevice',
|
'client-setMatchingDevice',
|
||||||
);
|
).then(device => {
|
||||||
|
this._deviceSet = true;
|
||||||
|
this._deviceResolve(device);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
supportsPlugin(Plugin: typeof FlipperPlugin): boolean {
|
supportsPlugin(Plugin: typeof FlipperPlugin): boolean {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ export abstract class FlipperBasePlugin<
|
|||||||
reducers: {
|
reducers: {
|
||||||
[actionName: string]: (state: State, actionData: any) => Partial<State>;
|
[actionName: string]: (state: State, actionData: any) => Partial<State>;
|
||||||
} = {};
|
} = {};
|
||||||
app: App;
|
app: App | null = null;
|
||||||
onKeyboardAction: ((action: string) => void) | undefined;
|
onKeyboardAction: ((action: string) => void) | undefined;
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
|||||||
Reference in New Issue
Block a user