Skip to content

Commit

Permalink
refactor: made private methods that will not be used outside
Browse files Browse the repository at this point in the history
  • Loading branch information
stanipetrosyan committed Aug 21, 2024
1 parent 9d6d9be commit 0f8b180
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 18 deletions.
10 changes: 5 additions & 5 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *defaultChannel) Listen() {
func (c *defaultChannel) Publisher() Publisher {
slog.Info("Publisher created", slog.String("channel", c.address))

return NewPublisher(c.ch)
return newPublisher(c.ch)
}

func (c *defaultChannel) Subscriber() Subscriber {
Expand All @@ -44,19 +44,19 @@ func (c *defaultChannel) Subscriber() Subscriber {

slog.Info("Subscriber created", slog.String("channel", c.address))

return NewSubscriber(ch)
return newSubscriber(ch)
}

func (c *defaultChannel) Processor(predicate func(message Message) bool) Channel {
c.processor = NewProcessorWithPredicate(predicate)
c.processor = newProcessorWithPredicate(predicate)

slog.Info("Processor created", slog.String("channel", c.address))
return c
}

func NewChannel(address string) Channel {
func newChannel(address string) Channel {
ch := make(chan Message)
channel := defaultChannel{address: address, ch: ch, chs: []chan Message{}, processor: NewProcessor()}
channel := defaultChannel{address: address, ch: ch, chs: []chan Message{}, processor: newProcessor()}
go channel.Listen()

return &channel
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func (s *tcpClient) Connect() {
}
}

func NewClient(address string, eventbus EventBus) Client {
func newClient(address string, eventbus EventBus) Client {
return &tcpClient{address: address, eventbus: eventbus}
}
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestClient(t *testing.T) {
}
}()

client := NewClient("localhost:8083", eventbus)
client := newClient("localhost:8083", eventbus)
go client.Connect()

wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func (c defaultContext) Result() Message {
return c.message
}

func NewConsumerContextWithMessage(message Message) Context {
func newConsumerContextWithMessage(message Message) Context {
return defaultContext{message: message}
}
4 changes: 3 additions & 1 deletion eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type defaultEventBus struct {
channels map[string]Channel
}

// Returns a Channel with that address. If it doesn't exist, it returns a new one
func (e *defaultEventBus) Channel(address string) Channel {
_, exists := e.channels[address]
if !exists {
e.channels[address] = NewChannel(address)
e.channels[address] = newChannel(address)
}

slog.Info("Channel created", slog.String("name", address))
return e.channels[address]
}

// Create a new eventbus with default parameters
func NewEventBus() EventBus {
return &defaultEventBus{
channels: map[string]Channel{},
Expand Down
4 changes: 2 additions & 2 deletions network_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func NewNetworkBus(bus EventBus, address string) NetworkBus {
}

func (b defaultNetworkBus) Server() Server {
return NewServer(b.address)
return newServer(b.address)
}

func (b defaultNetworkBus) Client() Client {
return NewClient(b.address, b.localBus)
return newClient(b.address, b.localBus)
}
4 changes: 2 additions & 2 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ type defaultProcessor struct {
predicate func(message Message) bool
}

func NewProcessor() Processor {
func newProcessor() Processor {
return defaultProcessor{func(message Message) bool { return true }}
}

func NewProcessorWithPredicate(predicate func(message Message) bool) Processor {
func newProcessorWithPredicate(predicate func(message Message) bool) Processor {
return defaultProcessor{predicate: predicate}
}

Expand Down
2 changes: 1 addition & 1 deletion publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type defaultPublisher struct {
ch chan Message
}

func NewPublisher(ch chan Message) Publisher {
func newPublisher(ch chan Message) Publisher {
return defaultPublisher{ch: ch}
}

Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func (s *tcpServer) Publish(channel string, message Message) {
s.Unlock()
}

func NewServer(address string) Server {
func newServer(address string) Server {
return &tcpServer{address: address, clients: []net.Conn{}}
}
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestServer(t *testing.T) {
var wg sync.WaitGroup

wg.Add(1)
server := NewServer("localhost:8082")
server := newServer("localhost:8082")
go server.Listen()

var conn net.Conn
Expand Down
4 changes: 2 additions & 2 deletions subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (s defaultSubscriber) Listen(consumer func(context Context)) {
return
}

consumer(NewConsumerContextWithMessage(data))
consumer(newConsumerContextWithMessage(data))
}
}()
}

func NewSubscriber(ch chan Message) Subscriber {
func newSubscriber(ch chan Message) Subscriber {
return defaultSubscriber{ch: ch}
}

0 comments on commit 0f8b180

Please sign in to comment.