From 836e446b5da76e37753b8a1c64efb3af411a1e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Sat, 15 Apr 2023 12:52:15 +0800 Subject: [PATCH] refactor: upgrade to v3 (#8) --- README.md | 6 +++--- dispatcher.go | 31 ++++++++++++++++++++++++------- dispatcher_test.go | 34 ++-------------------------------- event.go | 9 ++------- example/main.go | 4 +++- go.mod | 2 +- 6 files changed, 35 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index bd38e1c..2ae5d53 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Go](https://badgen.net/badge/Go/%3E=1.16/orange) [![Go Version](https://badgen.net/github/release/go-packagist/event/stable)](https://github.com/go-packagist/event/releases) -[![GoDoc](https://pkg.go.dev/badge/github.com/go-packagist/event/v2)](https://pkg.go.dev/github.com/go-packagist/event/v2) +[![GoDoc](https://pkg.go.dev/badge/github.com/go-packagist/event/v3)](https://pkg.go.dev/github.com/go-packagist/event/v3) [![codecov](https://codecov.io/gh/go-packagist/event/branch/master/graph/badge.svg?token=5TWGQ9DIRU)](https://codecov.io/gh/go-packagist/event) [![Go Report Card](https://goreportcard.com/badge/github.com/go-packagist/event)](https://goreportcard.com/report/github.com/go-packagist/event) [![tests](https://github.com/go-packagist/event/actions/workflows/go.yml/badge.svg)](https://github.com/go-packagist/event/actions/workflows/go.yml) @@ -11,7 +11,7 @@ ## Installation ```bash -go get github.com/go-packagist/event/v2 +go get github.com/go-packagist/event/v3 ``` ## Usage @@ -19,7 +19,7 @@ go get github.com/go-packagist/event/v2 ```go package main -import "github.com/go-packagist/event/v2" +import "github.com/go-packagist/event/v3" type Event struct { Stop bool diff --git a/dispatcher.go b/dispatcher.go index 5bae1ba..8d18c5c 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -1,26 +1,37 @@ package event +import "sync" + // Dispatcher event dispatcher type Dispatcher struct { - Listeners map[string][]Listener + listeners map[string][]Listener + + mu sync.Mutex } // NewDispatcher create new dispatcher func NewDispatcher() *Dispatcher { return &Dispatcher{ - Listeners: make(map[string][]Listener, 0), + listeners: make(map[string][]Listener, 0), } } // Listen add listener to event func (d *Dispatcher) Listen(name string, listener ...Listener) { - d.Listeners[name] = append(d.Listeners[name], listener...) + if _, ok := d.listeners[name]; !ok { + d.mu.Lock() + defer d.mu.Unlock() + + d.listeners[name] = make([]Listener, 0, len(listener)) + } + + d.listeners[name] = append(d.listeners[name], listener...) } // Dispatch event to all listeners func (d *Dispatcher) Dispatch(event Event) { for _, listener := range d.GetListeners(event.Name()) { - if event.IsStop() { + if event.(StoppableEvent) != nil && event.(StoppableEvent).IsPropagationStopped() { return } @@ -30,15 +41,21 @@ func (d *Dispatcher) Dispatch(event Event) { // GetListeners return all listeners of event func (d *Dispatcher) GetListeners(name string) []Listener { - return d.Listeners[name] + if listeners, ok := d.listeners[name]; ok { + return listeners + } + + return []Listener{} } // Flush remove listeners of event func (d *Dispatcher) Flush(name string) { - delete(d.Listeners, name) + if _, ok := d.listeners[name]; ok { + delete(d.listeners, name) + } } // FlushAll remove all listeners func (d *Dispatcher) FlushAll() { - d.Listeners = make(map[string][]Listener, 0) + d.listeners = make(map[string][]Listener, 0) } diff --git a/dispatcher_test.go b/dispatcher_test.go index eed728e..29521ef 100644 --- a/dispatcher_test.go +++ b/dispatcher_test.go @@ -12,6 +12,7 @@ type testEvent struct { } var _ Event = (*testEvent)(nil) +var _ StoppableEvent = (*testEvent)(nil) func newTestEvent(name string) Event { return &testEvent{name: name} @@ -21,7 +22,7 @@ func (t *testEvent) Name() string { return t.name } -func (t *testEvent) IsStop() bool { +func (t *testEvent) IsPropagationStopped() bool { return t.Stoped } @@ -118,34 +119,3 @@ func TestFlush(t *testing.T) { assert.Equal(t, 0, len(d.GetListeners("test1"))) assert.Equal(t, 0, len(d.GetListeners("test2"))) } - -type testEventable struct { - *Eventable -} - -func (t *testEventable) Name() string { - return "test" -} - -func TestEventable(t *testing.T) { - d := NewDispatcher() - - // u1 - u1, listener1, listener2 := &testEventable{}, &Test1Listener{}, &Test2Listener{} - assert.False(t, u1.IsStop()) - - d.Listen("test", listener1) - d.Listen("test", listener2) - d.Dispatch(u1) - - assert.Equal(t, "Test1 Done", listener1.Val) - assert.Equal(t, "Test2 Done", listener2.Val) - - // u2 - u2, listener3 := &testEventable{}, &Test1Listener{} - - d.Listen("test", listener3) - d.Dispatch(u2) - - assert.Equal(t, "Test1 Done", listener3.Val) -} diff --git a/event.go b/event.go index 3f1cb46..652d8ef 100644 --- a/event.go +++ b/event.go @@ -3,13 +3,8 @@ package event // Event interface(see psr-14:https://github.com/php-fig/event-dispatcher) type Event interface { Name() string - IsStop() bool } -// Eventable abstracts the event -type Eventable struct{} - -// IsStop is a method of Eventable -func (e *Eventable) IsStop() bool { - return false +type StoppableEvent interface { + IsPropagationStopped() bool } diff --git a/example/main.go b/example/main.go index 3661e8d..2d59cae 100644 --- a/example/main.go +++ b/example/main.go @@ -1,6 +1,8 @@ package main -import "github.com/go-packagist/event/v2" +import ( + "github.com/go-packagist/event/v3" +) type Event struct { Stop bool diff --git a/go.mod b/go.mod index 001efc9..a13cefb 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/go-packagist/event/v2 +module github.com/go-packagist/event/v3 go 1.20