Fixed the PersistentLocalStorage.ts
This commit is contained in:
parent
be56fa7117
commit
90c9fad28b
@ -7,6 +7,8 @@ const SETTINGS_DIR = path.join(APP_DATA, "settings");
|
||||
|
||||
let _local_storage: {[key: string]: any} = {};
|
||||
let _local_storage_save: {[key: string]: boolean} = {};
|
||||
let _save_timer: NodeJS.Timer;
|
||||
|
||||
export async function initialize() {
|
||||
await fs.mkdirp(SETTINGS_DIR);
|
||||
|
||||
@ -21,7 +23,13 @@ export async function initialize() {
|
||||
|
||||
_local_storage[key] = decoded;
|
||||
} catch(error) {
|
||||
console.error("Failed to load settings for %s: %o", key, error);
|
||||
const target_file = path.join(SETTINGS_DIR, file + "." + Date.now() + ".broken");
|
||||
console.error("Failed to load settings for %s: %o. Moving settings so the file does not get overridden. Target file: %s", key, error, target_file);
|
||||
try {
|
||||
await fs.move(path.join(SETTINGS_DIR, file), target_file);
|
||||
} catch (error) {
|
||||
console.warn("Failed to move broken settings file!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,9 +39,6 @@ export async function initialize() {
|
||||
_new_storage.setItem = (key, value) => {
|
||||
_local_storage[key] = value;
|
||||
_local_storage_save[key] = true;
|
||||
save_key(key).catch(error => {
|
||||
console.warn("Failed to save key: %s => %o", key, error);
|
||||
});
|
||||
(_new_storage as any)["length"] = Object.keys(_local_storage).length;
|
||||
};
|
||||
|
||||
@ -59,24 +64,37 @@ export async function initialize() {
|
||||
};
|
||||
|
||||
Object.assign(window.localStorage, _new_storage);
|
||||
|
||||
/* try to save everything all 60 seconds */
|
||||
_save_timer = setInterval(() => {
|
||||
save_all_sync();
|
||||
}, 60 * 1000);
|
||||
}
|
||||
|
||||
export async function save_all() {
|
||||
let promises: Promise<void>[] = [];
|
||||
for(const key of Object.keys(_local_storage))
|
||||
promises.push(save_key(key));
|
||||
await Promise.all(promises);
|
||||
export function save_all_sync() {
|
||||
for(const key of Object.keys(_local_storage_save))
|
||||
save_key_sync(key);
|
||||
}
|
||||
|
||||
export async function save_key(key: string) {
|
||||
function key_path(key: string) {
|
||||
return path.join(SETTINGS_DIR, encodeURIComponent(key));
|
||||
}
|
||||
|
||||
export function save_key_sync(key: string) {
|
||||
if(!_local_storage_save[key])
|
||||
return;
|
||||
|
||||
_local_storage_save[key] = false;
|
||||
await fs.writeJson(path.join(SETTINGS_DIR, encodeURIComponent(key)), _local_storage[key], {spaces: 0});
|
||||
delete _local_storage_save[key];
|
||||
const setting_path = key_path(key);
|
||||
fs.writeJsonSync(setting_path, _local_storage[key], {spaces: 0});
|
||||
}
|
||||
|
||||
export async function delete_key(key: string) {
|
||||
delete _local_storage_save[key];
|
||||
await fs.remove(path.join(SETTINGS_DIR, encodeURIComponent(key)));
|
||||
}
|
||||
const setting_path = key_path(key);
|
||||
await fs.remove(setting_path); /* could be async because we're not carrying about data */
|
||||
}
|
||||
|
||||
window.addEventListener("beforeunload", event => {
|
||||
save_all_sync();
|
||||
});
|
@ -24,8 +24,8 @@ function(setup_nodejs)
|
||||
set(NODEJS_URL "https://atom.io/download/atom-shell")
|
||||
set(NODEJS_VERSION "v8.0.0")
|
||||
|
||||
#set(NODEJS_URL "https://nodejs.org/download/release/")
|
||||
#set(NODEJS_VERSION "v12.13.0")
|
||||
# set(NODEJS_URL "https://nodejs.org/download/release/")
|
||||
# set(NODEJS_VERSION "v12.13.0")
|
||||
|
||||
find_package(NodeJS REQUIRED)
|
||||
|
||||
|
@ -167,6 +167,12 @@ void ProtocolHandler::progress_packet(const pipes::buffer_view &buffer) {
|
||||
auto ordered = packet_type.type() == protocol::COMMAND || packet_type.type() == protocol::COMMAND_LOW;
|
||||
//log_trace(category::connection, tr("Received packet {} with id {}"), packet->type().name(), packet->packetId());
|
||||
|
||||
/*
|
||||
if(ordered)
|
||||
if(rand() & 1)
|
||||
return;
|
||||
*/
|
||||
|
||||
/* special handling */
|
||||
if(packet_type.type() == protocol::INIT1) {
|
||||
this->handlePacketInit(packet);
|
||||
|
@ -61,8 +61,8 @@ const do_connect = () => {
|
||||
timeout: 5000,
|
||||
remote_port: 9987,
|
||||
//remote_host: "188.40.240.20", /* twerion */
|
||||
//remote_host: "127.0.0.1",
|
||||
remote_host: "ts.teaspeak.de",
|
||||
remote_host: "127.0.0.1",
|
||||
//remote_host: "ts.teaspeak.de",
|
||||
//remote_host: "51.68.181.92",
|
||||
//remote_host: "94.130.236.135",
|
||||
//remote_host: "54.36.232.11", /* the beast */
|
||||
|
Loading…
Reference in New Issue
Block a user