Documentation ¶
Overview ¶
Package livegollection implements a library for live synchronization between backend and frontend of a custom user-implemented collection. It's aimed for web applications since it works over websockets.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection[IdType any, ItemType Item[IdType]] interface { All() ([]ItemType, error) Item(ID IdType) (ItemType, error) Create(ItemType) (ItemType, error) Update(ItemType) error Delete(ID IdType) error }
Collection is the interface that the user-implemented collection (which is going to be synchronized) must satisfy. It wraps the following methods: All, Item, Create, Update, Delete. All returns all the items inside the collection (as a slice of items) or an error if something goes wrong. Item takes an id and returns the correspondant item or an error if something goes wrong. Create takes an item which will be added to the collection. It returns the newly added item AFTER its id field has been set or an error if something goes wrong. Update takes an item which will be updated inside the collection and returns an error if something goes wrong. Delete takes an id and deletes the correspondant item from the collection. It returns an error if something goes wrong. It takes two type parameters: IdType and ItemType. The first is the type of items' id, the second is the type of items that must satisfy Item[IdType] interface.
type Item ¶
type Item[IdType any] interface { ID() IdType }
Item is the interface that the items of the user-implemented collection (which is going to be synchronized) must satisfy. It wraps the ID method, which is supposed to return the item's id as an IdType where IdType is a type parameter.
type LiveGollection ¶
type LiveGollection[IdType any, ItemType Item[IdType]] struct { // contains filtered or unexported fields }
LiveGollection represents the instance of the livegollection server. It handles the pool of clients by sending and receiving live updates and calls the appropriate methods on the underlying user-implemented collection to keep it synchronized. It takes two type parameters: IdType and ItemType. The first is the type of items' id, the second is the type of items that must satisfy Item[IdType] interface.
func NewLiveGollection ¶
func NewLiveGollection[IdType any, ItemType Item[IdType]](ctx context.Context, coll Collection[IdType, ItemType], logger *log.Logger) *LiveGollection[IdType, ItemType]
NewLiveGollection returns a pointer to a new LiveGollection instance after having set it properly. It takes the following parameters: ctx, coll, logger. ctx is the Context object that can be used to terminate the LiveGollection and close the connections with all the clients in the pool. Pass context.TODO() if it's unclear which context to use. coll is the istance of the underlying user-implemented collection that will be synchronized. logger is where all the errors and messages will be reported. Pass nil if logging is not needed. It takes two type parameters: IdType and ItemType. The first is the type of items' id in coll, the second is the type of items in coll that must satisfy Item[IdType] interface.
func (*LiveGollection[IdType, ItemType]) Join ¶
func (lG *LiveGollection[IdType, ItemType]) Join(w http.ResponseWriter, r *http.Request)
Join is an http.HandlerFunc. The proper way to set the websocket server-side handler of livegollection is to add a route to this function with, for example, http.HandleFunc("route/to/livegollection", liveGoll.Join) .
func (*LiveGollection[IdType, ItemType]) SendMessageToAll ¶
func (lG *LiveGollection[IdType, ItemType]) SendMessageToAll(dataItem ItemType) *IdType
func (*LiveGollection[IdType, ItemType]) UpdateMessageToAll ¶
func (lG *LiveGollection[IdType, ItemType]) UpdateMessageToAll(dataItem ItemType) *IdType