Skip to content

Repository files navigation

hap

Go versionhap tests

hap, like in what's happening, is a generic event system aimed towards simplicity and performance.

Goals

  • Simple
  • Fast
  • Type-Safe
  • Reliable

Installation

go get -u github.com/mgjules/hap

Usage example

examples/simple.go:

package main
import (
"context""fmt""os""time""github.com/mgjules/hap""go.uber.org/zap"
)
funcmain() {
typeuserCreatedEventDatastruct {
// Embed to satisfy the necessary constraints.*hap.MetadataIDstringFirstNamestringLastNamestringCreatedAt time.Time
}
logger, err:=zap.NewDevelopment()
iferr!=nil {
fmt.Printf("Error creating logger: %v\n", err)
os.Exit(1)
}
sugaredLogger:=logger.Sugar()
// Create the event.userCreatedEvent:= hap.NewEvent[userCreatedEventData](sugaredLogger)
ctx:=context.Background()
received:=make(chanstruct{})
// Add handlers to the event.// If you don't specify an id, a random uuid will be generated.removeHandler:=userCreatedEvent.AddHandler(
ctx,
"",
func(data*userCreatedEventData) {
// Do something with the user created event data.sugaredLogger.Debugf("User created event: %#v", data)
received<-struct{}{}
},
)
deferremoveHandler()
// Trigger the event.userCreatedEvent.Trigger(ctx, userCreatedEventData{
Metadata: hap.MetadataFromContext(ctx),
ID: "4a2d6247-cf07-5eb8-b8fd-a27c37aaecfa",
FirstName: "John",
LastName: "Doe",
CreatedAt: time.Now(),
})
<-received
}

Note: You can also add buffered handlers to capture a series of events.

About

hap, like in what's happening, is a generic event system aimed towards simplicity and performance.

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages