From 0a55e42ef67888e411987d6ae53c925f13026bb5 Mon Sep 17 00:00:00 2001 From: rh389 Date: Fri, 28 Apr 2017 03:47:19 -0700 Subject: [PATCH] Refactor MessageQueue Summary: Looking to address a few new `FlowFixMe`s I had to do some minor refactoring of `MessageQueue`. Has the advantage of (IMO) making things clearer by removing a redundant counter and some gratuitous bitwise operations. Previously `_callbacks` was an array of `?Function`s where even elements were failure callbacks and odd elements were successes. Each new call incremented `_callID` by one and `_callbackID` by two. I've changed this to use two arrays `_successCallbacks` and `_failureCallbacks` indexed by `callID`. That made the `_callbackID` counter unnecessary and reduced the need for computed indices. Tested with flow and a quick play with UIExplorer. Closes https://github.com/facebook/react-native/pull/11986 Differential Revision: D4962162 Pulled By: javache fbshipit-source-id: 17dddfedc0cb5950dbdd9bd06fae6eb6de4c4a7d --- Libraries/BatchedBridge/MessageQueue.js | 58 ++++++++++--------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index ac4c2a2bc..1248f3b09 100644 --- a/Libraries/BatchedBridge/MessageQueue.js +++ b/Libraries/BatchedBridge/MessageQueue.js @@ -44,8 +44,8 @@ const DEBUG_INFO_LIMIT = 32; class MessageQueue { _callableModules: {[key: string]: Object}; _queue: [Array, Array, Array, number]; - _callbacks: []; - _callbackID: number; + _successCallbacks: Array; + _failureCallbacks: Array; _callID: number; _inCall: number; _lastFlush: number; @@ -60,8 +60,8 @@ class MessageQueue { constructor() { this._callableModules = {}; this._queue = [[], [], [], 0]; - this._callbacks = []; - this._callbackID = 0; + this._successCallbacks = []; + this._failureCallbacks = []; this._callID = 0; this._lastFlush = 0; this._eventLoopStartTime = new Date().getTime(); @@ -143,22 +143,17 @@ class MessageQueue { enqueueNativeCall(moduleID: number, methodID: number, params: Array, onFail: ?Function, onSucc: ?Function) { if (onFail || onSucc) { if (__DEV__) { - const callId = this._callbackID >> 1; - this._debugInfo[callId] = [moduleID, methodID]; - if (callId > DEBUG_INFO_LIMIT) { - delete this._debugInfo[callId - DEBUG_INFO_LIMIT]; + this._debugInfo[this._callID] = [moduleID, methodID]; + if (this._callID > DEBUG_INFO_LIMIT) { + delete this._debugInfo[this._callID - DEBUG_INFO_LIMIT]; } } - onFail && params.push(this._callbackID); - /* $FlowFixMe(>=0.38.0 site=react_native_fb,react_native_oss) - Flow error - * detected during the deployment of v0.38.0. To see the error, remove - * this comment and run flow */ - this._callbacks[this._callbackID++] = onFail; - onSucc && params.push(this._callbackID); - /* $FlowFixMe(>=0.38.0 site=react_native_fb,react_native_oss) - Flow error - * detected during the deployment of v0.38.0. To see the error, remove - * this comment and run flow */ - this._callbacks[this._callbackID++] = onSucc; + // Encode callIDs into pairs of callback identifiers by shifting left and using the rightmost bit + // to indicate fail (0) or success (1) + onFail && params.push(this._callID << 1); + onSucc && params.push((this._callID << 1) | 1); + this._successCallbacks[this._callID] = onSucc; + this._failureCallbacks[this._callID] = onFail; } if (__DEV__) { @@ -255,13 +250,16 @@ class MessageQueue { __invokeCallback(cbID: number, args: Array) { this._lastFlush = new Date().getTime(); this._eventLoopStartTime = this._lastFlush; - const callback = this._callbacks[cbID]; + + // The rightmost bit of cbID indicates fail (0) or success (1), the other bits are the callID shifted left. + const callID = cbID >>> 1; + const callback = (cbID & 1) ? this._successCallbacks[callID] : this._failureCallbacks[callID]; if (__DEV__) { - const debug = this._debugInfo[cbID >> 1]; + const debug = this._debugInfo[callID]; const module = debug && this._remoteModuleTable[debug[0]]; const method = debug && this._remoteMethodTable[debug[0]][debug[1]]; - if (callback == null) { + if (!callback) { let errorMessage = `Callback with id ${cbID}: ${module}.${method}() not found`; if (method) { errorMessage = `The callback ${method}() exists in module ${module}, ` @@ -278,21 +276,13 @@ class MessageQueue { } Systrace.beginEvent( `MessageQueue.invokeCallback(${profileName}, ${stringifySafe(args)})`); - } else { - if (!callback) { - return; - } } - /* $FlowFixMe(>=0.38.0 site=react_native_fb,react_native_oss) - Flow error - * detected during the deployment of v0.38.0. To see the error, remove this - * comment and run flow */ - this._callbacks[cbID & ~1] = null; - /* $FlowFixMe(>=0.38.0 site=react_native_fb,react_native_oss) - Flow error - * detected during the deployment of v0.38.0. To see the error, remove this - * comment and run flow */ - this._callbacks[cbID | 1] = null; - // $FlowIssue(>=0.35.0) #14551610 + if (!callback) { + return; + } + + this._successCallbacks[callID] = this._failureCallbacks[callID] = null; callback.apply(null, args); if (__DEV__) {