Comment by aziis98

2 years ago

This just looks like the "Event Bus" [1] [2] [3] pattern, this is not the one from the React world if you were thinking about that. Hooks here are just the following, the "Hook" is just a collection of event handlers.

    type Handler[T any] func(e T) error

    type handlerPair[T any] struct {
        id      string
        handler Handler[T]
    }

    type Hook[T any] struct {
        mux      sync.RWMutex
        handlers []*handlerPair[T]
    }

It's very useful when you want to make an easily extensible library/framework/application, as you can see in pocketbase/core/app.go you can register handlers for various things that can happen.

[1]: https://en.wikipedia.org/wiki/Event-driven_programming

[2]: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_patt...

[3]: https://en.wikipedia.org/wiki/Observer_pattern