Skip to content

Commit

Permalink
Fix session behaviour and improve session API
Browse files Browse the repository at this point in the history
- Ensure AW_SESSION_ID is always sent with feedback
- Remove maxAge argument to Session calls
- Manually clear expired session data (so it happens within call to
  Workflow.Run()
  • Loading branch information
deanishe committed Oct 23, 2017
1 parent 3950e35 commit 896d4db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 4 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ type Session struct {
// NewSession creates and initialises a Session.
func NewSession(dir, sessionID string) *Session {
s := &Session{sessionID, NewCache(dir)}
s.Clear(false) // Clear old session data
return s
}

Expand Down Expand Up @@ -263,17 +262,17 @@ func (s *Session) LoadJSON(name string, v interface{}) error {
// data are cached & returned.
//
// If maxAge is 0, any cached data are always returned.
func (s *Session) LoadOrStore(name string, maxAge time.Duration, reload func() ([]byte, error)) ([]byte, error) {
return s.cache.LoadOrStore(s.name(name), maxAge, reload)
func (s *Session) LoadOrStore(name string, reload func() ([]byte, error)) ([]byte, error) {
return s.cache.LoadOrStore(s.name(name), 0, reload)
}

// LoadOrStoreJSON loads JSON-serialised data from cache if they exist and are
// newer than maxAge. If the data do not exist or are older than maxAge, reload
// is called, and the returned interface{} is cached and returned.
//
// If maxAge is 0, any cached data are always returned.
func (s *Session) LoadOrStoreJSON(name string, maxAge time.Duration, reload func() (interface{}, error), v interface{}) error {
return s.cache.LoadOrStoreJSON(s.name(name), maxAge, reload, v)
func (s *Session) LoadOrStoreJSON(name string, reload func() (interface{}, error), v interface{}) error {
return s.cache.LoadOrStoreJSON(s.name(name), 0, reload, v)
}

// Exists returns true if the named cache exists.
Expand Down
3 changes: 2 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ func TestSession(t *testing.T) {
t.Errorf("cached data do not exist: %s", n)
}

NewSession(dir, sid2)
s = NewSession(dir, sid2)
s.Clear(false)

if s.Exists(n) {
t.Errorf("expired data still exist: %s", n)
Expand Down
8 changes: 6 additions & 2 deletions workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// AwGoVersion is the semantic version number of this library.
const AwGoVersion = "0.9"
const AwGoVersion = "0.10"

var (
startTime time.Time // Time execution started
Expand Down Expand Up @@ -467,7 +467,6 @@ func (wf *Workflow) SessionID() string {
wf.sessionID = ev
} else {
wf.sessionID = NewSessionID()
wf.Var("AW_SESSION_ID", wf.sessionID)
}
}
return wf.sessionID
Expand Down Expand Up @@ -583,6 +582,9 @@ func (wf *Workflow) Run(fn func()) {
}
log.Println(util.Pad(vstr, "-", 50))

// Clear expired session data
wf.Session.Clear(false)

// Catch any `panic` and display an error in Alfred.
// Fatal(msg) will terminate the process (via log.Fatal).
defer func() {
Expand Down Expand Up @@ -647,6 +649,8 @@ func (wf *Workflow) WarnEmpty(title, subtitle string) {
// workflow complete; sending further responses will have no effect.
func SendFeedback() { wf.SendFeedback() }
func (wf *Workflow) SendFeedback() *Workflow {
// Set session ID
wf.Var("AW_SESSION_ID", wf.SessionID())
// Truncate Items if MaxResults is set
if wf.MaxResults > 0 && len(wf.Feedback.Items) > wf.MaxResults {
wf.Feedback.Items = wf.Feedback.Items[0:wf.MaxResults]
Expand Down

0 comments on commit 896d4db

Please sign in to comment.