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

refactor(v3): onNativeEventReceive

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