[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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-18 16:56:33 +00:00
|
|
|
jest.setMock('ReactUpdates', {
|
|
|
|
batchedUpdates: fn => fn()
|
|
|
|
});
|
[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-06-18 16:56:33 +00:00
|
|
|
jest.dontMock('MessageQueue');
|
|
|
|
jest.dontMock('keyMirror');
|
[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
|
|
|
var MessageQueue = require('MessageQueue');
|
|
|
|
|
|
|
|
let MODULE_IDS = 0;
|
|
|
|
let METHOD_IDS = 1;
|
|
|
|
let PARAMS = 2;
|
|
|
|
|
|
|
|
let TestModule = {
|
|
|
|
testHook1(){}, testHook2(){},
|
|
|
|
};
|
|
|
|
|
|
|
|
let customRequire = (moduleName) => TestModule;
|
|
|
|
|
|
|
|
let assertQueue = (flushedQueue, index, moduleID, methodID, params) => {
|
|
|
|
expect(flushedQueue[MODULE_IDS][index]).toEqual(moduleID);
|
|
|
|
expect(flushedQueue[METHOD_IDS][index]).toEqual(methodID);
|
|
|
|
expect(flushedQueue[PARAMS][index]).toEqual(params);
|
|
|
|
};
|
|
|
|
|
|
|
|
var queue;
|
|
|
|
|
|
|
|
describe('MessageQueue', () => {
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
queue = new MessageQueue(
|
|
|
|
remoteModulesConfig,
|
|
|
|
localModulesConfig,
|
|
|
|
customRequire,
|
|
|
|
);
|
|
|
|
|
|
|
|
TestModule.testHook1 = jasmine.createSpy();
|
|
|
|
TestModule.testHook2 = jasmine.createSpy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should enqueue native calls', () => {
|
|
|
|
queue.__nativeCall(0, 1, [2]);
|
|
|
|
let flushedQueue = queue.flushedQueue();
|
|
|
|
assertQueue(flushedQueue, 0, 0, 1, [2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call a local function with id', () => {
|
|
|
|
expect(TestModule.testHook1.callCount).toEqual(0);
|
|
|
|
queue.__callFunction(0, 0, [1]);
|
|
|
|
expect(TestModule.testHook1.callCount).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call a local function with the function name', () => {
|
|
|
|
expect(TestModule.testHook2.callCount).toEqual(0);
|
|
|
|
queue.__callFunction('one', 'testHook2', [2]);
|
|
|
|
expect(TestModule.testHook2.callCount).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate native modules', () => {
|
|
|
|
queue.RemoteModules.one.remoteMethod1('foo');
|
|
|
|
let flushedQueue = queue.flushedQueue();
|
|
|
|
assertQueue(flushedQueue, 0, 0, 0, ['foo']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store callbacks', () => {
|
|
|
|
queue.RemoteModules.one.remoteMethod2('foo', () => {}, () => {});
|
|
|
|
let flushedQueue = queue.flushedQueue();
|
|
|
|
assertQueue(flushedQueue, 0, 0, 1, ['foo', 0, 1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the stored callback', (done) => {
|
|
|
|
var done = false;
|
|
|
|
queue.RemoteModules.one.remoteMethod1(() => { done = true; });
|
|
|
|
queue.__invokeCallback(1);
|
|
|
|
expect(done).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when calling the same callback twice', () => {
|
|
|
|
queue.RemoteModules.one.remoteMethod1(() => {});
|
|
|
|
queue.__invokeCallback(1);
|
|
|
|
expect(() => queue.__invokeCallback(1)).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when calling both success and failure callback', () => {
|
|
|
|
queue.RemoteModules.one.remoteMethod1(() => {}, () => {});
|
|
|
|
queue.__invokeCallback(1);
|
|
|
|
expect(() => queue.__invokeCallback(0)).toThrow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var remoteModulesConfig = {
|
|
|
|
'one': {
|
|
|
|
'moduleID':0,
|
|
|
|
'methods': {
|
|
|
|
'remoteMethod1':{ 'type': 'remote', 'methodID': 0 },
|
|
|
|
'remoteMethod2':{ 'type': 'remote', 'methodID': 1 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var localModulesConfig = {
|
|
|
|
'one': {
|
|
|
|
'moduleID': 0,
|
|
|
|
'methods': {
|
|
|
|
'testHook1':{ 'type': 'local', 'methodID': 0 },
|
|
|
|
'testHook2':{ 'type': 'local', 'methodID': 1 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|