~philipwhite/nix-package-search

e0f708bc425e077daa270414061827e222645742 — Philip White 2 years ago b5a93ef
Improve performance by reusing previous search when possible
1 files changed, 51 insertions(+), 50 deletions(-)

M index.js
M index.js => index.js +51 -50
@@ 10,6 10,7 @@ let channel_data = {};
// work should happen in the background; when it is finished it should update
// this variable and refresh the package list.
let current_packages = {};
let previous_search = "";
let current_expansion = null;

let header_row = document.createElement("tr");


@@ 106,10 107,19 @@ function insert_details(package_item, even_or_odd_class, package, package_key) {

function refresh_list() {
	if (channel_data !== null) {
		let prefiltered_channel_data = channel_data;
		if (filter_input.value.includes(previous_search)) {
			prefiltered_channel_data = current_packages;
		}
		previous_search = filter_input.value;
		current_packages = {};
		let package_count = 0;
		for (let package_key in channel_data) {
			let package = channel_data[package_key];

		packages_list.innerHTML = "";
		packages_list.appendChild(header_row);

		let index = 0;
		for (let package_key in prefiltered_channel_data) {
			let package = prefiltered_channel_data[package_key];
			let filter_items = filter_input.value.toLowerCase().split(" ");
			let passed = true;
			for (let filter_item of filter_items) {


@@ 127,40 137,34 @@ function refresh_list() {
			}
			if (passed) {
				current_packages[package_key] = package;
				package_count++;
			}
			if (package_count > 100) {
				break;
			}
		}
		packages_list.innerHTML = "";
		packages_list.appendChild(header_row);
		let index = 0;
		for (let package_key in current_packages) {
			let package = current_packages[package_key];
			let package_div = document.createElement("tr");
			package_div.classList.add("result");
			let even_or_odd_class = "odd";
			if (index % 2 === 0) {
				even_or_odd_class = "even";
			}
			package_div.classList.add(even_or_odd_class);
			let package_name_div = document.createElement("td");
			let package_key_div = document.createElement("td");
			let package_description_div = document.createElement("td");
			package_name_div.innerHTML = package.name;
			package_key_div.innerHTML = package_key;
			if (package.meta.description) {
				package_description_div.innerHTML = package.meta.description;

				if (index < 100) {
					let package = prefiltered_channel_data[package_key];
					let package_div = document.createElement("tr");
					package_div.classList.add("result");
					let even_or_odd_class = "odd";
					if (index % 2 === 0) {
						even_or_odd_class = "even";
					}
					package_div.classList.add(even_or_odd_class);
					let package_name_div = document.createElement("td");
					let package_key_div = document.createElement("td");
					let package_description_div = document.createElement("td");
					package_name_div.innerHTML = package.name;
					package_key_div.innerHTML = package_key;
					if (package.meta.description) {
						package_description_div.innerHTML = package.meta.description;
					}
					package_div.appendChild(package_name_div);
					package_div.appendChild(package_key_div);
					package_div.appendChild(package_description_div);
					packages_list.appendChild(package_div);
					package_div.addEventListener("click", function() {
						insert_details(package_div, even_or_odd_class, package, package_key);
					});
				}
				index++;
			}
			package_div.appendChild(package_name_div);
			package_div.appendChild(package_key_div);
			package_div.appendChild(package_description_div);
			packages_list.appendChild(package_div);
			package_div.addEventListener("click", function() {
				insert_details(package_div, even_or_odd_class, package, package_key);
			});
			index++;
		}
	}
}


@@ 170,18 174,15 @@ filter_input.addEventListener("keyup", function() {
	refresh_list();
});

for (let i of [0, 1, 2, 3, 4]) {
	let request = new XMLHttpRequest();
	request.onreadystatechange = function() {
		if (request.readyState === 4 && request.status === 200) {
			let json_data = JSON.parse(request.responseText);
			for (let package_key in json_data) {
				channel_data[package_key] = json_data[package_key];
			}
			refresh_list();
		}
	};

	request.open("GET", `packages.${i}.json`);
	request.send(null);
async function fetch_stuff() {
	for (let i of [0, 1, 2, 3, 4]) {
		let response = await fetch(`packages.${i}.json`);
		let data = await response.json();
		console.log(data);
		Object.assign(channel_data, data);
		current_packages = channel_data;
		refresh_list();
	}
}

fetch_stuff();