Skip to content

Commit

Permalink
refactor: upgrade to v3 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Apr 15, 2023
1 parent 88cc8ba commit 836e446
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -11,15 +11,15 @@
## Installation

```bash
go get github.com/go-packagist/event/v2
go get github.com/go-packagist/event/v3
```

## Usage

```go
package main

import "github.com/go-packagist/event/v2"
import "github.com/go-packagist/event/v3"

type Event struct {
Stop bool
Expand Down
31 changes: 24 additions & 7 deletions dispatcher.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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)
}
34 changes: 2 additions & 32 deletions dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type testEvent struct {
}

var _ Event = (*testEvent)(nil)
var _ StoppableEvent = (*testEvent)(nil)

func newTestEvent(name string) Event {
return &testEvent{name: name}
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
9 changes: 2 additions & 7 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/go-packagist/event/v2
module github.com/go-packagist/event/v3

go 1.20

Expand Down

0 comments on commit 836e446

Please sign in to comment.