Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<script src="media/js/worker.js"></script>
<script src="media/js/pages.js"></script>
<script src="media/js/apps.js"></script>
<script src="media/js/featured.js"></script>
Copy link
Owner

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 in settings_prod.js instead of settings_local.js.

<script src="media/js/views/search.js"></script>
<script src="media/js/main.js"></script>
</body>
Expand Down
88 changes: 88 additions & 0 deletions src/media/js/featured.js
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'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storage.getItem('all_featured') || '{}'

if (!cached) {
cached = [
{ name: 'trenta', weight: 10 },
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you have to google this?

Copy link
Author

Choose a reason for hiding this comment

The 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()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semicolon;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storage.getItem(...) || '{}'

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') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after if

date = new Date();
};
return 'featured_' + date.toDateString().replace(' ', '_').toLowerCase();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ /g to replace all the spaces

Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling error: "randomised"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not British.

// displayed on the homepage. Store
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Store" whaaaa?

console.log('Generating a new selection of featured apps.');
Copy link
Owner

Choose a reason for hiding this comment

The 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 = [];
Copy link
Owner

Choose a reason for hiding this comment

The 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)];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the * weighted_index.length is to get a random number within the range of indices we have, but a comment would be good

if (chosen_items.indexOf(random) === -1) {
chosen_items.push(random);
}
}

// Map the chosen indexes back to their original objects.
var featured = [];
Copy link
Owner

Choose a reason for hiding this comment

The 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.');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use "

var date = new Date();
date.setDate(date.getDate() - 1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're assuming they accessed it yesterday. what about the days prior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's irritating. Should probably just make featured_apps be a key and work off featured_apps_last_updated.

Copy link
Owner

Choose a reason for hiding this comment

The 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);
}

};

});
5 changes: 4 additions & 1 deletion src/media/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ define('settings', ['settings_local', 'utils'], function(settings_local, utils)

// How often (in milliseconds) to check if we have queued apps to install
// when we return online.
offlineInterval: 1000
offlineInterval: 1000,

// Number of apps featured on the homepage.
numberFeatured: 6
});

// The version number for `localStorage` data. Bump when the schema for
Expand Down