jsl data refactor

This commit is contained in:
Jan Prochazka
2020-10-22 08:23:53 +02:00
parent c4914429ce
commit da1617729b
5 changed files with 318 additions and 85 deletions

View File

@@ -2,10 +2,12 @@ const connectProcess = require('./connectProcess');
const databaseConnectionProcess = require('./databaseConnectionProcess');
const serverConnectionProcess = require('./serverConnectionProcess');
const sessionProcess = require('./sessionProcess');
const jslDatastoreProcess = require('./jslDatastoreProcess');
module.exports = {
connectProcess,
databaseConnectionProcess,
serverConnectionProcess,
sessionProcess,
jslDatastoreProcess,
};

View File

@@ -0,0 +1,57 @@
const childProcessChecker = require('../utility/childProcessChecker');
const JsonLinesDatastore = require('../utility/JsonLinesDatastore');
let lastPing = null;
let datastore = new JsonLinesDatastore();
function handlePing() {
lastPing = new Date().getTime();
}
function handleOpen({file }) {
handlePing();
datastore = new JsonLinesDatastore(file);
}
async function handleRead({ msgid, offset, limit }) {
handlePing();
const rows = await datastore.getRows(offset, limit);
process.send({ msgtype: 'response', msgid, rows });
}
function handleNotifyChanged() {
datastore.notifyChanged();
}
const messageHandlers = {
open: handleOpen,
read: handleRead,
ping: handlePing,
notifyChanged: handleNotifyChanged,
};
async function handleMessage({ msgtype, ...other }) {
const handler = messageHandlers[msgtype];
await handler(other);
}
function start() {
childProcessChecker();
setInterval(() => {
const time = new Date().getTime();
if (time - lastPing > 60 * 1000) {
process.exit(0);
}
}, 60 * 1000);
process.on('message', async (message) => {
try {
await handleMessage(message);
} catch (e) {
process.send({ msgtype: 'error', error: e.message });
}
});
}
module.exports = { start };