mirror of
https://github.com/SchildiChat/element-web.git
synced 2024-10-01 01:26:12 -04:00
Swap to async/await rather than promise chains
Since we do in fact support coroutines!
This commit is contained in:
parent
81d437ac1e
commit
bf887e82fe
@ -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
|
||||
* big string with all the new-line delimited logs.
|
||||
*/
|
||||
consume(clearAll) {
|
||||
async consume(clearAll) {
|
||||
const MAX_LOG_SIZE = 1024 * 1024 * 50; // 50 MB
|
||||
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
|
||||
// recursively fetch logs up to the given threshold. We can't cheat and fetch all the logs
|
||||
// from all time, but we may OOM if we do so.
|
||||
// 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 allLogIds = await fetchLogIds();
|
||||
let removeLogIds = [];
|
||||
let logs = [];
|
||||
let size = 0;
|
||||
logs.forEach((l) => {
|
||||
size += l.lines.length;
|
||||
});
|
||||
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
|
||||
for (let i = 0; i < allLogIds.length; i++) {
|
||||
let lines = await fetchLogs(allLogIds[i]);
|
||||
logs.push({
|
||||
lines: lines,
|
||||
id: logId,
|
||||
id: allLogIds[i],
|
||||
});
|
||||
// recurse with the next log ID. TODO: Stack overflow risk?
|
||||
return fetchLogsToThreshold(logIds, threshold, logs);
|
||||
})
|
||||
}
|
||||
|
||||
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.
|
||||
size += lines.length;
|
||||
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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (clearAll) {
|
||||
removeLogIds = allLogIds;
|
||||
}
|
||||
if (removeLogIds.length > 0) {
|
||||
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(() => {
|
||||
console.log(`Removed ${removeLogIds.length} old logs.`);
|
||||
}, (err) => {
|
||||
console.error(err);
|
||||
})
|
||||
}
|
||||
}console.log("async consumeeeee");
|
||||
return logs;
|
||||
});
|
||||
}
|
||||
|
||||
_generateLogEntry(lines) {
|
||||
@ -350,22 +321,22 @@ module.exports = {
|
||||
* Force-flush the logs to storage.
|
||||
* @return {Promise} Resolved when the logs have been flushed.
|
||||
*/
|
||||
flush: function() {
|
||||
flush: async function() {
|
||||
if (!store) {
|
||||
return Promise.resolve();
|
||||
return;
|
||||
}
|
||||
return store.flush();
|
||||
await store.flush();
|
||||
},
|
||||
|
||||
/**
|
||||
* Clean up old logs.
|
||||
* @return Promise Resolves if cleaned logs.
|
||||
*/
|
||||
cleanup: function() {
|
||||
cleanup: async function() {
|
||||
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.
|
||||
* @return {Promise} Resolved when the bug report is sent.
|
||||
*/
|
||||
sendBugReport: function(userText) {
|
||||
sendBugReport: async function(userText) {
|
||||
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
|
||||
// the in-memory console logs.
|
||||
let promise = Promise.resolve([]);
|
||||
let logs = [];
|
||||
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.
|
||||
const consoleLogs = logger.flush(); // remove logs from console
|
||||
const currentId = store ? store.id : "-";
|
||||
@ -391,7 +361,7 @@ module.exports = {
|
||||
lines: consoleLogs,
|
||||
id: currentId,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
await new Promise((resolve, reject) => {
|
||||
request({
|
||||
method: "POST",
|
||||
url: "http://localhost:1337",
|
||||
@ -412,6 +382,5 @@ module.exports = {
|
||||
resolve();
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user