Swap to async/await rather than promise chains

Since we do in fact support coroutines!
This commit is contained in:
Kegan Dougal 2017-01-19 16:40:54 +00:00
parent 81d437ac1e
commit bf887e82fe
1 changed files with 65 additions and 96 deletions

View File

@ -166,7 +166,7 @@ class IndexedDBLogStore {
* when the log file was created (the log ID). The objects have said log ID in an "id" field and "lines" which is a * when the log file was created (the log ID). The objects have said log ID in an "id" field and "lines" which is a
* big string with all the new-line delimited logs. * big string with all the new-line delimited logs.
*/ */
consume(clearAll) { async consume(clearAll) {
const MAX_LOG_SIZE = 1024 * 1024 * 50; // 50 MB const MAX_LOG_SIZE = 1024 * 1024 * 50; // 50 MB
const db = this.db; const db = this.db;
@ -223,65 +223,36 @@ class IndexedDBLogStore {
}); });
} }
// Ideally we'd just use coroutines and a for loop but riot-web doesn't support async/await so instead let allLogIds = await fetchLogIds();
// recursively fetch logs up to the given threshold. We can't cheat and fetch all the logs let removeLogIds = [];
// from all time, but we may OOM if we do so. let logs = [];
// Returns: Promise<Object[]> : Each object having 'id' and 'lines'. Same ordering as logIds.
function fetchLogsToThreshold(logIds, threshold, logs) {
// Base case: check log size and return if bigger than threshold
let size = 0; let size = 0;
logs.forEach((l) => { for (let i = 0; i < allLogIds.length; i++) {
size += l.lines.length; let lines = await fetchLogs(allLogIds[i]);
});
if (size > threshold) {
return Promise.resolve(logs);
}
// fetch logs for the first element
let logId = logIds.shift();
if (!logId) {
// no more entries
return Promise.resolve(logs);
}
return fetchLogs(logId).then((lines) => {
// add result to logs
logs.push({ logs.push({
lines: lines, lines: lines,
id: logId, id: allLogIds[i],
}); });
// recurse with the next log ID. TODO: Stack overflow risk? size += lines.length;
return fetchLogsToThreshold(logIds, threshold, logs); if (size > MAX_LOG_SIZE) {
}) // the remaining log IDs should be removed. If we go out of bounds this is just []
} removeLogIds = allLogIds.slice(i + 1);
break;
let allLogIds = [];
return fetchLogIds().then((logIds) => {
allLogIds = logIds.map((id) => id); // deep copy array as we'll modify it when fetching logs
return fetchLogsToThreshold(logIds, MAX_LOG_SIZE, []);
}).then((logs) => {
// Remove all logs that are beyond the threshold (not in logs), or the entire logs if clearAll was set.
let removeLogIds = allLogIds;
if (!clearAll) {
removeLogIds = removeLogIds.filter((id) => {
for (let i = 0; i < logs.length; i++) {
if (logs[i].id === id) {
return false; // do not remove logs that we're about to return to the caller.
} }
} }
return true; if (clearAll) {
}); removeLogIds = allLogIds;
} }
if (removeLogIds.length > 0) { if (removeLogIds.length > 0) {
console.log("Removing logs: ", removeLogIds); console.log("Removing logs: ", removeLogIds);
// Don't promise chain this because it's non-fatal if we can't clean up logs. // Don't await this because it's non-fatal if we can't clean up logs.
Promise.all(removeLogIds.map((id) => deleteLogs(id))).then(() => { Promise.all(removeLogIds.map((id) => deleteLogs(id))).then(() => {
console.log(`Removed ${removeLogIds.length} old logs.`); console.log(`Removed ${removeLogIds.length} old logs.`);
}, (err) => { }, (err) => {
console.error(err); console.error(err);
}) })
} }console.log("async consumeeeee");
return logs; return logs;
});
} }
_generateLogEntry(lines) { _generateLogEntry(lines) {
@ -350,22 +321,22 @@ module.exports = {
* Force-flush the logs to storage. * Force-flush the logs to storage.
* @return {Promise} Resolved when the logs have been flushed. * @return {Promise} Resolved when the logs have been flushed.
*/ */
flush: function() { flush: async function() {
if (!store) { if (!store) {
return Promise.resolve(); return;
} }
return store.flush(); await store.flush();
}, },
/** /**
* Clean up old logs. * Clean up old logs.
* @return Promise Resolves if cleaned logs. * @return Promise Resolves if cleaned logs.
*/ */
cleanup: function() { cleanup: async function() {
if (!store) { if (!store) {
return Promise.resolve(); return;
} }
return store.consume(false); await store.consume(false);
}, },
/** /**
@ -373,17 +344,16 @@ module.exports = {
* @param {string} userText Any additional user input. * @param {string} userText Any additional user input.
* @return {Promise} Resolved when the bug report is sent. * @return {Promise} Resolved when the bug report is sent.
*/ */
sendBugReport: function(userText) { sendBugReport: async function(userText) {
if (!logger) { if (!logger) {
return Promise.reject(new Error("No console logger, did you forget to call init()?")); throw new Error("No console logger, did you forget to call init()?");
} }
// If in incognito mode, store is null, but we still want bug report sending to work going off // If in incognito mode, store is null, but we still want bug report sending to work going off
// the in-memory console logs. // the in-memory console logs.
let promise = Promise.resolve([]); let logs = [];
if (store) { if (store) {
promise = store.consume(false); // TODO Swap to true to remove all logs logs = await store.consume(false);
} }
return promise.then((logs) => {
// and add the most recent console logs which won't be in the store yet. // and add the most recent console logs which won't be in the store yet.
const consoleLogs = logger.flush(); // remove logs from console const consoleLogs = logger.flush(); // remove logs from console
const currentId = store ? store.id : "-"; const currentId = store ? store.id : "-";
@ -391,7 +361,7 @@ module.exports = {
lines: consoleLogs, lines: consoleLogs,
id: currentId, id: currentId,
}); });
return new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
request({ request({
method: "POST", method: "POST",
url: "http://localhost:1337", url: "http://localhost:1337",
@ -412,6 +382,5 @@ module.exports = {
resolve(); resolve();
}) })
}); });
});
} }
}; };