Skip to content
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

Make / work under FastBoot #1937

Merged
merged 3 commits into from
Jan 3, 2020
Merged
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
28 changes: 1 addition & 27 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { readOnly } from '@ember/object/computed';

import ajax from 'ember-fetch/ajax';
import { task } from 'ember-concurrency';

export default Controller.extend({
model: readOnly('dataTask.lastSuccessful.value'),

hasData: computed('dataTask.{lastSuccessful,isRunning}', function() {
return this.get('dataTask.lastSuccessful') || !this.get('dataTask.isRunning');
}),

dataTask: task(function*() {
let data = yield ajax('/api/v1/summary');

addCrates(this.store, data.new_crates);
addCrates(this.store, data.most_downloaded);
addCrates(this.store, data.just_updated);
addCrates(this.store, data.most_recently_downloaded);

return data;
}).drop(),
hasData: true,
});

function addCrates(store, crates) {
for (let i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
}
}
25 changes: 23 additions & 2 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
fetcher: service(),

headTags() {
return [
{
Expand All @@ -13,8 +16,26 @@ export default Route.extend({
];
},

setupController(controller) {
setupController(controller, model) {
this._super(controller, model);
this.controllerFor('application').set('searchQuery', null);
controller.dataTask.perform();
},

model() {
return this.fetcher.ajax('/api/v1/summary');
},

// eslint-disable-next-line no-unused-vars
afterModel(model, transition) {
addCrates(this.store, model.new_crates);
addCrates(this.store, model.most_downloaded);
addCrates(this.store, model.just_updated);
addCrates(this.store, model.most_recently_downloaded);
},
});

function addCrates(store, crates) {
for (let i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
}
}
31 changes: 31 additions & 0 deletions app/services/fetcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Service, { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Service.extend({
fastboot: service(),

ajax(url) {
let fastboot = this.fastboot;
let shoebox = this.fastboot.shoebox;
let cache = shoebox.retrieve('ajax-cache');
if (!cache) {
cache = {};
}

if (cache[url]) {
return cache[url];
}

return ajax(url).then(function(resp) {
if (shoebox && fastboot.isFastBoot) {
cache[url] = deepCopy(resp);
shoebox.put('ajax-cache', cache);
}
return resp;
});
},
});

function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}