-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
33 lines (33 loc) · 899 Bytes
/
engine.js
File metadata and controls
33 lines (33 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
const events = new EventEmitter();
/**
* @type {Map<string,Buffer>}
*/
let bufferPool = new Map();
function syncData(path) {
if (bufferPool.has(path)) return;
bufferPool.set(path, fs.readFileSync(path));
}
/**
*
* @param {string} path
* @param {object} data
*/
function writeFile(path, data) {
bufferPool.set(path, Buffer.from(JSON.stringify(data)));
process.nextTick(() => {
events.emit('write', path, data);
});
}
function parseBuffer(buffer) {
return JSON.parse(buffer.toString());
}
function getParsedBuffer(path) {
return bufferPool.has(path) ? parseBuffer(bufferPool.get(path)) : undefined;
}
events.on('write', async (path, data) => {
let dat = Buffer.from(JSON.stringify(data, null, '\t'));
fs.writeFileSync(path, dat);
});
exports.engine = { getParsedBuffer, bufferPool, writeFile, syncData };