'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
32
33
34
35
36
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]) {
    return console.warn(`'${type}' registered: ` + (callbacks[type].toString()))
  }
  callback.keepAlive = !!keepAlive
  callbacks[type] = callback
}