Make MessageQueue.RemoteModules lazy
Reviewed By: javache Differential Revision: D3252400 fb-gh-sync-id: 11a31fd2e1e41c6a3ad538b08ef177207780bd88 fbshipit-source-id: 11a31fd2e1e41c6a3ad538b08ef177207780bd88
This commit is contained in:
parent
deef8aade2
commit
097f59112f
|
@ -13,8 +13,7 @@
|
||||||
const MessageQueue = require('MessageQueue');
|
const MessageQueue = require('MessageQueue');
|
||||||
|
|
||||||
const BatchedBridge = new MessageQueue(
|
const BatchedBridge = new MessageQueue(
|
||||||
__fbBatchedBridgeConfig.remoteModuleConfig,
|
() => global.__fbBatchedBridgeConfig
|
||||||
__fbBatchedBridgeConfig.localModulesConfig,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Move these around to solve the cycle in a cleaner way.
|
// TODO: Move these around to solve the cycle in a cleaner way.
|
||||||
|
|
|
@ -46,11 +46,13 @@ var guard = (fn) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Config = {
|
||||||
|
remoteModuleConfig: Object,
|
||||||
|
};
|
||||||
|
|
||||||
class MessageQueue {
|
class MessageQueue {
|
||||||
|
|
||||||
constructor(remoteModules, localModules) {
|
constructor(configProvider: () => Config) {
|
||||||
this.RemoteModules = {};
|
|
||||||
|
|
||||||
this._callableModules = {};
|
this._callableModules = {};
|
||||||
this._queue = [[], [], [], 0];
|
this._queue = [[], [], [], 0];
|
||||||
this._callbacks = [];
|
this._callbacks = [];
|
||||||
|
@ -59,21 +61,27 @@ class MessageQueue {
|
||||||
this._lastFlush = 0;
|
this._lastFlush = 0;
|
||||||
this._eventLoopStartTime = new Date().getTime();
|
this._eventLoopStartTime = new Date().getTime();
|
||||||
|
|
||||||
|
this._debugInfo = {};
|
||||||
|
this._remoteModuleTable = {};
|
||||||
|
this._remoteMethodTable = {};
|
||||||
|
|
||||||
[
|
[
|
||||||
'invokeCallbackAndReturnFlushedQueue',
|
'invokeCallbackAndReturnFlushedQueue',
|
||||||
'callFunctionReturnFlushedQueue',
|
'callFunctionReturnFlushedQueue',
|
||||||
'flushedQueue',
|
'flushedQueue',
|
||||||
].forEach((fn) => this[fn] = this[fn].bind(this));
|
].forEach((fn) => this[fn] = this[fn].bind(this));
|
||||||
|
|
||||||
let modulesConfig = this._genModulesConfig(remoteModules);
|
lazyProperty(this, 'RemoteModules', () => {
|
||||||
this._genModules(modulesConfig);
|
let {remoteModuleConfig} = configProvider();
|
||||||
|
let modulesConfig = this._genModulesConfig(remoteModuleConfig);
|
||||||
|
let modules = this._genModules(modulesConfig);
|
||||||
|
|
||||||
this._debugInfo = {};
|
this._genLookupTables(
|
||||||
this._remoteModuleTable = {};
|
modulesConfig, this._remoteModuleTable, this._remoteMethodTable
|
||||||
this._remoteMethodTable = {};
|
);
|
||||||
this._genLookupTables(
|
|
||||||
modulesConfig, this._remoteModuleTable, this._remoteMethodTable
|
return modules;
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,6 +115,7 @@ class MessageQueue {
|
||||||
|
|
||||||
processModuleConfig(config, moduleID) {
|
processModuleConfig(config, moduleID) {
|
||||||
const module = this._genModule(config, moduleID);
|
const module = this._genModule(config, moduleID);
|
||||||
|
this.RemoteModules[module.name] = module;
|
||||||
this._genLookup(config, moduleID, this._remoteModuleTable, this._remoteMethodTable);
|
this._genLookup(config, moduleID, this._remoteModuleTable, this._remoteMethodTable);
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
@ -280,14 +289,21 @@ class MessageQueue {
|
||||||
}
|
}
|
||||||
|
|
||||||
_genModules(remoteModules) {
|
_genModules(remoteModules) {
|
||||||
|
let modules = {};
|
||||||
|
|
||||||
remoteModules.forEach((config, moduleID) => {
|
remoteModules.forEach((config, moduleID) => {
|
||||||
this._genModule(config, moduleID);
|
let module = this._genModule(config, moduleID);
|
||||||
|
if (module) {
|
||||||
|
modules[module.name] = module;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return modules;
|
||||||
}
|
}
|
||||||
|
|
||||||
_genModule(config, moduleID) {
|
_genModule(config, moduleID): ?Object {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let moduleName, constants, methods, asyncMethods, syncHooks;
|
let moduleName, constants, methods, asyncMethods, syncHooks;
|
||||||
|
@ -297,7 +313,9 @@ class MessageQueue {
|
||||||
[moduleName, methods, asyncMethods, syncHooks] = config;
|
[moduleName, methods, asyncMethods, syncHooks] = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
let module = {};
|
let module = {
|
||||||
|
name: moduleName
|
||||||
|
};
|
||||||
methods && methods.forEach((methodName, methodID) => {
|
methods && methods.forEach((methodName, methodID) => {
|
||||||
const isAsync = asyncMethods && arrayContains(asyncMethods, methodID);
|
const isAsync = asyncMethods && arrayContains(asyncMethods, methodID);
|
||||||
const isSyncHook = syncHooks && arrayContains(syncHooks, methodID);
|
const isSyncHook = syncHooks && arrayContains(syncHooks, methodID);
|
||||||
|
@ -313,7 +331,6 @@ class MessageQueue {
|
||||||
module.moduleID = moduleID;
|
module.moduleID = moduleID;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.RemoteModules[moduleName] = module;
|
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -385,4 +402,21 @@ function createErrorFromErrorData(errorData: {message: string}): Error {
|
||||||
return Object.assign(error, extraErrorInfo);
|
return Object.assign(error, extraErrorInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lazyProperty(target: Object, name: string, f: () => any) {
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
get() {
|
||||||
|
const value = f();
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
configurable: true,
|
||||||
|
enumerable: true,
|
||||||
|
writeable: true,
|
||||||
|
value: value,
|
||||||
|
});
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = MessageQueue;
|
module.exports = MessageQueue;
|
||||||
|
|
|
@ -32,8 +32,7 @@ describe('MessageQueue', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
queue = new MessageQueue(
|
queue = new MessageQueue(
|
||||||
remoteModulesConfig,
|
() => ({ remoteModuleConfig: remoteModulesConfig })
|
||||||
localModulesConfig
|
|
||||||
);
|
);
|
||||||
|
|
||||||
queue.registerCallableModule('one', TestModule);
|
queue.registerCallableModule('one', TestModule);
|
||||||
|
|
Loading…
Reference in New Issue