energy/cef/cef-ipc-browser-channel.go

50 lines
1.3 KiB
Go
Raw Normal View History

2023-03-23 11:46:34 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// GO browser IPC通道
package cef
2023-03-23 16:03:20 +08:00
import (
"github.com/energye/energy/pkgs/channel"
2023-03-23 17:33:33 +08:00
"github.com/energye/energy/pkgs/json"
2023-03-23 16:03:20 +08:00
)
2023-03-23 11:46:34 +08:00
2023-03-24 14:31:40 +08:00
// 主进程IPC
var browserIPC *browserIPCChan
2023-03-23 11:46:34 +08:00
// browserIPCChan
type browserIPCChan struct {
2023-03-24 14:31:40 +08:00
ipc channel.IBrowserChannel //channel
callback []func(channelId int64, data json.JSON) bool //实现回调函数
2023-03-23 11:46:34 +08:00
}
2023-03-24 14:31:40 +08:00
// browserIPCCreate 主进程IPC创建
func browserIPCCreate() {
if browserIPC == nil {
browserIPC = new(browserIPCChan)
browserIPC.ipc = channel.NewBrowser()
browserIPC.ipc.Handler(func(context channel.IIPCContext) {
data := context.Message().JSON()
//callback 返回 true ipc 停止遍历,否则继续遍历,直到最后一个
for _, call := range browserIPC.callback {
if call(context.ChannelId(), data) {
break
}
2023-03-23 17:33:33 +08:00
}
2023-03-23 11:46:34 +08:00
context.Free()
})
}
}
2023-03-23 17:33:33 +08:00
2023-03-24 14:31:40 +08:00
// addCallback 添加回调函数, callback 返回 true ipc 停止遍历,否则继续遍历,直到最后一个
func (m *browserIPCChan) addCallback(callback func(channelId int64, data json.JSON) bool) {
m.callback = append(m.callback, callback)
2023-03-23 17:33:33 +08:00
}