Improve flow typing and linting for MessageQueue

Differential Revision: D5987892

fbshipit-source-id: 8b9218875944decc5e21863e3c3f3a659ff2e2e4
This commit is contained in:
Riley Dulin 2017-10-10 16:20:15 -07:00 committed by Facebook Github Bot
parent 1f8826815d
commit 7b575d669d
3 changed files with 30 additions and 31 deletions

View File

@ -11,8 +11,6 @@
* @format * @format
*/ */
/*eslint no-bitwise: 0*/
'use strict'; 'use strict';
const ErrorUtils = require('ErrorUtils'); const ErrorUtils = require('ErrorUtils');
@ -26,7 +24,7 @@ export type SpyData = {
type: number, type: number,
module: ?string, module: ?string,
method: string | number, method: string | number,
args: any, args: any[],
}; };
const TO_JS = 0; const TO_JS = 0;
@ -37,6 +35,7 @@ const METHOD_IDS = 1;
const PARAMS = 2; const PARAMS = 2;
const MIN_TIME_BETWEEN_FLUSHES_MS = 5; const MIN_TIME_BETWEEN_FLUSHES_MS = 5;
// eslint-disable-next-line no-bitwise
const TRACE_TAG_REACT_APPS = 1 << 17; const TRACE_TAG_REACT_APPS = 1 << 17;
const DEBUG_INFO_LIMIT = 32; const DEBUG_INFO_LIMIT = 32;
@ -46,17 +45,17 @@ let JSTimers = null;
class MessageQueue { class MessageQueue {
_lazyCallableModules: {[key: string]: (void) => Object}; _lazyCallableModules: {[key: string]: (void) => Object};
_queue: [Array<number>, Array<number>, Array<any>, number]; _queue: [number[], number[], any[], number];
_successCallbacks: Array<?Function>; _successCallbacks: (?Function)[];
_failureCallbacks: Array<?Function>; _failureCallbacks: (?Function)[];
_callID: number; _callID: number;
_inCall: number; _inCall: number;
_lastFlush: number; _lastFlush: number;
_eventLoopStartTime: number; _eventLoopStartTime: number;
_debugInfo: Object; _debugInfo: {[number]: [number, number]};
_remoteModuleTable: Object; _remoteModuleTable: {[number]: string};
_remoteMethodTable: Object; _remoteMethodTable: {[number]: string[]};
__spy: ?(data: SpyData) => void; __spy: ?(data: SpyData) => void;
@ -107,11 +106,7 @@ class MessageQueue {
} }
} }
callFunctionReturnFlushedQueue( callFunctionReturnFlushedQueue(module: string, method: string, args: any[]) {
module: string,
method: string,
args: Array<any>,
) {
this.__guard(() => { this.__guard(() => {
this.__callFunction(module, method, args); this.__callFunction(module, method, args);
}); });
@ -122,7 +117,7 @@ class MessageQueue {
callFunctionReturnResultAndFlushedQueue( callFunctionReturnResultAndFlushedQueue(
module: string, module: string,
method: string, method: string,
args: Array<any>, args: any[],
) { ) {
let result; let result;
this.__guard(() => { this.__guard(() => {
@ -132,7 +127,7 @@ class MessageQueue {
return [result, this.flushedQueue()]; return [result, this.flushedQueue()];
} }
invokeCallbackAndReturnFlushedQueue(cbID: number, args: Array<any>) { invokeCallbackAndReturnFlushedQueue(cbID: number, args: any[]) {
this.__guard(() => { this.__guard(() => {
this.__invokeCallback(cbID, args); this.__invokeCallback(cbID, args);
}); });
@ -178,7 +173,7 @@ class MessageQueue {
enqueueNativeCall( enqueueNativeCall(
moduleID: number, moduleID: number,
methodID: number, methodID: number,
params: Array<any>, params: any[],
onFail: ?Function, onFail: ?Function,
onSucc: ?Function, onSucc: ?Function,
) { ) {
@ -191,7 +186,9 @@ class MessageQueue {
} }
// Encode callIDs into pairs of callback identifiers by shifting left and using the rightmost bit // Encode callIDs into pairs of callback identifiers by shifting left and using the rightmost bit
// to indicate fail (0) or success (1) // to indicate fail (0) or success (1)
// eslint-disable-next-line no-bitwise
onFail && params.push(this._callID << 1); onFail && params.push(this._callID << 1);
// eslint-disable-next-line no-bitwise
onSucc && params.push((this._callID << 1) | 1); onSucc && params.push((this._callID << 1) | 1);
this._successCallbacks[this._callID] = onSucc; this._successCallbacks[this._callID] = onSucc;
this._failureCallbacks[this._callID] = onFail; this._failureCallbacks[this._callID] = onFail;
@ -248,7 +245,7 @@ class MessageQueue {
} }
} }
createDebugLookup(moduleID: number, name: string, methods: Array<string>) { createDebugLookup(moduleID: number, name: string, methods: string[]) {
if (__DEV__) { if (__DEV__) {
this._remoteModuleTable[moduleID] = name; this._remoteModuleTable[moduleID] = name;
this._remoteMethodTable[moduleID] = methods; this._remoteMethodTable[moduleID] = methods;
@ -279,7 +276,7 @@ class MessageQueue {
Systrace.endEvent(); Systrace.endEvent();
} }
__callFunction(module: string, method: string, args: Array<any>) { __callFunction(module: string, method: string, args: any[]): any {
this._lastFlush = new Date().getTime(); this._lastFlush = new Date().getTime();
this._eventLoopStartTime = this._lastFlush; this._eventLoopStartTime = this._lastFlush;
Systrace.beginEvent(`${module}.${method}()`); Systrace.beginEvent(`${module}.${method}()`);
@ -304,14 +301,16 @@ class MessageQueue {
return result; return result;
} }
__invokeCallback(cbID: number, args: Array<any>) { __invokeCallback(cbID: number, args: any[]) {
this._lastFlush = new Date().getTime(); this._lastFlush = new Date().getTime();
this._eventLoopStartTime = this._lastFlush; this._eventLoopStartTime = this._lastFlush;
// The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left. // The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left.
// eslint-disable-next-line no-bitwise
const callID = cbID >>> 1; const callID = cbID >>> 1;
const callback = // eslint-disable-next-line no-bitwise
cbID & 1 const isSuccess = cbID & 1;
const callback = isSuccess
? this._successCallbacks[callID] ? this._successCallbacks[callID]
: this._failureCallbacks[callID]; : this._failureCallbacks[callID];
@ -344,7 +343,7 @@ class MessageQueue {
} }
this._successCallbacks[callID] = this._failureCallbacks[callID] = null; this._successCallbacks[callID] = this._failureCallbacks[callID] = null;
callback.apply(null, args); callback(...args);
if (__DEV__) { if (__DEV__) {
Systrace.endEvent(); Systrace.endEvent();

View File

@ -65,20 +65,20 @@ describe('MessageQueue', function() {
it('should call the stored callback', () => { it('should call the stored callback', () => {
let done = false; let done = false;
queue.enqueueNativeCall(0, 1, [], () => {}, () => { done = true; }); queue.enqueueNativeCall(0, 1, [], () => {}, () => { done = true; });
queue.__invokeCallback(1); queue.__invokeCallback(1, []);
expect(done).toEqual(true); expect(done).toEqual(true);
}); });
it('should throw when calling the same callback twice', () => { it('should throw when calling the same callback twice', () => {
queue.enqueueNativeCall(0, 1, [], () => {}, () => {}); queue.enqueueNativeCall(0, 1, [], () => {}, () => {});
queue.__invokeCallback(1); queue.__invokeCallback(1, []);
expect(() => queue.__invokeCallback(1)).toThrow(); expect(() => queue.__invokeCallback(1, [])).toThrow();
}); });
it('should throw when calling both success and failure callback', () => { it('should throw when calling both success and failure callback', () => {
queue.enqueueNativeCall(0, 1, [], () => {}, () => {}); queue.enqueueNativeCall(0, 1, [], () => {}, () => {});
queue.__invokeCallback(1); queue.__invokeCallback(1, []);
expect(() => queue.__invokeCallback(0)).toThrow(); expect(() => queue.__invokeCallback(0, [])).toThrow();
}); });
it('should throw when calling with unknown module', () => { it('should throw when calling with unknown module', () => {

View File

@ -45,7 +45,7 @@ const BridgeSpyStallHandler = {
} }
} }
return `${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` + return `${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` +
`${info.module ? (info.module + '.') : ''}${info.method}(${args})`; `${info.module ? (info.module + '.') : ''}${info.method}(${JSON.stringify(args)})`;
}), }),
); );
}, },