func (engine *Engine) AddMessageListener(callback MessageCallback) int64 { listener := &MessageListener{ Id: engine.messageListenerId, Callback: callback, } engine.messageListenerId += 1 needAppend := true for i, listener := range engine.messageListeners { if listener == nil { engine.messageListeners[i] = listener needAppend = false break } } if needAppend { engine.messageListeners = append(engine.messageListeners, listener) } engine.messageListenerNum += 1 if engine.messageListenerNum == 1 { C.V8_EnableMessageListener(engine.self, unsafe.Pointer(engine), 1) } return listener.Id }
func (engine *Engine) RemoveMessageListener(id int64) { for i, listener := range engine.messageListeners { if listener != nil && listener.Id == id { engine.messageListeners[i] = nil engine.messageListenerNum -= 1 break } } if engine.messageListenerNum == 0 { C.V8_EnableMessageListener(engine.self, unsafe.Pointer(engine), 0) } }
func (engine *Engine) AddMessageListener(callback MessageCallback) int64 { listener := &messageListener{ Id: engine.messageListenerId, Callback: callback, } if engine.lastMessageListener == nil { engine.firstMessageListener = listener engine.lastMessageListener = listener C.V8_EnableMessageListener(engine.self, unsafe.Pointer(engine), 1) } else { engine.lastMessageListener.Next = listener engine.lastMessageListener = listener } return listener.Id }
func (engine *Engine) RemoveMessageListener(id int64) { var p *messageListener for i := engine.firstMessageListener; i != nil; p, i = i, i.Next { if i.Id == id { if p == nil { engine.firstMessageListener = i.Next } else { p.Next = i.Next } if i == engine.lastMessageListener { engine.lastMessageListener = p } break } } if engine.firstMessageListener == nil { C.V8_EnableMessageListener(engine.self, unsafe.Pointer(engine), 0) } }