[ReactNative] Allow single callbacks in NativeModules

This commit is contained in:
Spencer Ahrens 2015-03-17 03:08:49 -07:00
parent b396de3cc8
commit 54c6a7ddff
3 changed files with 11 additions and 60 deletions

View File

@ -22,33 +22,6 @@ var MethodTypes = keyMirror({
*/ */
var BatchedBridgeFactory = { var BatchedBridgeFactory = {
MethodTypes: MethodTypes, MethodTypes: MethodTypes,
/**
* @deprecated: Remove callsites and delete this method.
*
* @param {MessageQueue} messageQueue Message queue that has been created with
* the `moduleConfig` (among others perhaps).
* @param {object} moduleConfig Configuration of module names/method
* names to callback types.
* @return {object} Remote representation of configured module.
*/
_createDeprecatedBridgedModule: function(messageQueue, moduleConfig, moduleName) {
var remoteModule = mapObject(moduleConfig.methods, function(methodConfig, memberName) {
return methodConfig.type === MethodTypes.local ? null : function() {
var lastArg = arguments.length ? arguments[arguments.length - 1] : null;
var hasCB =
typeof lastArg == 'function';
var args = slice.call(arguments, 0, arguments.length - (hasCB ? 1 : 0));
var cb = hasCB ? lastArg : null;
return messageQueue.callDeprecated(moduleName, memberName, args, cb);
};
});
for (var constName in moduleConfig.constants) {
warning(!remoteModule[constName], 'saw constant and method named %s', constName);
remoteModule[constName] = moduleConfig.constants[constName];
}
return remoteModule;
},
/** /**
* @param {MessageQueue} messageQueue Message queue that has been created with * @param {MessageQueue} messageQueue Message queue that has been created with
* the `moduleConfig` (among others perhaps). * the `moduleConfig` (among others perhaps).
@ -63,14 +36,14 @@ var BatchedBridgeFactory = {
var secondLastArg = arguments.length > 1 ? arguments[arguments.length - 2] : null; var secondLastArg = arguments.length > 1 ? arguments[arguments.length - 2] : null;
var hasSuccCB = typeof lastArg === 'function'; var hasSuccCB = typeof lastArg === 'function';
var hasErrorCB = typeof secondLastArg === 'function'; var hasErrorCB = typeof secondLastArg === 'function';
var hasCBs = hasSuccCB; hasErrorCB && invariant(
invariant( hasSuccCB,
(hasSuccCB && hasErrorCB) || (!hasSuccCB && !hasErrorCB), 'Cannot have a non-function arg after a function arg.'
'You must supply error callbacks and success callbacks or neither'
); );
var args = slice.call(arguments, 0, arguments.length - (hasCBs ? 2 : 0)); var numCBs = (hasSuccCB ? 1 : 0) + (hasErrorCB ? 1 : 0);
var onSucc = hasCBs ? lastArg : null; var args = slice.call(arguments, 0, arguments.length - numCBs);
var onFail = hasCBs ? secondLastArg : null; var onSucc = hasSuccCB ? lastArg : null;
var onFail = hasErrorCB ? secondLastArg : null;
return messageQueue.call(moduleName, memberName, args, onFail, onSucc); return messageQueue.call(moduleName, memberName, args, onFail, onSucc);
}; };
}); });
@ -93,7 +66,7 @@ var BatchedBridgeFactory = {
messageQueue.invokeCallbackAndReturnFlushedQueue.bind(messageQueue), messageQueue.invokeCallbackAndReturnFlushedQueue.bind(messageQueue),
flushedQueue: messageQueue.flushedQueue.bind(messageQueue), flushedQueue: messageQueue.flushedQueue.bind(messageQueue),
// These deprecated modules do not accept an error callback. // These deprecated modules do not accept an error callback.
RemoteModulesDeprecated: mapObject(modulesConfig, this._createDeprecatedBridgedModule.bind(this, messageQueue)), RemoteModulesDeprecated: mapObject(modulesConfig, this._createBridgedModule.bind(this, messageQueue)),
RemoteModules: mapObject(modulesConfig, this._createBridgedModule.bind(this, messageQueue)), RemoteModules: mapObject(modulesConfig, this._createBridgedModule.bind(this, messageQueue)),
setLoggingEnabled: messageQueue.setLoggingEnabled.bind(messageQueue), setLoggingEnabled: messageQueue.setLoggingEnabled.bind(messageQueue),
getLoggedOutgoingItems: messageQueue.getLoggedOutgoingItems.bind(messageQueue), getLoggedOutgoingItems: messageQueue.getLoggedOutgoingItems.bind(messageQueue),

View File

@ -6,7 +6,7 @@
*/ */
'use strict'; 'use strict';
var NativeModules = require('NativeModulesDeprecated'); var NativeModules = require('NativeModules');
var RCTAsyncLocalStorage = NativeModules.RCTAsyncLocalStorage; var RCTAsyncLocalStorage = NativeModules.RCTAsyncLocalStorage;
var RCTAsyncRocksDBStorage = NativeModules.RCTAsyncRocksDBStorage; var RCTAsyncRocksDBStorage = NativeModules.RCTAsyncRocksDBStorage;

View File

@ -415,28 +415,6 @@ var MessageQueueMixin = {
return ret; return ret;
}, },
callDeprecated: function(moduleName, methodName, params, cb, scope) {
invariant(
!cb || typeof cb === 'function',
'Last argument (callback) must be function'
);
// Store callback _before_ sending the request, just in case the MailBox
// returns the response in a blocking manner
if (cb) {
this._storeCallbacksInCurrentThread(null, cb, scope, this._POOLED_CBIDS);
params.push(this._POOLED_CBIDS.successCallbackID);
}
var moduleID = this._remoteModuleNameToModuleID[moduleName];
if (moduleID === undefined || moduleID === null) {
throw new Error('Unrecognized module name:' + moduleName);
}
var methodID = this._remoteModuleNameToMethodNameToID[moduleName][methodName];
if (methodID === undefined || moduleID === null) {
throw new Error('Unrecognized method name:' + methodName);
}
this._pushRequestToOutgoingItems(moduleID, methodID, params);
},
call: function(moduleName, methodName, params, onFail, onSucc, scope) { call: function(moduleName, methodName, params, onFail, onSucc, scope) {
invariant( invariant(
(!onFail || typeof onFail === 'function') && (!onFail || typeof onFail === 'function') &&
@ -445,9 +423,9 @@ var MessageQueueMixin = {
); );
// Store callback _before_ sending the request, just in case the MailBox // Store callback _before_ sending the request, just in case the MailBox
// returns the response in a blocking manner. // returns the response in a blocking manner.
if (onFail || onSucc) { if (onSucc) {
this._storeCallbacksInCurrentThread(onFail, onSucc, scope, this._POOLED_CBIDS); this._storeCallbacksInCurrentThread(onFail, onSucc, scope, this._POOLED_CBIDS);
params.push(this._POOLED_CBIDS.errorCallbackID); onFail && params.push(this._POOLED_CBIDS.errorCallbackID);
params.push(this._POOLED_CBIDS.successCallbackID); params.push(this._POOLED_CBIDS.successCallbackID);
} }
var moduleID = this._remoteModuleNameToModuleID[moduleName]; var moduleID = this._remoteModuleNameToModuleID[moduleName];