initial
This commit is contained in:
126
autosave.mjs
Normal file
126
autosave.mjs
Normal file
@@ -0,0 +1,126 @@
|
||||
throw new Error('Not implemented');
|
||||
|
||||
//TODO - this sketch is messy and incomplete
|
||||
|
||||
class Autosaver {
|
||||
|
||||
#autosave_timer = null
|
||||
#buffer_file_stream = null
|
||||
|
||||
constructor(line_buffer=[], buffer_file=null, autosave=false, autosave_interval=600) {
|
||||
Object.assign({ line_buffer, buffer_file, autosave, autosave_interval });
|
||||
this.apply_configuration(true);
|
||||
}
|
||||
|
||||
|
||||
perform_autosave() {
|
||||
// TODO: flush pending lines to buffer_file
|
||||
if (this.autosave) {
|
||||
console.log("Autosave not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
constructor(line_buffer=[], buffer_file=null, autosave=false, autosave_interval=600) {
|
||||
Object.assign({ line_buffer, buffer_file, autosave, autosave_interval });
|
||||
this.apply_configuration(true);
|
||||
}
|
||||
|
||||
#get_buffer_writer() {
|
||||
|
||||
//TODO: This function should be properly vetted (ChatGPT 5.2)
|
||||
|
||||
if (!this.buffer_file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.#buffer_file_stream) {
|
||||
try {
|
||||
this.#buffer_file_stream = fs.createWriteStream(this.buffer_file, {
|
||||
flags: 'a',
|
||||
encoding: 'latin1'
|
||||
});
|
||||
|
||||
this.#buffer_file_stream.on('error', (err) => {
|
||||
console.log("Buffer stream error:", err);
|
||||
|
||||
try {
|
||||
this.#buffer_file_stream.destroy();
|
||||
} catch (destroy_err) {
|
||||
console.log("Failed to destroy buffer stream due to", destroy_err);
|
||||
}
|
||||
|
||||
this.#buffer_file_stream = null;
|
||||
});
|
||||
} catch (err) {
|
||||
console.log("Failed to open buffer file due to", err);
|
||||
this.#buffer_file_stream = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const stream = this.#buffer_file_stream;
|
||||
|
||||
const write_line = (line) => {
|
||||
|
||||
if (!stream.writable) {
|
||||
this.#buffer_file_stream = null;
|
||||
return;
|
||||
}
|
||||
|
||||
stream.write(line + '\n');
|
||||
|
||||
};
|
||||
|
||||
return write_line;
|
||||
}
|
||||
|
||||
|
||||
#maybe_read_previous_buffer_file() {
|
||||
if (this.buffer_file) {
|
||||
let content;
|
||||
try {
|
||||
content = fs.readFileSync(this.buffer_file, { encoding: 'latin1' });
|
||||
} catch (err) {
|
||||
console.log("Failed to read buffer file due to", err);
|
||||
}
|
||||
|
||||
if (content !== undefined) {
|
||||
const lines = content.split('\n');
|
||||
for (const line of lines) {
|
||||
this.log_line(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#configure_autosave_timer() {
|
||||
if (this.#autosave_timer) {
|
||||
clearInterval(this.#autosave_timer);
|
||||
this.#autosave_timer = null;
|
||||
}
|
||||
|
||||
if (this.autosave && this.autosave_interval > 0) {
|
||||
this.#autosave_timer = setInterval(() => {
|
||||
this.perform_autosave();
|
||||
}, this.autosave_interval * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
apply_configuration(starting = false) {
|
||||
|
||||
if (starting === true) {
|
||||
this.#maybe_read_previous_buffer_file();
|
||||
}
|
||||
|
||||
this.#configure_autosave_timer();
|
||||
|
||||
}
|
||||
|
||||
|
||||
buffer_line(line) {
|
||||
const write_line = this.#get_buffer_writer();
|
||||
write_line?.(line);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user