MCPcopy Index your code
hub / github.com/btvoidx/mint

github.com/btvoidx/mint @v0.4.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.4.3 ↗ · + Follow
18 symbols 49 edges 3 files 7 documented · 39%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Mint 🍃

Tiny generic event emitter.

  • Very simple: mint has 3 exported functions and 1 type
  • Type safe: built on generics
  • Fast: does not use reflection
  • Independant: has no external dependencies

Get

go get github.com/btvoidx/mint

Use

// Create an emitter
e := new(mint.Emitter) // or &mint.Emmiter{}

// Create a consumer
func OnMyEvent(MyEvent) {
    // do stuff with the value
}

// Subscribe
off := mint.On(e, OnMyEvent)
defer off() // don't forget to unsubsribe later!

// ...

// In some other place
mint.Emit(e, MyEvent{Msg: "Hi", FromID: 1})
mint.Emit(e, MyEvent{Msg: "Hello indeed", FromID: 2})

By importing the package as "github.com/btvoidx/mint/context" you can access contextful api.

import "github.com/btvoidx/mint/context"

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

// Not all consumers may receive the message due to timeout,
// but Emit does wait for the active consumer to finish.
err := mint.Emit(e, ctx, MyEvent{Msg: "A message"})

// err is always ctx.Err()
if err != ctx.Err() {   /* unreachable code */ }

Both versions can operate on the same mint.Emitter instance, as contextless api just wraps the contextful one with context.Background().

// MyEvent consumers will receive both events.
mintctx.Emit(e, ctx, MyEvent{Msg: "A message"})
mint.Emit(e, MyEvent{Msg: "A message"}) // uses context.Background()

If you prefer channel-based consumers you can create a wrapper for mint.On so that it forwards all data to a channel.

func MyOn[T any](e *mint.Emitter) (<-ch T, off func()) {
    ch := make(chan T)
    off := mint.On(e, func(v T) {
        go func() { ch <- v }
    })

    return ch, func() {
        off()
        // drain channel so pending emits don't panic
        for {
            select {
            case <-ch:
            default:
                close(ch)
                return
            }
        }
    }
}

// Use it like so
ch, off := MyOn[MyEvent](e)
defer off()

for event := range ch {
    // deal with incoming data
}

For additional examples see mint_test.go.

Reporting issues

If you have questions or problems just open a new issue.

Contributing

This project uses lowercase commit messages. Pull requests containing commits starting with capital letters must be rebased to change commit messages.

Core symbols most depended-on inside this repo

Emit
called by 8
mint.go
On
called by 6
mint.go
Use
called by 1
mint.go
init
called by 1
context/mint.go
Emit
called by 0
context/mint.go
On
called by 0
context/mint.go
Use
called by 0
context/mint.go

Shape

Function 14
Struct 3
Method 1

Languages

Go100%

Modules by API surface

mint_test.go9 symbols
context/mint.go6 symbols
mint.go3 symbols

For agents

$ claude mcp add mint \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact