2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule MessageQueue
|
2016-09-23 18:12:51 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
/*eslint no-bitwise: 0*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
'use strict';
|
2015-04-29 18:54:26 +00:00
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
const ErrorUtils = require('ErrorUtils');
|
|
|
|
const JSTimersExecution = require('JSTimersExecution');
|
2016-09-23 18:12:51 +00:00
|
|
|
const Systrace = require('Systrace');
|
2015-03-24 00:09:14 +00:00
|
|
|
|
2016-09-19 11:43:09 +00:00
|
|
|
const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationInDev');
|
2016-09-23 18:12:51 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-06-15 14:50:20 +00:00
|
|
|
const stringifySafe = require('stringifySafe');
|
2015-03-24 00:09:14 +00:00
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
export type SpyData = {
|
|
|
|
type: number,
|
|
|
|
module: ?string,
|
|
|
|
method: string|number,
|
|
|
|
args: any
|
|
|
|
}
|
|
|
|
|
|
|
|
const TO_JS = 0;
|
|
|
|
const TO_NATIVE = 1;
|
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
const MODULE_IDS = 0;
|
|
|
|
const METHOD_IDS = 1;
|
|
|
|
const PARAMS = 2;
|
|
|
|
const MIN_TIME_BETWEEN_FLUSHES_MS = 5;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-08-04 15:38:49 +00:00
|
|
|
const TRACE_TAG_REACT_APPS = 1 << 17;
|
2015-07-09 16:12:53 +00:00
|
|
|
|
2016-08-27 01:44:07 +00:00
|
|
|
const DEBUG_INFO_LIMIT = 32;
|
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
const guard = (fn) => {
|
2015-05-13 17:56:09 +00:00
|
|
|
try {
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
fn();
|
|
|
|
} catch (error) {
|
|
|
|
ErrorUtils.reportFatalError(error);
|
2015-05-13 17:56:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-06 11:13:30 +00:00
|
|
|
class MessageQueue {
|
2016-09-23 18:12:51 +00:00
|
|
|
_callableModules: {[key: string]: Object};
|
2016-09-23 18:12:54 +00:00
|
|
|
_queue: [Array<number>, Array<number>, Array<any>, number];
|
2016-09-23 18:12:51 +00:00
|
|
|
_callbacks: [];
|
|
|
|
_callbackID: number;
|
|
|
|
_callID: number;
|
|
|
|
_lastFlush: number;
|
|
|
|
_eventLoopStartTime: number;
|
|
|
|
|
|
|
|
_debugInfo: Object;
|
|
|
|
_remoteModuleTable: Object;
|
|
|
|
_remoteMethodTable: Object;
|
|
|
|
|
|
|
|
__spy: ?(data: SpyData) => void;
|
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
constructor() {
|
2015-12-08 23:57:34 +00:00
|
|
|
this._callableModules = {};
|
2016-01-04 10:15:19 +00:00
|
|
|
this._queue = [[], [], [], 0];
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
this._callbacks = [];
|
|
|
|
this._callbackID = 0;
|
2016-01-04 10:15:19 +00:00
|
|
|
this._callID = 0;
|
2015-10-13 15:00:36 +00:00
|
|
|
this._lastFlush = 0;
|
Add yieldy, chained async task support to InteractionManager
Summary:
Default behavior should be unchanged.
If we queue up a bunch of expensive tasks during an interaction, the default
`InteractionManager` behavior would execute them all in one synchronous loop at
the end the JS event loop via one `setImmediate` call, blocking the JS thread
the entire time.
The `setDeadline` addition in this diff enables an option to only execute tasks
until the `eventLoopRunningTime` is hit (added to MessageQueue/BatchedBridge),
allowing the queue execution to be paused if an interaction starts in between
tasks, making the app more responsive.
Additionally, if a task ends up generating a bunch of additional tasks
asynchronously, the previous implementation would execute these new tasks after
already scheduled tasks. This is often fine, but I want it to fully resolve
async tasks and all their dependencies before making progress in the rest of the
queue, so I added support for `type PromiseTask = {gen: () => Promise}` to do
just this. It works by building a stack of queues each time a `PromiseTask` is
started, and pops them off the stack once they are resolved and the queues are
processed.
I also pulled all of the actual queue logic out of `InteractionManager` and into
a new `TaskQueue` class to isolate concerns a bit.
public
Reviewed By: josephsavona
Differential Revision: D2754311
fb-gh-sync-id: bfd6d0c54e6410cb261aa1d2c5024dd91a3959e6
2015-12-24 00:10:39 +00:00
|
|
|
this._eventLoopStartTime = new Date().getTime();
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
this._debugInfo = {};
|
|
|
|
this._remoteModuleTable = {};
|
|
|
|
this._remoteMethodTable = {};
|
|
|
|
}
|
2016-05-06 11:13:30 +00:00
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
(this:any).callFunctionReturnFlushedQueue = this.callFunctionReturnFlushedQueue.bind(this);
|
|
|
|
(this:any).callFunctionReturnResultAndFlushedQueue = this.callFunctionReturnResultAndFlushedQueue.bind(this);
|
|
|
|
(this:any).flushedQueue = this.flushedQueue.bind(this);
|
|
|
|
(this:any).invokeCallbackAndReturnFlushedQueue = this.invokeCallbackAndReturnFlushedQueue.bind(this);
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
/**
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
* Public APIs
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2016-08-04 15:38:49 +00:00
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
static spy(spyOrToggle: boolean|(data: SpyData) => void){
|
2016-08-04 15:38:49 +00:00
|
|
|
if (spyOrToggle === true){
|
2016-09-23 18:12:51 +00:00
|
|
|
MessageQueue.prototype.__spy = info => {
|
2016-09-23 18:12:54 +00:00
|
|
|
console.log(`${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` +
|
2016-08-09 13:32:41 +00:00
|
|
|
`${info.module ? (info.module + '.') : ''}${info.method}` +
|
2016-08-04 15:38:49 +00:00
|
|
|
`(${JSON.stringify(info.args)})`);
|
2016-08-09 13:32:41 +00:00
|
|
|
};
|
2016-08-04 15:38:49 +00:00
|
|
|
} else if (spyOrToggle === false) {
|
|
|
|
MessageQueue.prototype.__spy = null;
|
|
|
|
} else {
|
|
|
|
MessageQueue.prototype.__spy = spyOrToggle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
callFunctionReturnFlushedQueue(module: string, method: string, args: Array<any>) {
|
2015-06-25 16:38:54 +00:00
|
|
|
guard(() => {
|
2015-10-13 10:22:40 +00:00
|
|
|
this.__callFunction(module, method, args);
|
|
|
|
this.__callImmediates();
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
});
|
2015-09-29 10:14:14 +00:00
|
|
|
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
return this.flushedQueue();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
callFunctionReturnResultAndFlushedQueue(module: string, method: string, args: Array<any>) {
|
2016-07-18 14:12:19 +00:00
|
|
|
let result;
|
|
|
|
guard(() => {
|
|
|
|
result = this.__callFunction(module, method, args);
|
|
|
|
this.__callImmediates();
|
|
|
|
});
|
|
|
|
|
2016-09-16 13:14:13 +00:00
|
|
|
return [result, this.flushedQueue()];
|
2016-07-18 14:12:19 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
invokeCallbackAndReturnFlushedQueue(cbID: number, args: Array<any>) {
|
2015-10-13 10:22:40 +00:00
|
|
|
guard(() => {
|
|
|
|
this.__invokeCallback(cbID, args);
|
|
|
|
this.__callImmediates();
|
|
|
|
});
|
|
|
|
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
return this.flushedQueue();
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
flushedQueue() {
|
2015-09-29 10:14:14 +00:00
|
|
|
this.__callImmediates();
|
2015-10-13 10:22:40 +00:00
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
const queue = this._queue;
|
2016-01-04 10:15:19 +00:00
|
|
|
this._queue = [[], [], [], this._callID];
|
2015-10-13 10:22:40 +00:00
|
|
|
return queue[0].length ? queue : null;
|
2015-09-29 10:14:14 +00:00
|
|
|
}
|
|
|
|
|
Add yieldy, chained async task support to InteractionManager
Summary:
Default behavior should be unchanged.
If we queue up a bunch of expensive tasks during an interaction, the default
`InteractionManager` behavior would execute them all in one synchronous loop at
the end the JS event loop via one `setImmediate` call, blocking the JS thread
the entire time.
The `setDeadline` addition in this diff enables an option to only execute tasks
until the `eventLoopRunningTime` is hit (added to MessageQueue/BatchedBridge),
allowing the queue execution to be paused if an interaction starts in between
tasks, making the app more responsive.
Additionally, if a task ends up generating a bunch of additional tasks
asynchronously, the previous implementation would execute these new tasks after
already scheduled tasks. This is often fine, but I want it to fully resolve
async tasks and all their dependencies before making progress in the rest of the
queue, so I added support for `type PromiseTask = {gen: () => Promise}` to do
just this. It works by building a stack of queues each time a `PromiseTask` is
started, and pops them off the stack once they are resolved and the queues are
processed.
I also pulled all of the actual queue logic out of `InteractionManager` and into
a new `TaskQueue` class to isolate concerns a bit.
public
Reviewed By: josephsavona
Differential Revision: D2754311
fb-gh-sync-id: bfd6d0c54e6410cb261aa1d2c5024dd91a3959e6
2015-12-24 00:10:39 +00:00
|
|
|
getEventLoopRunningTime() {
|
|
|
|
return new Date().getTime() - this._eventLoopStartTime;
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
registerCallableModule(name: string, module: Object) {
|
|
|
|
this._callableModules[name] = module;
|
2016-09-08 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
enqueueNativeCall(moduleID: number, methodID: number, params: Array<any>, onFail: ?Function, onSucc: ?Function) {
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
if (onFail || onSucc) {
|
2016-06-15 14:50:20 +00:00
|
|
|
if (__DEV__) {
|
2016-09-19 11:43:09 +00:00
|
|
|
const callId = this._callbackID >> 1;
|
2016-09-23 18:12:51 +00:00
|
|
|
this._debugInfo[callId] = [moduleID, methodID];
|
2016-08-27 01:44:07 +00:00
|
|
|
if (callId > DEBUG_INFO_LIMIT) {
|
|
|
|
delete this._debugInfo[callId - DEBUG_INFO_LIMIT];
|
|
|
|
}
|
2016-06-15 14:50:20 +00:00
|
|
|
}
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
onFail && params.push(this._callbackID);
|
2017-01-19 18:24:12 +00:00
|
|
|
/* $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 */
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
this._callbacks[this._callbackID++] = onFail;
|
|
|
|
onSucc && params.push(this._callbackID);
|
2017-01-19 18:24:12 +00:00
|
|
|
/* $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 */
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
this._callbacks[this._callbackID++] = onSucc;
|
2015-06-15 20:01:39 +00:00
|
|
|
}
|
2016-01-04 10:15:19 +00:00
|
|
|
|
2016-06-21 19:02:31 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
global.nativeTraceBeginAsyncFlow &&
|
|
|
|
global.nativeTraceBeginAsyncFlow(TRACE_TAG_REACT_APPS, 'native', this._callID);
|
|
|
|
}
|
2016-01-04 10:15:19 +00:00
|
|
|
this._callID++;
|
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
this._queue[MODULE_IDS].push(moduleID);
|
|
|
|
this._queue[METHOD_IDS].push(methodID);
|
2016-09-08 10:59:32 +00:00
|
|
|
|
2016-09-19 11:43:09 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
// Any params sent over the bridge should be encodable as JSON
|
|
|
|
JSON.stringify(params);
|
|
|
|
|
|
|
|
// The params object should not be mutated after being queued
|
2016-09-23 18:12:51 +00:00
|
|
|
deepFreezeAndThrowOnMutationInDev((params:any));
|
2016-09-19 11:43:09 +00:00
|
|
|
}
|
|
|
|
this._queue[PARAMS].push(params);
|
2015-10-13 15:00:36 +00:00
|
|
|
|
2016-06-15 14:50:20 +00:00
|
|
|
const now = new Date().getTime();
|
2015-10-13 15:00:36 +00:00
|
|
|
if (global.nativeFlushQueueImmediate &&
|
|
|
|
now - this._lastFlush >= MIN_TIME_BETWEEN_FLUSHES_MS) {
|
|
|
|
global.nativeFlushQueueImmediate(this._queue);
|
2016-01-04 10:15:19 +00:00
|
|
|
this._queue = [[], [], [], this._callID];
|
2015-10-13 15:00:36 +00:00
|
|
|
this._lastFlush = now;
|
|
|
|
}
|
2015-12-30 07:36:34 +00:00
|
|
|
Systrace.counterEvent('pending_js_to_native_queue', this._queue[0].length);
|
2016-09-23 18:12:51 +00:00
|
|
|
if (__DEV__ && this.__spy && isFinite(moduleID)) {
|
2017-02-07 01:39:15 +00:00
|
|
|
this.__spy(
|
|
|
|
{ type: TO_NATIVE,
|
|
|
|
module: this._remoteModuleTable[moduleID],
|
|
|
|
method: this._remoteMethodTable[moduleID][methodID],
|
|
|
|
args: params }
|
|
|
|
);
|
|
|
|
} else if (this.__spy) {
|
|
|
|
this.__spy({type: TO_NATIVE, module: moduleID + '', method: methodID, args: params});
|
2015-07-13 16:37:05 +00:00
|
|
|
}
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
createDebugLookup(moduleID: number, name: string, methods: Array<string>) {
|
|
|
|
if (__DEV__) {
|
|
|
|
this._remoteModuleTable[moduleID] = name;
|
|
|
|
this._remoteMethodTable[moduleID] = methods;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Private" methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
__callImmediates() {
|
|
|
|
Systrace.beginEvent('JSTimersExecution.callImmediates()');
|
|
|
|
guard(() => JSTimersExecution.callImmediates());
|
|
|
|
Systrace.endEvent();
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
__callFunction(module: string, method: string, args: Array<any>) {
|
2015-10-13 15:00:36 +00:00
|
|
|
this._lastFlush = new Date().getTime();
|
Add yieldy, chained async task support to InteractionManager
Summary:
Default behavior should be unchanged.
If we queue up a bunch of expensive tasks during an interaction, the default
`InteractionManager` behavior would execute them all in one synchronous loop at
the end the JS event loop via one `setImmediate` call, blocking the JS thread
the entire time.
The `setDeadline` addition in this diff enables an option to only execute tasks
until the `eventLoopRunningTime` is hit (added to MessageQueue/BatchedBridge),
allowing the queue execution to be paused if an interaction starts in between
tasks, making the app more responsive.
Additionally, if a task ends up generating a bunch of additional tasks
asynchronously, the previous implementation would execute these new tasks after
already scheduled tasks. This is often fine, but I want it to fully resolve
async tasks and all their dependencies before making progress in the rest of the
queue, so I added support for `type PromiseTask = {gen: () => Promise}` to do
just this. It works by building a stack of queues each time a `PromiseTask` is
started, and pops them off the stack once they are resolved and the queues are
processed.
I also pulled all of the actual queue logic out of `InteractionManager` and into
a new `TaskQueue` class to isolate concerns a bit.
public
Reviewed By: josephsavona
Differential Revision: D2754311
fb-gh-sync-id: bfd6d0c54e6410cb261aa1d2c5024dd91a3959e6
2015-12-24 00:10:39 +00:00
|
|
|
this._eventLoopStartTime = this._lastFlush;
|
2015-12-16 10:12:33 +00:00
|
|
|
Systrace.beginEvent(`${module}.${method}()`);
|
2017-02-07 01:39:15 +00:00
|
|
|
if (this.__spy) {
|
2016-08-04 15:38:49 +00:00
|
|
|
this.__spy({ type: TO_JS, module, method, args});
|
2015-07-09 16:12:53 +00:00
|
|
|
}
|
2016-06-15 14:50:20 +00:00
|
|
|
const moduleMethods = this._callableModules[module];
|
2015-12-11 07:15:38 +00:00
|
|
|
invariant(
|
|
|
|
!!moduleMethods,
|
2016-08-10 14:41:54 +00:00
|
|
|
'Module %s is not a registered callable module (calling %s)',
|
|
|
|
module, method
|
2015-12-11 07:15:38 +00:00
|
|
|
);
|
2016-08-02 18:06:19 +00:00
|
|
|
invariant(
|
|
|
|
!!moduleMethods[method],
|
|
|
|
'Method %s does not exist on module %s',
|
|
|
|
method, module
|
|
|
|
);
|
2016-07-18 14:12:19 +00:00
|
|
|
const result = moduleMethods[method].apply(moduleMethods, args);
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.endEvent();
|
2016-07-18 14:12:19 +00:00
|
|
|
return result;
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-09-23 18:12:51 +00:00
|
|
|
__invokeCallback(cbID: number, args: Array<any>) {
|
2015-10-13 15:00:36 +00:00
|
|
|
this._lastFlush = new Date().getTime();
|
Add yieldy, chained async task support to InteractionManager
Summary:
Default behavior should be unchanged.
If we queue up a bunch of expensive tasks during an interaction, the default
`InteractionManager` behavior would execute them all in one synchronous loop at
the end the JS event loop via one `setImmediate` call, blocking the JS thread
the entire time.
The `setDeadline` addition in this diff enables an option to only execute tasks
until the `eventLoopRunningTime` is hit (added to MessageQueue/BatchedBridge),
allowing the queue execution to be paused if an interaction starts in between
tasks, making the app more responsive.
Additionally, if a task ends up generating a bunch of additional tasks
asynchronously, the previous implementation would execute these new tasks after
already scheduled tasks. This is often fine, but I want it to fully resolve
async tasks and all their dependencies before making progress in the rest of the
queue, so I added support for `type PromiseTask = {gen: () => Promise}` to do
just this. It works by building a stack of queues each time a `PromiseTask` is
started, and pops them off the stack once they are resolved and the queues are
processed.
I also pulled all of the actual queue logic out of `InteractionManager` and into
a new `TaskQueue` class to isolate concerns a bit.
public
Reviewed By: josephsavona
Differential Revision: D2754311
fb-gh-sync-id: bfd6d0c54e6410cb261aa1d2c5024dd91a3959e6
2015-12-24 00:10:39 +00:00
|
|
|
this._eventLoopStartTime = this._lastFlush;
|
2016-06-15 14:50:20 +00:00
|
|
|
const callback = this._callbacks[cbID];
|
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
const debug = this._debugInfo[cbID >> 1];
|
|
|
|
const module = debug && this._remoteModuleTable[debug[0]];
|
|
|
|
const method = debug && this._remoteMethodTable[debug[0]][debug[1]];
|
2016-11-15 04:39:21 +00:00
|
|
|
if (callback == null) {
|
2016-06-23 10:06:58 +00:00
|
|
|
let errorMessage = `Callback with id ${cbID}: ${module}.${method}() not found`;
|
2016-06-15 14:50:20 +00:00
|
|
|
if (method) {
|
|
|
|
errorMessage = `The callback ${method}() exists in module ${module}, `
|
2016-08-02 18:06:19 +00:00
|
|
|
+ 'but only one callback may be registered to a function in a native module.';
|
2016-06-15 14:50:20 +00:00
|
|
|
}
|
|
|
|
invariant(
|
|
|
|
callback,
|
|
|
|
errorMessage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const profileName = debug ? '<callback for ' + module + '.' + method + '>' : cbID;
|
2017-02-07 01:39:15 +00:00
|
|
|
if (callback && this.__spy) {
|
2016-08-04 15:38:49 +00:00
|
|
|
this.__spy({ type: TO_JS, module:null, method:profileName, args });
|
2016-06-15 14:50:20 +00:00
|
|
|
}
|
|
|
|
Systrace.beginEvent(
|
|
|
|
`MessageQueue.invokeCallback(${profileName}, ${stringifySafe(args)})`);
|
|
|
|
} else {
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
2016-03-23 17:07:45 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2016-06-15 14:50:20 +00:00
|
|
|
|
2017-01-19 18:24:12 +00:00
|
|
|
/* $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 */
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
this._callbacks[cbID & ~1] = null;
|
2017-01-19 18:24:12 +00:00
|
|
|
/* $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 */
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
this._callbacks[cbID | 1] = null;
|
2016-11-15 04:39:21 +00:00
|
|
|
// $FlowIssue(>=0.35.0) #14551610
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
callback.apply(null, args);
|
2016-06-15 14:50:20 +00:00
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
}
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
module.exports = MessageQueue;
|