Skip to content

Commit 07dda0a

Browse files
committed
move fetch code into the fetchMixin class
1 parent 5525b23 commit 07dda0a

File tree

2 files changed

+84
-76
lines changed

2 files changed

+84
-76
lines changed

src/core/fetch/index.js

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,56 @@ import { noop } from '../util/core';
44
import { getAndActive } from '../event/sidebar';
55
import { get } from './ajax';
66

7-
function loadNested(path, qs, file, next, vm, first) {
8-
path = first ? path : path.replace(/\/$/, '');
9-
path = getParentPath(path);
10-
11-
if (!path) {
12-
return;
13-
}
14-
15-
get(
16-
vm.router.getFile(path + file) + qs,
17-
false,
18-
vm.config.requestHeaders
19-
).then(next, _ => loadNested(path, qs, file, next, vm));
20-
}
21-
22-
let last;
23-
24-
const abort = () => last && last.abort && last.abort();
25-
const request = (url, hasbar, requestHeaders) => {
26-
abort();
27-
last = get(url, true, requestHeaders);
28-
return last;
29-
};
30-
31-
const get404Path = (path, config) => {
32-
const { notFoundPage, ext } = config;
33-
const defaultPath = '_404' + (ext || '.md');
34-
let key;
35-
let path404;
36-
37-
switch (typeof notFoundPage) {
38-
case 'boolean':
39-
path404 = defaultPath;
40-
break;
41-
case 'string':
42-
path404 = notFoundPage;
43-
break;
7+
export function fetchMixin(Base = class {}) {
8+
return class extends Base {
9+
constructor() {
10+
super();
11+
this._lastRequest = null;
12+
}
4413

45-
case 'object':
46-
key = Object.keys(notFoundPage)
47-
.sort((a, b) => b.length - a.length)
48-
.find(key => path.match(new RegExp('^' + key)));
14+
_abort() {
15+
return (
16+
this._lastRequest &&
17+
this._lastRequest.abort &&
18+
this._lastRequest.abort()
19+
);
20+
}
4921

50-
path404 = (key && notFoundPage[key]) || defaultPath;
51-
break;
22+
_request(url, hasbar, requestHeaders) {
23+
this._abort();
24+
this._lastRequest = get(url, true, requestHeaders);
25+
return this._lastRequest;
26+
}
5227

53-
default:
54-
break;
55-
}
28+
_get404Path(path, config) {
29+
const { notFoundPage, ext } = config;
30+
const defaultPath = '_404' + (ext || '.md');
31+
let key;
32+
let path404;
33+
34+
switch (typeof notFoundPage) {
35+
case 'boolean':
36+
path404 = defaultPath;
37+
break;
38+
case 'string':
39+
path404 = notFoundPage;
40+
break;
41+
42+
case 'object':
43+
key = Object.keys(notFoundPage)
44+
.sort((a, b) => b.length - a.length)
45+
.find(key => path.match(new RegExp('^' + key)));
46+
47+
path404 = (key && notFoundPage[key]) || defaultPath;
48+
break;
49+
50+
default:
51+
break;
52+
}
5653

57-
return path404;
58-
};
54+
return path404;
55+
}
5956

60-
export function fetchMixin(Base = class {}) {
61-
return class extends Base {
6257
_loadSideAndNav(path, qs, loadSidebar, cb) {
6358
return () => {
6459
if (!loadSidebar) {
@@ -71,7 +66,7 @@ export function fetchMixin(Base = class {}) {
7166
};
7267

7368
// Load sidebar
74-
loadNested(path, qs, loadSidebar, fn, this, true);
69+
this._loadNested(path, qs, loadSidebar, fn, true);
7570
};
7671
}
7772

@@ -82,7 +77,7 @@ export function fetchMixin(Base = class {}) {
8277
// Abort last request
8378

8479
const file = this.router.getFile(path);
85-
const req = request(file + qs, true, requestHeaders);
80+
const req = this._request(file + qs, true, requestHeaders);
8681

8782
// Current page is html
8883
this.isHTML = /\.html$/g.test(file);
@@ -102,16 +97,30 @@ export function fetchMixin(Base = class {}) {
10297

10398
// Load nav
10499
loadNavbar &&
105-
loadNested(
100+
this._loadNested(
106101
path,
107102
qs,
108103
loadNavbar,
109104
text => this._renderNav(text),
110-
this,
111105
true
112106
);
113107
}
114108

109+
_loadNested(path, qs, file, next, first) {
110+
path = first ? path : path.replace(/\/$/, '');
111+
path = getParentPath(path);
112+
113+
if (!path) {
114+
return;
115+
}
116+
117+
get(
118+
this.router.getFile(path + file) + qs,
119+
false,
120+
this.config.requestHeaders
121+
).then(next, _ => this._loadNested(path, qs, file, next));
122+
}
123+
115124
_fetchCover() {
116125
const { coverpage, requestHeaders } = this.config;
117126
const query = this.route.query;
@@ -180,7 +189,7 @@ export function fetchMixin(Base = class {}) {
180189
}
181190

182191
const newPath = path.replace(new RegExp(`^/${local}`), '');
183-
const req = request(newPath + qs, true, requestHeaders);
192+
const req = this._request(newPath + qs, true, requestHeaders);
184193

185194
req.then(
186195
(text, opt) =>
@@ -208,9 +217,9 @@ export function fetchMixin(Base = class {}) {
208217

209218
const fnLoadSideAndNav = this._loadSideAndNav(path, qs, loadSidebar, cb);
210219
if (notFoundPage) {
211-
const path404 = get404Path(path, this.config);
220+
const path404 = this._get404Path(path, this.config);
212221

213-
request(this.router.getFile(path404), true, requestHeaders).then(
222+
this._request(this.router.getFile(path404), true, requestHeaders).then(
214223
(text, opt) => this._renderMain(text, opt, fnLoadSideAndNav),
215224
() => this._renderMain(null, {}, fnLoadSideAndNav)
216225
);
@@ -220,24 +229,24 @@ export function fetchMixin(Base = class {}) {
220229
this._renderMain(null, {}, fnLoadSideAndNav);
221230
return false;
222231
}
223-
};
224-
}
225232

226-
export function initFetch(vm) {
227-
const { loadSidebar } = vm.config;
233+
initFetch() {
234+
const { loadSidebar } = this.config;
228235

229-
// Server-Side Rendering
230-
if (vm.rendered) {
231-
const activeEl = getAndActive(vm.router, '.sidebar-nav', true, true);
232-
if (loadSidebar && activeEl) {
233-
activeEl.parentNode.innerHTML += window.__SUB_SIDEBAR__;
234-
}
236+
// Server-Side Rendering
237+
if (this.rendered) {
238+
const activeEl = getAndActive(this.router, '.sidebar-nav', true, true);
239+
if (loadSidebar && activeEl) {
240+
activeEl.parentNode.innerHTML += window.__SUB_SIDEBAR__;
241+
}
235242

236-
vm._bindEventOnRendered(activeEl);
237-
vm.$resetEvents();
238-
vm.callHook('doneEach');
239-
vm.callHook('ready');
240-
} else {
241-
vm.$fetch(_ => vm.callHook('ready'));
242-
}
243+
this._bindEventOnRendered(activeEl);
244+
this.$resetEvents();
245+
this.callHook('doneEach');
246+
this.callHook('ready');
247+
} else {
248+
this.$fetch(_ => this.callHook('ready'));
249+
}
250+
}
251+
};
243252
}

src/core/init/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import config from '../config';
2-
import { initFetch } from '../fetch';
32

43
export function initMixin(Base = class {}) {
54
return class extends Base {
@@ -12,7 +11,7 @@ export function initMixin(Base = class {}) {
1211
this.initRouter(); // Add router
1312
this.initRender(); // Render base DOM
1413
this.initEvent(); // Bind events
15-
initFetch(this); // Fetch data
14+
this.initFetch(); // Fetch data
1615
this.callHook('mounted');
1716
}
1817
};

0 commit comments

Comments
 (0)