Git Product home page Git Product logo

hap's Introduction

hap

Go version hap 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"
)

func main() {
	type userCreatedEventData struct {
		// Embed to satisfy the necessary constraints.
		*hap.Metadata

		ID        string
		FirstName string
		LastName  string
		CreatedAt time.Time
	}

	logger, err := zap.NewDevelopment()
	if err != 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(chan struct{})

	// 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{}{}
		},
	)
	defer removeHandler()

	// 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.

hap's People

Contributors

mgjules avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.