summaryrefslogtreecommitdiff
path: root/reddit_background
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-14 22:52:21 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-14 22:52:21 +0200
commitb8f7e622bbee068ac9e6a96c2307dbc7d7b02b14 (patch)
treec945307775d90c51301d060878ff816aae4f017d /reddit_background
parent5b456afb49bfa6ff1567510bc1d9362377d32216 (diff)
downloadstartpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.tar.gz
startpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.tar.bz2
startpage-b8f7e622bbee068ac9e6a96c2307dbc7d7b02b14.zip
Lazy load background image using XHR
We now don't immediately reload the reddit background image, as this delays the initial page load quite a bit. Instead we trigger an XHR request on page load to update the background image.
Diffstat (limited to 'reddit_background')
-rw-r--r--reddit_background/reddit_background.go38
1 files changed, 31 insertions, 7 deletions
diff --git a/reddit_background/reddit_background.go b/reddit_background/reddit_background.go
index 67b25d0..60ba4ec 100644
--- a/reddit_background/reddit_background.go
+++ b/reddit_background/reddit_background.go
@@ -41,6 +41,23 @@ type RedditImage struct {
Mediatype string `json:"-"`
}
+type RedditImageForAjax struct {
+ Permalink string
+ Title string
+ Saved bool
+}
+
+func (ri *RedditImage) ForAjax() *RedditImageForAjax {
+ if ri == nil {
+ return nil
+ }
+ return &RedditImageForAjax{
+ Permalink: ri.Permalink,
+ Title: ri.Title,
+ Saved: ri.Saved,
+ }
+}
+
func GetRedditImage(maxsize int, subreddit string) (*RedditImage, error) {
subredditUrl := fmt.Sprintf("https://www.reddit.com/r/%s.json", subreddit)
@@ -211,21 +228,28 @@ func NewRedditImageProvider(maxsize int, subreddit string) *RedditImageProvider
}
}
-func (rip *RedditImageProvider) Image() *RedditImage {
+func (rip *RedditImageProvider) UpdateImage() bool {
+ updated := false
+
rip.intervalRunner.Run(func() bool {
log.Printf("Getting new RedditImage")
- var err error
- rip.image, err = GetRedditImage(rip.maxsize, rip.subreddit)
+ image, err := GetRedditImage(rip.maxsize, rip.subreddit)
- if err == nil {
- log.Printf("Successfully loaded RedditImage")
- } else {
+ if err != nil {
log.Printf("Failed loading RedditImage: %s", err)
+ return false
}
- return err == nil
+ log.Printf("Successfully loaded RedditImage")
+ rip.image = image
+ updated = true
+ return true
})
+ return updated
+}
+
+func (rip *RedditImageProvider) Image() *RedditImage {
return rip.image
}