提交 af42895d 编写于 作者: fxy060608's avatar fxy060608

refactor(v3): onNativeEventReceive

上级 06fcd97d
......@@ -87,8 +87,8 @@ function createKeepAliveApiCallback (apiName, callback) {
const callbackId = invokeCallbackId++
const invokeCallbackName = 'api.' + apiName + '.' + callbackId
const invokeCallback = function (res) {
callback(res)
const invokeCallback = function (res, extras) {
callback(res, extras)
}
invokeCallbacks[callbackId] = {
......@@ -163,11 +163,11 @@ function createApiCallback (apiName, params = {}, extras = {}) {
res.errMsg = apiName + ':ok'
} else if (res.errMsg.indexOf(':cancel') !== -1) {
res.errMsg = apiName + ':cancel'
} else if (res.errMsg.indexOf(':fail') !== -1) {
let errDetail = ''
let spaceIndex = res.errMsg.indexOf(' ')
if (spaceIndex > -1) {
errDetail = res.errMsg.substr(spaceIndex)
} else if (res.errMsg.indexOf(':fail') !== -1) {
let errDetail = ''
let spaceIndex = res.errMsg.indexOf(' ')
if (spaceIndex > -1) {
errDetail = res.errMsg.substr(spaceIndex)
}
res.errMsg = apiName + ':fail' + errDetail
}
......@@ -232,15 +232,15 @@ function createInvokeCallback (apiName, params = {}, extras = {}) {
callbackId
}
}
export function invokeCallbackHandler (invokeCallbackId, res) {
// onNativeEventReceive((event,data)=>{}) 需要两个参数,写死最多两个参数,避免改动太大,影响已有逻辑
export function invokeCallbackHandler (invokeCallbackId, res, extras) {
if (typeof invokeCallbackId === 'number') {
const invokeCallback = invokeCallbacks[invokeCallbackId]
if (invokeCallback) {
if (!invokeCallback.keepAlive) {
delete invokeCallbacks[invokeCallbackId]
}
return invokeCallback.callback(res)
return invokeCallback.callback(res, extras)
}
}
return res
......
import {
isFn
}
from 'uni-shared'
invoke
} from 'uni-core/service/bridge'
import {
onMethod
} from 'uni-core/service/platform'
const callbacks = []
export function onNativeEventReceive (callback) {
isFn(callback) &&
callbacks.indexOf(callback) === -1 &&
callbacks.push(callback)
}
onMethod('uniMPNativeEvent', function (res) {
callbacks.forEach(callbackId => {
invoke(callbackId, res.event, res.data)
})
})
export function consumeNativeEvent (event, data) {
callbacks.forEach(callback => callback(event, data))
export function onNativeEventReceive (callbackId) {
callbacks.push(callbackId)
}
......@@ -35,10 +35,6 @@ import {
backbuttonListener
} from './backbutton'
import {
consumeNativeEvent
} from '../api/plugin/on-native-event-receive'
let appCtx
const defaultApp = {
......@@ -96,11 +92,8 @@ function initGlobalListeners () {
})
})
globalEvent.addEventListener('uniMPNativeEvent', function ({
event,
data
}) {
consumeNativeEvent(event, data)
globalEvent.addEventListener('uniMPNativeEvent', function (event) {
publish('uniMPNativeEvent', event)
})
plus.globalEvent.addEventListener('plusMessage', onPlusMessage)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册