4 ms·
It is the first time I see that "hook" pattern in a Go API backend: `hook.Hook` and `hook.TaggedHook`. Where does it come from? Why is it useful here? What a
by jadayesnaamsi 3y ago
It is the first time I see that "hook" pattern in a Go API backend: `hook.Hook` and `hook.TaggedHook`.
Where does it come from?
Why is it useful here?
What are the alternatives? Advantages/Drawbacks?
Is there an article somewhere, outside of the Pocketbase docs, presenting that pattern?
- https://github.com/pocketbase/pocketbase/blob/master/core/app.go https://github.com/pocketbase/pocketbase/blob/master/core/ap...
- https://github.com/pocketbase/pocketbase/tree/master/tools/hook https://github.com/pocketbase/pocketbase/tree/master/tools/h...
- aziis98 3y agoThis 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 https://en.wikipedia.org/wiki/Event-driven_programming [2]: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_patt... [3]: https://en.wikipedia.org/wiki/Observer_pattern https://en.wikipedia.org/wiki/Observer_pattern