Skip to content
This repository has been archived by the owner on Dec 27, 2020. It is now read-only.

short-d/eventbus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventBus

Minimal Event Bus written in Go

Example

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/short-d/eventbus"
)

func main() {
	bus := eventbus.NewEventBus()

	notificationChannel := make(eventbus.DataChannel)
	notification := "notification"
	bus.Subscribe(notification, notificationChannel)

	var waitGroup sync.WaitGroup
	waitGroup.Add(1)

	go func() {
		for {
			select {
			case data := <-notificationChannel:
				fmt.Println(data)
				bus.UnSubscribe(notification, notificationChannel)
				waitGroup.Done()
			}
		}
	}()

	go func() {
		time.Sleep(2 * time.Second)
		bus.Publish(notification, "Hello!")
	}()
	waitGroup.Wait()
}

Author

Harry Liu - byliuyang

License

This project is maintained under MIT license

About

Minimal EventBus in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published