plus-message.js 930 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import {
  publish
} from '../bridge'

const callbacks = {}
const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE'
// 简单处理 view 层与 service 层的通知系统
/**
 * 消费 view 层通知
 */
export function consumePlusMessage (type, args) {
  // 处理 web-view 组件发送的通知
  if (type === WEB_INVOKE_APPSERVICE) {
    publish(WEB_INVOKE_APPSERVICE, args.data, args.webviewIds)
    return true
  }
  const callback = callbacks[type]
  if (callback) {
    callback(args)
    if (!callback.keepAlive) {
      delete callbacks[type]
    }
    return true
  }
  return false
}
/**
 * 注册 view 层通知 service 层事件处理
 */
export function registerPlusMessage (type, callback, keepAlive = true) {
  if (callbacks[type]) {
fxy060608's avatar
fxy060608 已提交
32
    return console.warn(`${type} 已注册:` + (callbacks[type].toString()))
fxy060608's avatar
fxy060608 已提交
33 34 35 36
  }
  callback.keepAlive = !!keepAlive
  callbacks[type] = callback
}