unsaved connections limit

This commit is contained in:
SPRINX0\prochazka
2024-12-03 15:20:28 +01:00
parent 72bd536aec
commit 8e5ef98a7c
2 changed files with 33 additions and 0 deletions

View File

@@ -201,6 +201,7 @@ module.exports = {
// @ts-ignore // @ts-ignore
this.datastore = new JsonLinesDatabase(path.join(dir, 'connections.jsonl')); this.datastore = new JsonLinesDatabase(path.join(dir, 'connections.jsonl'));
} }
await this.checkUnsavedConnectionsLimit();
}, },
list_meta: true, list_meta: true,
@@ -300,6 +301,29 @@ module.exports = {
return res; return res;
}, },
async checkUnsavedConnectionsLimit() {
const MAX_UNSAVED_CONNECTIONS = 5;
await this.datastore.transformAll(connections => {
const count = connections.filter(x => x.unsaved).length;
if (count > MAX_UNSAVED_CONNECTIONS) {
const res = [];
let unsavedToSkip = count - MAX_UNSAVED_CONNECTIONS;
for (const item of connections) {
if (item.unsaved) {
if (unsavedToSkip > 0) {
unsavedToSkip--;
} else {
res.push(item);
}
} else {
res.push(item);
}
}
return res;
}
});
},
update_meta: true, update_meta: true,
async update({ _id, values }, req) { async update({ _id, values }, req) {
if (portalConnections) return; if (portalConnections) return;

View File

@@ -111,6 +111,15 @@ class JsonLinesDatabase {
return removed; return removed;
} }
async transformAll(transformFunction) {
await this._ensureLoaded();
const newData = transformFunction(this.data);
if (newData) {
this.data = newData;
await this._save();
}
}
// async _openReader() { // async _openReader() {
// return new Promise((resolve, reject) => // return new Promise((resolve, reject) =>
// lineReader.open(this.filename, (err, reader) => { // lineReader.open(this.filename, (err, reader) => {