Documentation ¶
Overview ¶
Package notifications provides a notification system.
Notification Lifecycle ¶
1. Create Notification with an ID and Message. 2. Set possible actions and save it. 3. When the user responds, the action is executed.
Example
// create notification n := notifications.New("update-available", "A new update is available. Restart to upgrade.") // set actions and save n.AddAction("later", "Later").AddAction("restart", "Restart now!").Save() // wait for user action selectedAction := <-n.Response() switch selectedAction { case "later": log.Infof("user wants to upgrade later.") case "restart": log.Infof("user wants to restart now.") }
Index ¶
- Constants
- Variables
- func SetPersistenceBasePath(dbBasePath string)
- func UpdateNotification(n *Notification, key string)
- type Action
- type Notification
- func (n *Notification) AddDataSubject(ds sync.Locker)
- func (n *Notification) Delete() error
- func (n *Notification) Expired() <-chan struct{}
- func (n *Notification) Lock()
- func (n *Notification) Response() <-chan string
- func (n *Notification) Save() *Notification
- func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification
- func (n *Notification) Unlock()
- func (n *Notification) Update(expires int64)
- type StorageInterface
- func (s *StorageInterface) Delete(key string) error
- func (s *StorageInterface) Get(key string) (record.Record, error)
- func (s *StorageInterface) Put(r record.Record) (record.Record, error)
- func (s *StorageInterface) Query(q *query.Query, local, internal bool) (*iterator.Iterator, error)
- func (s *StorageInterface) ReadOnly() bool
Constants ¶
const ( Info uint8 = 0 Warning uint8 = 1 Prompt uint8 = 2 )
Notification types
Variables ¶
var ( ErrInvalidData = errors.New("invalid data, must be a notification object") ErrInvalidPath = errors.New("invalid path") ErrNoDelete = errors.New("notifications may not be deleted, they must be handled") )
Storage interface errors
Functions ¶
func SetPersistenceBasePath ¶
func SetPersistenceBasePath(dbBasePath string)
SetPersistenceBasePath sets the base path for persisting persistent notifications.
func UpdateNotification ¶ added in v0.3.0
func UpdateNotification(n *Notification, key string)
UpdateNotification updates a notification with input from a database action. Notification will not be saved/propagated if there is no valid change.
Types ¶
type Notification ¶
type Notification struct { record.Base ID string GUID string Message string // MessageTemplate string // MessageData []string DataSubject sync.Locker Type uint8 Persistent bool // this notification persists until it is handled and survives restarts Created int64 // creation timestamp, notification "starts" Expires int64 // expiry timestamp, notification is expected to be canceled at this time and may be cleaned up afterwards Responded int64 // response timestamp, notification "ends" Executed int64 // execution timestamp, notification will be deleted soon AvailableActions []*Action SelectedActionID string // contains filtered or unexported fields }
Notification represents a notification that is to be delivered to the user.
func EnsureNotification ¶
func EnsureNotification(r record.Record) (*Notification, error)
EnsureNotification ensures that the given record is a Notification and returns it.
func Get ¶
func Get(id string) *Notification
Get returns the notification identifed by the given id or nil if it doesn't exist.
func NotifyInfo ¶ added in v0.5.2
func NotifyInfo(id, msg string, actions ...Action) *Notification
NotifyInfo is a helper method for quickly showing a info notification. The notification is already shown. If id is an empty string a new UUIDv4 will be generated.
func NotifyPrompt ¶ added in v0.5.2
func NotifyPrompt(id, msg string, actions ...Action) *Notification
NotifyPrompt is a helper method for quickly showing a prompt notification. The notification is already shown. If id is an empty string a new UUIDv4 will be generated.
func NotifyWarn ¶ added in v0.5.2
func NotifyWarn(id, msg string, actions ...Action) *Notification
NotifyWarn is a helper method for quickly showing a warning notification. The notification is already shown. If id is an empty string a new UUIDv4 will be generated.
func (*Notification) AddDataSubject ¶
func (n *Notification) AddDataSubject(ds sync.Locker)
AddDataSubject adds the data subject to the notification. This is the only way how a data subject should be added - it avoids locking problems.
func (*Notification) Delete ¶ added in v0.3.0
func (n *Notification) Delete() error
Delete (prematurely) cancels and deletes a notification.
func (*Notification) Expired ¶ added in v0.3.0
func (n *Notification) Expired() <-chan struct{}
Expired notifies the caller when the notification has expired.
func (*Notification) Lock ¶
func (n *Notification) Lock()
Lock locks the Notification and the DataSubject, if available.
func (*Notification) Response ¶
func (n *Notification) Response() <-chan string
Response waits for the user to respond to the notification and returns the selected action.
func (*Notification) Save ¶
func (n *Notification) Save() *Notification
Save saves the notification and returns it.
func (*Notification) SetActionFunction ¶
func (n *Notification) SetActionFunction(fn func(*Notification)) *Notification
SetActionFunction sets a trigger function to be executed when the user reacted on the notification. The provided function will be started as its own goroutine and will have to lock everything it accesses, even the provided notification.
func (*Notification) Unlock ¶
func (n *Notification) Unlock()
Unlock unlocks the Notification and the DataSubject, if available.
func (*Notification) Update ¶ added in v0.3.0
func (n *Notification) Update(expires int64)
Update updates/resends a notification if it was not already responded to.
type StorageInterface ¶
type StorageInterface struct {
storage.InjectBase
}
StorageInterface provices a storage.Interface to the configuration manager.
func (*StorageInterface) Delete ¶
func (s *StorageInterface) Delete(key string) error
Delete deletes a record from the database.
func (*StorageInterface) Get ¶
func (s *StorageInterface) Get(key string) (record.Record, error)
Get returns a database record.
func (*StorageInterface) ReadOnly ¶
func (s *StorageInterface) ReadOnly() bool
ReadOnly returns whether the database is read only.