Permalink
Browse files

Fix recently introduced events bug

  • Loading branch information...
1 parent 1f7a272 commit 8cfac4e9697f74dcf093e090eb53d2e418eba9e2 @lxn committed Feb 24, 2012
Showing with 100 additions and 19 deletions.
  1. +11 −2 cancelevent.go
  2. +11 −2 closeevent.go
  3. +11 −2 errorevent.go
  4. +11 −2 event.go
  5. +11 −2 intevent.go
  6. +11 −2 keyevent.go
  7. +11 −2 mouseevent.go
  8. +1 −1 tabpage.go
  9. +11 −2 treeviewitemevent.go
  10. +11 −2 treeviewitemselectionevent.go
View
@@ -11,12 +11,19 @@ type CancelEvent struct {
}
func (e *CancelEvent) Attach(handler CancelEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *CancelEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type CancelEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *CancelEventPublisher) Event() *CancelEvent {
func (p *CancelEventPublisher) Publish(canceled *bool) {
for _, handler := range p.event.handlers {
- handler(canceled)
+ if handler != nil {
+ handler(canceled)
+ }
}
}
View
@@ -11,12 +11,19 @@ type CloseEvent struct {
}
func (e *CloseEvent) Attach(handler CloseEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *CloseEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type CloseEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *CloseEventPublisher) Event() *CloseEvent {
func (p *CloseEventPublisher) Publish(canceled *bool, reason CloseReason) {
for _, handler := range p.event.handlers {
- handler(canceled, reason)
+ if handler != nil {
+ handler(canceled, reason)
+ }
}
}
View
@@ -11,12 +11,19 @@ type ErrorEvent struct {
}
func (e *ErrorEvent) Attach(handler ErrorEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *ErrorEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type ErrorEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *ErrorEventPublisher) Event() *ErrorEvent {
func (p *ErrorEventPublisher) Publish(err error) {
for _, handler := range p.event.handlers {
- handler(err)
+ if handler != nil {
+ handler(err)
+ }
}
}
View
@@ -11,12 +11,19 @@ type Event struct {
}
func (e *Event) Attach(handler EventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *Event) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type EventPublisher struct {
@@ -29,6 +36,8 @@ func (p *EventPublisher) Event() *Event {
func (p *EventPublisher) Publish() {
for _, handler := range p.event.handlers {
- handler()
+ if handler != nil {
+ handler()
+ }
}
}
View
@@ -11,12 +11,19 @@ type IntEvent struct {
}
func (e *IntEvent) Attach(handler IntEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *IntEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type IntEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *IntEventPublisher) Event() *IntEvent {
func (p *IntEventPublisher) Publish(n int) {
for _, handler := range p.event.handlers {
- handler(n)
+ if handler != nil {
+ handler(n)
+ }
}
}
View
@@ -11,12 +11,19 @@ type KeyEvent struct {
}
func (e *KeyEvent) Attach(handler KeyEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *KeyEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type KeyEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *KeyEventPublisher) Event() *KeyEvent {
func (p *KeyEventPublisher) Publish(key int) {
for _, handler := range p.event.handlers {
- handler(key)
+ if handler != nil {
+ handler(key)
+ }
}
}
View
@@ -19,12 +19,19 @@ type MouseEvent struct {
}
func (e *MouseEvent) Attach(handler MouseEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *MouseEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type MouseEventPublisher struct {
@@ -37,6 +44,8 @@ func (p *MouseEventPublisher) Event() *MouseEvent {
func (p *MouseEventPublisher) Publish(x, y int, button MouseButton) {
for _, handler := range p.event.handlers {
- handler(x, y, button)
+ if handler != nil {
+ handler(x, y, button)
+ }
}
}
View
@@ -28,7 +28,7 @@ func NewTabPage() (*TabPage, error) {
nil,
tabPageWindowClass,
WS_POPUP,
- WS_EX_CONTROLPARENT /*|WS_EX_TRANSPARENT*/ ); err != nil {
+ WS_EX_CONTROLPARENT /*|WS_EX_TRANSPARENT*/); err != nil {
return nil, err
}
View
@@ -11,12 +11,19 @@ type TreeViewItemEvent struct {
}
func (e *TreeViewItemEvent) Attach(handler TreeViewItemEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *TreeViewItemEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type TreeViewItemEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *TreeViewItemEventPublisher) Event() *TreeViewItemEvent {
func (p *TreeViewItemEventPublisher) Publish(item *TreeViewItem) {
for _, handler := range p.event.handlers {
- handler(item)
+ if handler != nil {
+ handler(item)
+ }
}
}
@@ -11,12 +11,19 @@ type TreeViewItemSelectionEvent struct {
}
func (e *TreeViewItemSelectionEvent) Attach(handler TreeViewItemSelectionEventHandler) int {
+ for i, h := range e.handlers {
+ if h == nil {
+ e.handlers[i] = handler
+ return i
+ }
+ }
+
e.handlers = append(e.handlers, handler)
return len(e.handlers) - 1
}
func (e *TreeViewItemSelectionEvent) Detach(handle int) {
- e.handlers = append(e.handlers[:handle], e.handlers[handle+1:]...)
+ e.handlers[handle] = nil
}
type TreeViewItemSelectionEventPublisher struct {
@@ -29,6 +36,8 @@ func (p *TreeViewItemSelectionEventPublisher) Event() *TreeViewItemSelectionEven
func (p *TreeViewItemSelectionEventPublisher) Publish(old, new *TreeViewItem) {
for _, handler := range p.event.handlers {
- handler(old, new)
+ if handler != nil {
+ handler(old, new)
+ }
}
}

0 comments on commit 8cfac4e

Please sign in to comment.