为什么我的Go事件每两个事件只处理一次?

iibxawm4  于 2022-12-25  发布在  Go
关注(0)|答案(1)|浏览(163)

我有下面的settings_manager.go

package main

type Settings struct {
    globalTrackingDisabled      bool
    textTracking                bool
    imageTracking               bool
    debug                       bool
    Event                       chan string
    EventTextTracking           string
    EventImageTracking          string
    EventGlobalTrackingDisabled string
    EventDebugToggled           string
}

type DefaultValues struct {
    GlobalTrackingDisabled bool
    TextTracking           bool
    ImageTracking          bool
    DebugEnabled           bool
}

func NewSettings(defaults DefaultValues) *Settings {
    return &Settings{
        globalTrackingDisabled:      defaults.GlobalTrackingDisabled,
        textTracking:                defaults.TextTracking,
        imageTracking:               defaults.ImageTracking,
        debug:                       defaults.DebugEnabled,
        Event:                       make(chan string, 1),
        EventTextTracking:           "textTracking",
        EventImageTracking:          "imageTracking",
        EventGlobalTrackingDisabled: "globalTrackingDisabled",
        EventDebugToggled:           "debugToggled",
    }
}

// Get the current value of textTracking
func (s *Settings) TextTrackingEnabled() bool {
    return s.textTracking
}

// Get the current value of imageTracking
func (s *Settings) ImageTrackingEnabled() bool {
    return s.imageTracking
}

// Get the current value of globalTrackingDisabled
func (s *Settings) GlobalTrackingDisabled() bool {
    return s.globalTrackingDisabled
}

// Get inverse of TextTrackingEnabled()
func (s *Settings) TextTrackingDisabled() bool {
    return !s.TextTrackingEnabled()
}

// Get inverse of ImageTrackingEnabled()
func (s *Settings) ImageTrackingDisabled() bool {
    return !s.ImageTrackingEnabled()
}

// Get the inverse of GlobalTrackingDisabled()
func (s *Settings) GlobalTrackingEnabled() bool {
    return !s.GlobalTrackingDisabled()
}

// Get the inverse of GlobalTrackingDisabled()
func (s *Settings) Debug() bool {
    return s.debug
}

// SetGlobalTracking sets the globalTrackingDisabled property and fires an event
func (s *Settings) SetGlobalTracking(value bool) {
    s.textTracking = value
    s.Event <- s.EventGlobalTrackingDisabled
}

// SetTextTracking sets the textTracking property and fires an event
func (s *Settings) SetTextTracking(value bool) {
    Debug("setter to ", value, s.EventTextTracking)
    s.textTracking = value
    s.Event <- s.EventTextTracking
    Debug("Sent event %s\n", s.EventTextTracking)
}

// SetImageTracking sets the imageTracking property and fires an event
func (s *Settings) SetImageTracking(value bool) {
    s.imageTracking = value
    s.Event <- s.EventImageTracking
}

// SetDebug sets the debug property and fires an event
func (s *Settings) SetDebug(value bool) {
    s.debug = value
    s.Event <- s.EventDebugToggled
}

// DebugDisable sets the debug property to false and fires an event
func (s *Settings) DebugDisable() {
    s.debug = false
    s.Event <- s.EventDebugToggled
}

// DebugEnable sets the debug property to true and fires an event
func (s *Settings) DebugEnable() {
    s.debug = true
    s.Event <- s.EventDebugToggled
}

我有一个系统托盘菜单项,我想切换文本跟踪设置时,单击。

// Click action

    go func() {
        for {
            select {
            case <-captureTextMenuItem.ClickedCh:
                Debug("Menu item clicked. New Value should be ", !captureTextMenuItem.Checked())
                if captureTextMenuItem.Checked() {
                    settings.SetTextTracking(false)
                } else {
                    settings.SetTextTracking(true)
                }
                //settings.SetTextTracking(!settings.TextTrackingEnabled())
                //Debug(settings.TextTrackingEnabled())
            }
        }
    }()

以下是我如何处理该事件:

// event handler

    go func() {
        for {
            select {
            case event := <-settings.Event:
                Debug("Received Event", event)
                switch event {
                case settings.EventTextTracking:
                    Debug("Ready to do something with the text event")
                    if settings.TextTrackingEnabled() {
                        Debug("Enabling checkmark")
                        captureTextMenuItem.Check()
                    } else {
                        Debug("Removing checkmark")
                        captureTextMenuItem.Uncheck()
                    }
                case settings.EventGlobalTrackingDisabled:
                    Debug("Ready to do something with the global tracker event")
                    if settings.GlobalTrackingEnabled() {
                        Debug("enabling global tracker")
                        captureTextMenuItem.Enable()
                    } else {
                        Debug("disabling global tracker")
                        captureTextMenuItem.Disabled()
                    }
                default:
                    fmt.Println("received unknown event:", event)
                }
            }
        }
    }()

我遇到的问题是,每次单击菜单项时,我都可以看到SetTextTracking被触发,但是,我的事件处理程序每触发两次事件才调用一次。

mnemlml8

mnemlml81#

因为你使用缓冲通道。你发送一个有效载荷到通道,什么也没发生。当你发送另一个有效载荷时,缓冲区将被刷新,接收端将开始运行。
尝试使用无缓冲通道。

相关问题