Skip to content

Commit 7b575d6

Browse files
Riley Dulinfacebook-github-bot
authored andcommitted
Improve flow typing and linting for MessageQueue
Differential Revision: D5987892 fbshipit-source-id: 8b9218875944decc5e21863e3c3f3a659ff2e2e4
1 parent 1f88268 commit 7b575d6

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

Libraries/BatchedBridge/MessageQueue.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* @format
1212
*/
1313

14-
/*eslint no-bitwise: 0*/
15-
1614
'use strict';
1715

1816
const ErrorUtils = require('ErrorUtils');
@@ -26,7 +24,7 @@ export type SpyData = {
2624
type: number,
2725
module: ?string,
2826
method: string | number,
29-
args: any,
27+
args: any[],
3028
};
3129

3230
const TO_JS = 0;
@@ -37,6 +35,7 @@ const METHOD_IDS = 1;
3735
const PARAMS = 2;
3836
const MIN_TIME_BETWEEN_FLUSHES_MS = 5;
3937

38+
// eslint-disable-next-line no-bitwise
4039
const TRACE_TAG_REACT_APPS = 1 << 17;
4140

4241
const DEBUG_INFO_LIMIT = 32;
@@ -46,17 +45,17 @@ let JSTimers = null;
4645

4746
class MessageQueue {
4847
_lazyCallableModules: {[key: string]: (void) => Object};
49-
_queue: [Array<number>, Array<number>, Array<any>, number];
50-
_successCallbacks: Array<?Function>;
51-
_failureCallbacks: Array<?Function>;
48+
_queue: [number[], number[], any[], number];
49+
_successCallbacks: (?Function)[];
50+
_failureCallbacks: (?Function)[];
5251
_callID: number;
5352
_inCall: number;
5453
_lastFlush: number;
5554
_eventLoopStartTime: number;
5655

57-
_debugInfo: Object;
58-
_remoteModuleTable: Object;
59-
_remoteMethodTable: Object;
56+
_debugInfo: {[number]: [number, number]};
57+
_remoteModuleTable: {[number]: string};
58+
_remoteMethodTable: {[number]: string[]};
6059

6160
__spy: ?(data: SpyData) => void;
6261

@@ -107,11 +106,7 @@ class MessageQueue {
107106
}
108107
}
109108

