github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/internal/bus/bus.go (about) 1 /* 2 Package bus provides access to a singleton instance of an event bus (provided by the calling application). The event bus 3 is intended to allow for the syft library to publish events which library consumers can subscribe to. These events 4 can provide static information, but also have an object as a payload for which the consumer can poll for updates. 5 This is akin to a logger, except instead of only allowing strings to be logged, rich objects that can be interacted with. 6 7 Note that the singleton instance is only allowed to publish events and not subscribe to them --this is intentional. 8 Internal library interactions should continue to use traditional in-execution-path approaches for data sharing 9 (e.g. function returns and channels) and not depend on bus subscriptions for critical interactions (e.g. one part of the 10 lib publishes an event and another part of the lib subscribes and reacts to that event). The bus is provided only as a 11 means for consumers to observe events emitted from the library (such as to provide a rich UI) and not to allow 12 consumers to augment or otherwise change execution. 13 */ 14 package bus 15 16 import "github.com/wagoodman/go-partybus" 17 18 var publisher partybus.Publisher 19 20 // Set sets the singleton event bus publisher. This is optional; if no bus is provided, the library will 21 // behave no differently than if a bus had been provided. 22 func Set(p partybus.Publisher) { 23 publisher = p 24 } 25 26 func Get() partybus.Publisher { 27 return publisher 28 } 29 30 // Publish an event onto the bus. If there is no bus set by the calling application, this does nothing. 31 func Publish(e partybus.Event) { 32 if publisher != nil { 33 publisher.Publish(e) 34 } 35 }