Files

82 lines
2.6 KiB
JavaScript

/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
global.hippyBridge = (_action, _callObj) => {
let resp = 'success';
let action = _action;
let callObj = _callObj;
if (action === 'pauseInstance') {
action = 'callJsModule';
callObj = {
methodName: 'receiveNativeEvent',
moduleName: 'EventDispatcher',
params: ['@hippy:pauseInstance', null],
};
}
if (action === 'resumeInstance') {
action = 'callJsModule';
callObj = {
methodName: 'receiveNativeEvent',
moduleName: 'EventDispatcher',
params: ['@hippy:resumeInstance', null],
};
}
switch (action) {
case 'callBack': {
if (__GLOBAL__.moduleCallList[callObj.callId]) {
const callbackObj = __GLOBAL__.moduleCallList[callObj.callId];
if (callObj.result !== 0 && typeof callbackObj.reject === 'function') {
callbackObj.reject(callObj.params);
} else {
typeof callbackObj.cb === 'function' && callbackObj.cb(callObj.params);
}
if (callbackObj.type === 0 || callbackObj.type === 1) {
delete __GLOBAL__.moduleCallList[callObj.callId];
}
} else {
resp = 'native2js error: native callback id is not registered in js';
}
break;
}
case 'callJsModule': {
if (!callObj || !callObj.moduleName || !callObj.methodName) {
resp = 'native2js error: callJsModule param is invalid';
} else {
const targetModule = __GLOBAL__.jsModuleList[callObj.moduleName];
if (!targetModule || typeof targetModule[callObj.methodName] !== 'function') {
resp = 'native2js error: callJsModule is targeting an undefined module or method';
} else {
targetModule[callObj.methodName](callObj.params);
}
}
break;
}
default: {
resp = 'native2js error: native2js action is not defined';
break;
}
}
return resp;
};