-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a transparent cache called "fetcher" to prevent making 2 XHRs (both from the frontend to the backend).
- Loading branch information
Showing
3 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |