internal/events decouples the parts of JobSearch that cause events from
the parts that react to them. bus.go has Bus: Subscribe, and a synchronous
Publish that calls each handler in turn on the publisher's goroutine, so
handlers mustn't block (the hub doesn't: it queues per client). events.go
has the event types and their payloads, starting with OpportunitiesUpdated.
With fetcher.WithEvents, a save that stores new opportunities, or new
releases of stored ones, publishes OpportunitiesUpdated with their UUIDs and
counts. An opportunity whose row is only rewritten, as when it is fetched
again from the newest stored release, isn't counted as updated, and nothing
is published when nothing changed. store.SaveResult now reports
NewOpportunity for each opportunity saved.
job-search serve subscribes its WebSocket hub to the bus, so every client
gets {"type":"event","eventType":"OpportunitiesUpdated","payload":{"new":
[...],"updated":[...],"summary":{"new":n,"updated":n}}}. The Envelope gains
an eventType field, and Hub.PushEvent and Server.PushEvent send events.
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package events
|
|
|
|
import "sync"
|
|
|
|
// Bus passes each published Event to every subscriber. Its zero value is
|
|
// ready to use, and it is safe for concurrent use.
|
|
type Bus struct {
|
|
mu sync.RWMutex
|
|
handlers []func(Event)
|
|
}
|
|
|
|
// Event is something that happened, for subscribers to react to. Type names
|
|
// it (e.g. OpportunitiesUpdated) and says what Payload is.
|
|
type Event struct {
|
|
Type string
|
|
Payload any
|
|
}
|
|
|
|
// Subscribe makes handler receive every event published from now on.
|
|
// Subscribe before anything publishes, so no event is missed.
|
|
func (b *Bus) Subscribe(handler func(Event)) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.handlers = append(b.handlers, handler)
|
|
}
|
|
|
|
// Publish passes event to every subscriber in turn, in the order they
|
|
// subscribed, and returns once they all have. Handlers are called on the
|
|
// publisher's goroutine, so they mustn't block; one with slow work to do
|
|
// should queue it. Publish may be called from several goroutines at once.
|
|
func (b *Bus) Publish(event Event) {
|
|
b.mu.RLock()
|
|
handlers := b.handlers
|
|
b.mu.RUnlock()
|
|
|
|
// Called outside the lock, so a handler may itself subscribe or publish.
|
|
for _, handler := range handlers {
|
|
handler(event)
|
|
}
|
|
}
|