Skip to content

Commit 03f0cdf

Browse files
committed
models/version: Extract loadReadmeTask
1 parent 06c8e2a commit 03f0cdf

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

app/controllers/crate/version.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,18 @@ export default Controller.extend({
128128
}),
129129

130130
loadReadmeTask: task(function* () {
131-
if (this.currentVersion.get('readme_path')) {
132-
try {
133-
let r = yield fetch(this.currentVersion.get('readme_path'));
134-
if (r.ok) {
135-
this.crate.set('readme', yield r.text());
136-
137-
if (typeof document !== 'undefined') {
138-
setTimeout(() => {
139-
let e = document.createEvent('CustomEvent');
140-
e.initCustomEvent('hashchange', true, true);
141-
window.dispatchEvent(e);
142-
});
143-
}
144-
} else {
145-
this.crate.set('readme', null);
146-
}
147-
} catch (error) {
148-
this.crate.set('readme', null);
131+
try {
132+
this.crate.set('readme', yield this.currentVersion.loadReadmeTask.perform());
133+
134+
if (typeof document !== 'undefined') {
135+
setTimeout(() => {
136+
let e = document.createEvent('CustomEvent');
137+
e.initCustomEvent('hashchange', true, true);
138+
window.dispatchEvent(e);
139+
});
149140
}
141+
} catch (error) {
142+
this.crate.set('readme', null);
150143
}
151144
}),
152145

app/models/version.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ export default Model.extend({
3939

4040
return { normal, build, dev };
4141
}).keepLatest(),
42+
43+
loadReadmeTask: task(function* () {
44+
if (this.readme_path) {
45+
let response = yield fetch(this.readme_path);
46+
if (!response.ok) {
47+
throw new Error(`README request for ${this.crateName} v${this.num} failed`);
48+
}
49+
50+
return yield response.text();
51+
}
52+
}).keepLatest(),
4253
});

0 commit comments

Comments
 (0)