fcc-core
Version:
Fusion communication center.
85 lines (83 loc) • 3 kB
JavaScript
import eventTypes from './eventTypes'
import pointToPointCall from './pointToPointCall'
import monitor from './monitor'
import meeting from './meeting'
import phone from './phone'
import nats from './nats'
import IM from './IM'
// // 动态加载 api
// const modules = {}
// const requireContext = require.context('./', true, /^\.\/(.+)\/(.+)\/index\.js$/)
// requireContext.keys().forEach(key => {
// const mod = requireContext(key)
// const handle = mod.__esModule && mod.default ? mod.default : mod
// const match = key.match(/^\.\/(.+)\/(.+)\/index\.js$/)
// handle.type = match[1]
// modules[match[2]] = handle
// })
// 融合通讯内部开发者使用的中台对象与事件对象的对应关系映射
export const xwsToEvents = new Map()
export class XwEvent {
constructor (xw) {
this.xw = xw // 融合通讯开发者用的中台对象
this.eventTypes = eventTypes // 所有的事件类型集合
this.listens = {}
this.initListens()
}
initListens () {
for (let eventType in this.eventTypes) {
if (this.eventTypes.hasOwnProperty(eventType)) {
this.listens[eventType] = []
}
}
// 点对点视频呼叫的事件
pointToPointCall(this)
// 监控的事件
monitor(this)
// 监听会议事件
meeting(this)
// 电话事件
phone(this)
// nats事件
nats(this)
// im事件
IM(this)
}
emit (eventType, payload) {
if (eventType in this.eventTypes) {
if (this.eventTypes[eventType].module === 'system' || this.xw.public[this.eventTypes[eventType].module]) { // 判断当前中台实例是否开启了该事件
this.listens[eventType].forEach(callback => {
try {
callback(payload)
} catch (e) {
console.error(e)
}
})
}
} else {
throw new Error('融合通讯中台内部报错:要派发的中台事件未定义!')
}
}
listen (eventType, callback) {
if (eventType in this.eventTypes) {
if (this.eventTypes[eventType].module === 'system' || this.xw.public[this.eventTypes[eventType].module]) { // 判断当前中台实例是否开启了该事件
this.listens[eventType].push(callback)
} else {
throw new this.xw.BaseException(411, '没有监听该融合通讯中台事件,请联系管理员!')
}
} else {
throw new this.xw.BaseException(400, '你要监听的事件类型不存在,请参考融合通讯中台开发手册!')
}
}
unListen (eventType, callback) {
if (eventType in this.eventTypes) {
if (callback) {
this.listens[eventType] = this.listens[eventType].filter(item => item !== callback)
} else {
this.listens[eventType] = []
}
} else {
throw new this.xw.BaseException(400, '你取消监听的事件类型不存在,请参考融合通讯中台开发手册!')
}
}
}