-
Notifications
You must be signed in to change notification settings - Fork 1
Add module for choosing/caching featured apps for display #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| define('featured', ['settings', 'storage', 'log'], function(settings, storage, log) { | ||
|
|
||
| var console = log('featured'); | ||
|
|
||
| return { | ||
|
|
||
| all: function() { | ||
| // This will eventually fetch from the JSON database. For now... | ||
| var cached = JSON.parse(storage.getItem('all_featured')); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!cached) { | ||
| cached = [ | ||
| { name: 'trenta', weight: 10 }, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you have to google this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. 31 oz coffees are how I'm able to tolerate you. |
||
| { name: 'venti', weight: 8 }, | ||
| { name: 'grande', weight: 6 }, | ||
| { name: 'tall', weight: 4 }, | ||
| { name: 'nessuno', weight: 2 } | ||
| ]; | ||
| storage.setItem('all_featured', JSON.stringify(cached)); | ||
| } | ||
| return cached; | ||
| }, | ||
|
|
||
| get: function() { | ||
| // Retrieve a subset of the featured apps from cache. If the cache has | ||
| // expired, regenerate a random subset and return the featured apps. | ||
| var cached = JSON.parse(storage.getItem(this.cacheKey())) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semicolon
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!cached) { | ||
| cached = this.regenerate(); | ||
| this.removeYesterday(); | ||
| } | ||
| return cached; | ||
| }, | ||
|
|
||
| cacheKey: function(date) { | ||
| // Generate a date-based cache key for the passed Date object (default: today). | ||
| if(typeof date === 'undefined') { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
| date = new Date(); | ||
| }; | ||
| return 'featured_' + date.toDateString().replace(' ', '_').toLowerCase(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this format 👍 |
||
| }, | ||
|
|
||
| regenerate: function() { | ||
| // From the pool of featured apps, returned a randomized, weighted selection to be | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spelling error: "randomised"
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not British. |
||
| // displayed on the homepage. Store | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Store" whaaaa? |
||
| console.log('Generating a new selection of featured apps.'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no period. period. |
||
| var all = this.all(); | ||
|
|
||
| // Create a weighted array of all the available items' indexes in `all`. | ||
| var weighted_index = []; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. camelCase in this #$%^&! |
||
| for (var i = 0; i < all.length; i++) { | ||
| for (var n = 0; n < Math.ceil(all[i].weight); n++) { | ||
| weighted_index.push(i); | ||
| } | ||
| } | ||
|
|
||
| // Choose the appropriate number of random unique indexes from the weighted array. | ||
| var chosen_items = []; | ||
| while (chosen_items.length < settings.numberFeatured) { | ||
| var random = weighted_index[Math.floor(Math.random() * weighted_index.length)]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that the |
||
| if (chosen_items.indexOf(random) === -1) { | ||
| chosen_items.push(random); | ||
| } | ||
| } | ||
|
|
||
| // Map the chosen indexes back to their original objects. | ||
| var featured = []; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var featured = chosen_items.map(function (item) {
return all[item];
}); |
||
| for (var i = 0; i < chosen_items.length; i++) { | ||
| featured.push(all[chosen_items[i]]); | ||
| } | ||
|
|
||
| // Store and return the original objects, removing yesterday's cache. | ||
| storage.setItem(this.cacheKey(), JSON.stringify(featured)); | ||
| return featured; | ||
|
|
||
| }, | ||
|
|
||
| removeYesterday: function() { | ||
| // Remove the cache for yesterday. | ||
| console.log('Removing yesterday\'s selection of featured apps.'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use |
||
| var date = new Date(); | ||
| date.setDate(date.getDate() - 1); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're assuming they accessed it yesterday. what about the days prior?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's irritating. Should probably just make
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would store in localStorage an array of keys so you know which ones you can purge later |
||
| var key = this.cacheKey(date); | ||
| storage.removeItem(key); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add this to the prod bundle in the
Gruntfile.js. yeah it's not DRY but it's let us swap insettings_prod.jsinstead ofsettings_local.js.