110-
callFunctionReturnFlushedQueue(
111-
module: string,
112-
method: string,
113-
args: Array<any>,
114-
) {
109+
callFunctionReturnFlushedQueue(module: string, method: string, args: any[]) {
115110
this.__guard(() => {
116111
this.__callFunction(module, method, args);
117112
});
@@ -122,7 +117,7 @@ class MessageQueue {
122117
callFunctionReturnResultAndFlushedQueue(
123118
module: string,
124119
method: string,
125-
args: Array<any>,
120+
args: any[],
126121
) {
127122
let result;
128123
this.__guard(() => {
@@ -132,7 +127,7 @@ class MessageQueue {
132127
return [result, this.flushedQueue()];
133128
}
134129

135-
invokeCallbackAndReturnFlushedQueue(cbID: number, args: Array<any>) {
130+
invokeCallbackAndReturnFlushedQueue(cbID: number, args: any[]) {
136131
this.__guard(() => {
137132
this.__invokeCallback(cbID, args);
138133
});
@@ -178,7 +173,7 @@ class MessageQueue {
178173
enqueueNativeCall(
179174
moduleID: number,
180175
methodID: number,
181-
params: Array<any>,
176+
params: any[],
182177
onFail: ?Function,
183178
onSucc: ?Function,
184179
) {
@@ -191,7 +186,9 @@ class MessageQueue {
191186
}
192187
// Encode callIDs into pairs of callback identifiers by shifting left and using the rightmost bit
193188
// to indicate fail (0) or success (1)
189+
// eslint-disable-next-line no-bitwise
194190
onFail && params.push(this._callID << 1);
191+
// eslint-disable-next-line no-bitwise
195192
onSucc && params.push((this._callID << 1) | 1);
196193
this._successCallbacks[this._callID] = onSucc;
197194
this._failureCallbacks[this._callID] = onFail;
@@ -248,7 +245,7 @@ class MessageQueue {
248245
}
249246
}
250247

251-
createDebugLookup(moduleID: number, name: string, methods: Array<string>) {
248+
createDebugLookup(moduleID: number, name: string, methods: string[]) {
252249
if (__DEV__) {
253250
this._remoteModuleTable[moduleID] = name;
254251
this._remoteMethodTable[moduleID] = methods;
@@ -279,7 +276,7 @@ class MessageQueue {
279276
Systrace.endEvent();
280277
}
281278

282-
__callFunction(module: string, method: string, args: Array<any>) {
279+
__callFunction(module: string, method: string, args: any[]): any {
283280
this._lastFlush = new Date().getTime();
284281
this._eventLoopStartTime = this._lastFlush;
285282
Systrace.beginEvent(`${module}.${method}()`);
@@ -304,16 +301,18 @@ class MessageQueue {
304301
return result;
305302
}
306303

307-
__invokeCallback(cbID: number, args: Array<any>) {
304+
__invokeCallback(cbID: number, args: any[]) {
308305
this._lastFlush = new Date().getTime();
309306
this._eventLoopStartTime = this._lastFlush;
310307

311308
// The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left.
309+
// eslint-disable-next-line no-bitwise
312310
const callID = cbID >>> 1;
313-
const callback =
314-
cbID & 1
315-
? this._successCallbacks[callID]
316-
: this._failureCallbacks[callID];
311+
// eslint-disable-next-line no-bitwise
312+
const isSuccess = cbID & 1;
313+
const callback = isSuccess
314+
? this._successCallbacks[callID]
315+
: this._failureCallbacks[callID];
317316

318317
if (__DEV__) {
319318
const debug = this._debugInfo[callID];
@@ -344,7 +343,7 @@ class MessageQueue {
344343
}
345344

346345
this._successCallbacks[callID] = this._failureCallbacks[callID] = null;
347-
callback.apply(null, args);
346+
callback(...args);
348347

349348
if (__DEV__) {
350349
Systrace.endEvent();

Libraries/BatchedBridge/__tests__/MessageQueue-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ describe('MessageQueue', function() {
6565
it('should call the stored callback', () => {
6666
let done = false;
6767
queue.enqueueNativeCall(0, 1, [], () => {}, () => { done = true; });
68-
queue.__invokeCallback(1);
68+
queue.__invokeCallback(1, []);
6969
expect(done).toEqual(true);
7070
});
7171

7272
it('should throw when calling the same callback twice', () => {
7373
queue.enqueueNativeCall(0, 1, [], () => {}, () => {});
74-
queue.__invokeCallback(1);
75-
expect(() => queue.__invokeCallback(1)).toThrow();
74+
queue.__invokeCallback(1, []);
75+
expect(() => queue.__invokeCallback(1, [])).toThrow();
7676
});
7777

7878
it('should throw when calling both success and failure callback', () => {
7979
queue.enqueueNativeCall(0, 1, [], () => {}, () => {});
80-
queue.__invokeCallback(1);
81-
expect(() => queue.__invokeCallback(0)).toThrow();
80+
queue.__invokeCallback(1, []);
81+
expect(() => queue.__invokeCallback(0, [])).toThrow();
8282
});
8383

8484
it('should throw when calling with unknown module', () => {

Libraries/Interaction/BridgeSpyStallHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const BridgeSpyStallHandler = {
4545
}
4646
}
4747
return `${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` +
48-
`${info.module ? (info.module + '.') : ''}${info.method}(${args})`;
48+
`${info.module ? (info.module + '.') : ''}${info.method}(${JSON.stringify(args)})`;
4949
}),
5050
);
5151
},

0 commit comments

Comments
 (0)