🎨 moved checkloop outside main loop function

This commit is contained in:
ryzetech 2023-01-19 08:30:37 +01:00
parent 3577f3df55
commit 806b3198a1
2 changed files with 22 additions and 13 deletions

30
app.js
View file

@ -42,22 +42,26 @@ app.listen(4000);
(async function () { (async function () {
for await (const time of setInterval(config.checkperiod * 10)) { for await (const time of setInterval(config.checkperiod * 10)) {
console.log("Checking for spaces..."); console.log("Checking for spaces...");
for (const space of spaces) { await loop();
console.log(`Checking ${space.id}...`);
let o = await checkSpace(space);
console.log(`Space ${space.id} is ${o ? "open" : "closed"}`);
if (o !== cache.get(space.id)) {
cache.set(space.id, o);
let update = await prisma.space.upsert({
where: { id: space.id },
update: { open: o },
create: { id: space.id, open: o }
});
}
}
} }
})(); })();
async function loop() {
for (const space of spaces) {
console.log(`Checking ${space.id}...`);
let o = await checkSpace(space);
console.log(`Space ${space.id} is ${o ? "open" : "closed"}`);
if (o !== cache.get(space.id)) {
cache.set(space.id, o);
let update = await prisma.space.upsert({
where: { id: space.id },
update: { open: o },
create: { id: space.id, open: o }
});
}
}
}
// HELPER FUNCTIONS // HELPER FUNCTIONS
async function checkSpace(space) { async function checkSpace(space) {
let response, data, open; let response, data, open;

View file

@ -8,5 +8,10 @@
"node-cache": "^5.1.2", "node-cache": "^5.1.2",
"node-fetch": "2", "node-fetch": "2",
"prisma": "^4.9.0" "prisma": "^4.9.0"
},
"scripts": {
"start": "node app.js",
"generate": "npx prisma generate dev",
"migrate": "npx prisma migrate"
} }
} }