Files
job-search/internal/events/events.go
T
bwilliams 6e101d2b4d Added an events bus, and pushed OpportunitiesUpdated to WebSocket clients when the fetcher stores new opportunities
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.
2026-10-16 11:59:26 +01:00

28 lines
1.1 KiB
Go

// Package events passes events between the parts of JobSearch that cause
// them, such as the fetcher, and the parts that react to them, such as the
// WebSocket hub, so neither depends on the other. It holds the event types
// and their payloads.
package events
// OpportunitiesUpdated is published when a save stores new opportunities or
// new releases of stored ones. Its payload is an OpportunitiesUpdatedPayload.
const OpportunitiesUpdated = "OpportunitiesUpdated"
// ---------- OpportunitiesUpdated ----------
// OpportunitiesUpdatedPayload lists the opportunities a save changed by UUID:
// New were stored for the first time, and Updated were already stored and
// gained at least one new release. Summary counts them.
type OpportunitiesUpdatedPayload struct {
New []string `json:"new"`
Updated []string `json:"updated"`
Summary OpportunitiesSummary `json:"summary"`
}
// OpportunitiesSummary counts the opportunities in an
// OpportunitiesUpdatedPayload.
type OpportunitiesSummary struct {
New int `json:"new"`
Updated int `json:"updated"`
}