You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ycrpg/js/plugins/SF_AutoUpdateWorker.js

77 lines
2.7 KiB
JavaScript

onmessage = function (e) {
var request = e.data;
3 years ago
var result_files = {};
result_files.delete_list = [];
result_files.update_list = [];
3 years ago
switch (request.command) {
case "compare":
3 years ago
compare_file_info(request.local_file_info, request.remote_file_info, result_files);
send_command("delete", result_files.delete_list);
send_command("update", result_files.update_list);
send_command("finish", []);
break;
case "finish":
send_command("finish");
break;
}
};
3 years ago
function compare_file_info(local_file_info, remote_file_info, result_files) {
3 years ago
if (!remote_file_info) {
3 years ago
result_files.delete_list = result_files.delete_list.concat(generate_file_list(local_file_info));
3 years ago
} else if (!local_file_info) {
3 years ago
result_files.update_list = result_files.update_list.concat(generate_file_list(remote_file_info));
3 years ago
} else if (local_file_info.is_dir !== remote_file_info.is_dir) {
3 years ago
result_files.delete_list = result_files.delete_list.concat(generate_file_list(local_file_info));
result_files.update_list = result_files.update_list.concat(generate_file_list(remote_file_info));
} else if (local_file_info.sha_512 !== remote_file_info.sha_512) {
if (local_file_info.is_dir) {
var visited_children = new Set();
for (var i in local_file_info.children) {
3 years ago
compare_file_info(local_file_info.children[i], remote_file_info.children[i], result_files);
visited_children.add(i);
}
for (var i in remote_file_info.children) {
if (!visited_children.has(i)) {
result_files.update_list = result_files.update_list.concat(
generate_file_list(remote_file_info.children[i])
);
}
}
} else {
3 years ago
result_files.update_list = result_files.update_list.concat(generate_file_list(remote_file_info));
}
}
}
function generate_file_list(file_info) {
if (!file_info) {
return [];
}
var file_list = [];
if (file_info.is_dir) {
for (var i in file_info.children) {
file_list = file_list.concat(generate_file_list(file_info.children[i]));
}
}
3 years ago
file_list.push({
file_name: file_info.file_name,
is_dir: file_info.is_dir,
is_file: file_info.is_file,
sha_512: file_info.sha_512,
3 years ago
});
return file_list;
}
// command: delete, update, finish
function send_command(command, file_list) {
var command_data = {
command: command,
file_list: file_list,
};
postMessage(command_data);
}