From 7b575d669d8d6806f17fb679aa34ecb05c75883a Mon Sep 17 00:00:00 2001 From: Riley Dulin Date: Tue, 10 Oct 2017 16:20:15 -0700 Subject: [PATCH] Improve flow typing and linting for MessageQueue Differential Revision: D5987892 fbshipit-source-id: 8b9218875944decc5e21863e3c3f3a659ff2e2e4 --- Libraries/BatchedBridge/MessageQueue.js | 49 +++++++++---------- .../__tests__/MessageQueue-test.js | 10 ++-- .../Interaction/BridgeSpyStallHandler.js | 2 +- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index f7c296bc6d8..0f017bfb481 100644 --- a/Libraries/BatchedBridge/MessageQueue.js +++ b/Libraries/BatchedBridge/MessageQueue.js @@ -11,8 +11,6 @@ * @format */ -/*eslint no-bitwise: 0*/ - 'use strict'; const ErrorUtils = require('ErrorUtils'); @@ -26,7 +24,7 @@ export type SpyData = { type: number, module: ?string, method: string | number, - args: any, + args: any[], }; const TO_JS = 0; @@ -37,6 +35,7 @@ const METHOD_IDS = 1; const PARAMS = 2; const MIN_TIME_BETWEEN_FLUSHES_MS = 5; +// eslint-disable-next-line no-bitwise const TRACE_TAG_REACT_APPS = 1 << 17; const DEBUG_INFO_LIMIT = 32; @@ -46,17 +45,17 @@ let JSTimers = null; class MessageQueue { _lazyCallableModules: {[key: string]: (void) => Object}; - _queue: [Array, Array, Array, number]; - _successCallbacks: Array; - _failureCallbacks: Array; + _queue: [number[], number[], any[], number]; + _successCallbacks: (?Function)[]; + _failureCallbacks: (?Function)[]; _callID: number; _inCall: number; _lastFlush: number; _eventLoopStartTime: number; - _debugInfo: Object; - _remoteModuleTable: Object; - _remoteMethodTable: Object; + _debugInfo: {[number]: [number, number]}; + _remoteModuleTable: {[number]: string}; + _remoteMethodTable: {[number]: string[]}; __spy: ?(data: SpyData) => void; @@ -107,11 +106,7 @@ class MessageQueue { } } - callFunctionReturnFlushedQueue( - module: string, - method: string, - args: Array, - ) { + callFunctionReturnFlushedQueue(module: string, method: string, args: any[]) { this.__guard(() => { this.__callFunction(module, method, args); }); @@ -122,7 +117,7 @@ class MessageQueue { callFunctionReturnResultAndFlushedQueue( module: string, method: string, - args: Array, + args: any[], ) { let result; this.__guard(() => { @@ -132,7 +127,7 @@ class MessageQueue { return [result, this.flushedQueue()]; } - invokeCallbackAndReturnFlushedQueue(cbID: number, args: Array) { + invokeCallbackAndReturnFlushedQueue(cbID: number, args: any[]) { this.__guard(() => { this.__invokeCallback(cbID, args); }); @@ -178,7 +173,7 @@ class MessageQueue { enqueueNativeCall( moduleID: number, methodID: number, - params: Array, + params: any[], onFail: ?Function, onSucc: ?Function, ) { @@ -191,7 +186,9 @@ class MessageQueue { } // Encode callIDs into pairs of callback identifiers by shifting left and using the rightmost bit // to indicate fail (0) or success (1) + // eslint-disable-next-line no-bitwise onFail && params.push(this._callID << 1); + // eslint-disable-next-line no-bitwise onSucc && params.push((this._callID << 1) | 1); this._successCallbacks[this._callID] = onSucc; this._failureCallbacks[this._callID] = onFail; @@ -248,7 +245,7 @@ class MessageQueue { } } - createDebugLookup(moduleID: number, name: string, methods: Array) { + createDebugLookup(moduleID: number, name: string, methods: string[]) { if (__DEV__) { this._remoteModuleTable[moduleID] = name; this._remoteMethodTable[moduleID] = methods; @@ -279,7 +276,7 @@ class MessageQueue { Systrace.endEvent(); } - __callFunction(module: string, method: string, args: Array) { + __callFunction(module: string, method: string, args: any[]): any { this._lastFlush = new Date().getTime(); this._eventLoopStartTime = this._lastFlush; Systrace.beginEvent(`${module}.${method}()`); @@ -304,16 +301,18 @@ class MessageQueue { return result; } - __invokeCallback(cbID: number, args: Array) { + __invokeCallback(cbID: number, args: any[]) { this._lastFlush = new Date().getTime(); this._eventLoopStartTime = this._lastFlush; // 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 callback = - cbID & 1 - ? this._successCallbacks[callID] - : this._failureCallbacks[callID]; + // eslint-disable-next-line no-bitwise + const isSuccess = cbID & 1; + const callback = isSuccess + ? this._successCallbacks[callID] + : this._failureCallbacks[callID]; if (__DEV__) { const debug = this._debugInfo[callID]; @@ -344,7 +343,7 @@ class MessageQueue { } this._successCallbacks[callID] = this._failureCallbacks[callID] = null; - callback.apply(null, args); + callback(...args); if (__DEV__) { Systrace.endEvent(); diff --git a/Libraries/BatchedBridge/__tests__/MessageQueue-test.js b/Libraries/BatchedBridge/__tests__/MessageQueue-test.js index d9cd5a4e358..d48c77313e0 100644 --- a/Libraries/BatchedBridge/__tests__/MessageQueue-test.js +++ b/Libraries/BatchedBridge/__tests__/MessageQueue-test.js @@ -65,20 +65,20 @@ describe('MessageQueue', function() { it('should call the stored callback', () => { let done = false; queue.enqueueNativeCall(0, 1, [], () => {}, () => { done = true; }); - queue.__invokeCallback(1); + queue.__invokeCallback(1, []); expect(done).toEqual(true); }); it('should throw when calling the same callback twice', () => { queue.enqueueNativeCall(0, 1, [], () => {}, () => {}); - queue.__invokeCallback(1); - expect(() => queue.__invokeCallback(1)).toThrow(); + queue.__invokeCallback(1, []); + expect(() => queue.__invokeCallback(1, [])).toThrow(); }); it('should throw when calling both success and failure callback', () => { queue.enqueueNativeCall(0, 1, [], () => {}, () => {}); - queue.__invokeCallback(1); - expect(() => queue.__invokeCallback(0)).toThrow(); + queue.__invokeCallback(1, []); + expect(() => queue.__invokeCallback(0, [])).toThrow(); }); it('should throw when calling with unknown module', () => { diff --git a/Libraries/Interaction/BridgeSpyStallHandler.js b/Libraries/Interaction/BridgeSpyStallHandler.js index 920dab5f9db..78d61c0321b 100644 --- a/Libraries/Interaction/BridgeSpyStallHandler.js +++ b/Libraries/Interaction/BridgeSpyStallHandler.js @@ -45,7 +45,7 @@ const BridgeSpyStallHandler = { } } 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)})`; }), ); },