Merge pull request #527 from Hornwitser/autosave-data

Implement autosaving of data held in memory
This commit is contained in:
Hornwitser authored and GitHub committed 2023-12-01 02:30:54 +01:00
commit f802e6ba67
22 files changed
+366 -138

No files matched your search

+6
View File
@@ -85,6 +85,9 @@ export class Config extends events.EventEmitter {
_groups = new Map<string, ConfigGroup>();
_unknownGroups = new Map<string, SerializedGroup>();
/** Set to true when a field in the config is changed. */
dirty = false;
/**
* Create a new instance of the given config
*
@@ -566,6 +569,7 @@ export class ConfigGroup {
let prev = this._fields.get(name);
this._fields.set(name, value);
if (this.config && !isDeepStrictEqual(value, prev)) {
this.config.dirty = true;
this.config.emit("fieldChanged", this, name, prev);
}
}
@@ -604,6 +608,7 @@ export class ConfigGroup {
}
this._fields.set(name, updated);
if (this.config && !isDeepStrictEqual(updated, prev)) {
this.config.dirty = true;
this.config.emit("fieldChanged", this, name, prev);
}
}
@@ -663,6 +668,7 @@ export class ConfigGroup {
let prev = this._fields.get(name);
this._fields.set(name, value);
if (notify && this.config && !isDeepStrictEqual(value, prev)) {
this.config.dirty = true;
this.config.emit("fieldChanged", this, name, prev);
}
}
+8
View File
@@ -31,6 +31,13 @@ ControllerGroup.define({
type: "string",
initial_value: "database",
});
ControllerGroup.define({
name: "autosave_interval",
title: "Autosave Interval",
description: "Interval in seconds to autosave data in memory to disk.",
type: "number",
initial_value: 60,
});
ControllerGroup.define({
name: "http_port",
title: "HTTP Port",
@@ -157,6 +164,7 @@ export class ControllerConfig extends classes.Config {
get(name: "controller.proxy_stream_timeout"): number;
get(name: "controller.default_mod_pack_id"): number | null;
get(name: "controller.default_role_id"): number | null;
get(name: "controller.autosave_interval"): number;
get(name: string): unknown;
get(name: string) {
return super.get(name);
+56
View File
@@ -51,6 +51,62 @@ export async function timeout<T>(promise: Promise<T>, time: number, timeoutResul
}
}
/**
* Helper to serialise and merge waiting calls to an async callback.
*
* Provides a convenient interface to serialise the calls made to an async
* callback such that no overlapping invocations of the callback is made.
* If the callback is already running when another invocation is requested
* the request will be queued until the callback returns and then an the
* callback called again.
*
* If multiple calls are made while the callback is running then these will
* be merged into one call of the callback.
*/
export class AsyncSerialMergingCallback {
private _currentlyRunning = false;
private _currentlyWaiting: (() => void)[] = [];
/**
* @param callback - Async function to serialise access to
*/
constructor(
public callback: () => Promise<void>,
) { }
/**
* Invoke the assosiated callback.
*
* If the callback is currently running then this will wait until the
* existing invocation finishes and then invoke it again. If called
* multiple times while an invocation is running the calls will be
* merged into one call.
*/
async invoke() {
if (this._currentlyRunning) {
const waitForCompletion = () => new Promise<void>(resolve => {
this._currentlyWaiting.push(resolve);
});
await waitForCompletion();
if (this._currentlyRunning) {
await waitForCompletion();
return;
}
}
this._currentlyRunning = true;
try {
await this.callback();
} finally {
this._currentlyRunning = false;
for (const waiter of this._currentlyWaiting) {
waiter();
}
this._currentlyWaiting.length = 0;
}
}
}
/**
* Read stream to the end and return its content