Remove async/await from lib

This commit is contained in:
Emil Tholin
2018-01-05 23:19:40 +01:00
parent ee94f355d5
commit 2b3472b1b1
2 changed files with 79 additions and 59 deletions

View File

@@ -23,9 +23,11 @@ function connect_dev() {
heartbeat: 10 * 1000
}),
async (req, res, next) => {
asset_cache = await watcher.ready;
(req, res, next) => {
watcher.ready.then(cache => {
asset_cache = cache;
next();
});
},
set_req_pathname,
@@ -126,7 +128,7 @@ function get_asset_handler(opts) {
}
function get_route_handler(fn) {
return async function handle_route(req, res, next) {
return function handle_route(req, res, next) {
const url = req.pathname;
const { client, server } = fn();
@@ -136,8 +138,15 @@ function get_route_handler(fn) {
'Content-Type': 'text/html'
});
try {
for (const route of route_manager.routes) {
let i = 0;
function handle_route_inner() {
if (i === route_manager.routes.length) {
next();
return;
}
const route = route_manager.routes[i];
if (route.test(url)) {
req.params = route.exec(url);
@@ -156,7 +165,7 @@ function get_route_handler(fn) {
return mod.render(data);
});
await templates.stream(res, 200, {
return templates.stream(res, 200, {
main: client.main_file,
html: promise.then(rendered => rendered.html),
head: promise.then(({ head }) => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`),
@@ -183,10 +192,12 @@ function get_route_handler(fn) {
return;
}
++i;
return handle_route_inner();
}
next();
} catch(err) {
return Promise.resolve().then(handle_route_inner).catch(err => {
res.status(500);
res.end(templates.render(500, {
title: (err && err.name) || 'Internal server error',
@@ -194,7 +205,7 @@ function get_route_handler(fn) {
error: escape_html(err && (err.details || err.message || err) || 'Unknown error'),
stack: err && err.stack.split('\n').slice(1).join('\n')
}));
}
});
};
}

View File

@@ -31,10 +31,14 @@ function create_templates() {
return key in data ? data[key] : '';
});
},
stream: async (res, data) => {
stream: (res, data) => {
let i = 0;
do {
function stream_inner() {
if (i >= template.length) {
return;
}
const start = template.indexOf('%sapper', i);
if (start === -1) {
@@ -53,9 +57,14 @@ function create_templates() {
const match = /sapper\.(\w+)/.exec(tag);
if (!match || !(match[1] in data)) throw new Error(`Bad template`); // TODO ditto
res.write(await data[match[1]]);
return Promise.resolve(data[match[1]]).then(datamatch => {
res.write(datamatch);
i = end + 1;
} while (i < template.length);
return stream_inner();
});
}
return Promise.resolve().then(stream_inner);
}
};
})