~philipwhite/nix-package-search

610c04a981274586f7a795d4f73da1f610afacd0 — Philip White 2 years ago 6801026 master
Forgot to add worker.js
1 files changed, 73 insertions(+), 0 deletions(-)

A worker.js
A worker.js => worker.js +73 -0
@@ 0,0 1,73 @@
let ENABLE_WEB_WORKERS = false;
let channel_data = {};
let current_packages = {};
let previous_search = "";

window = this;
if (!window.Worker || !window.document || !ENABLE_WEB_WORKERS) {
	fetch(`packages.small.json`)
		.then(response => response.json())
		.then(data => {
			channel_data = data.packages;
			current_packages = channel_data;
			console.log(`searching ${previous_search}`);
			self.onmessage({ data: previous_search });
			fetch(`packages.json`)
				.then(response => response.json())
				.then(data => {
					channel_data = data.packages;
					current_packages = channel_data;
					console.log(`searching ${previous_search}`);
					self.onmessage({ data: previous_search });
				});
		});
}

self.onmessage = function(e) {
	let current_search = e.data;
	console.log(current_search);
	let prefiltered_channel_data = channel_data;
	if (current_search.includes(previous_search)) {
		prefiltered_channel_data = current_packages;
	}
	previous_search = current_search;

	if (channel_data !== null) {
		current_packages = {};

		let index = 0;
		let stop = false;
		for (let package_key in prefiltered_channel_data) {
			let package = prefiltered_channel_data[package_key];
			let search_items = current_search.toLowerCase().split(" ");
			let passed = true;
			for (let search_item of search_items) {
				if (
					!(
						package.name.toLowerCase().includes(search_item) ||
						package_key.toLowerCase().includes(search_item) ||
						(package.meta.description &&
							package.meta.description.toLowerCase().includes(search_item)) ||
						(package.meta.longDescripion &&
							package.meta.longDescription.toLowerCase().includes(search_item))
					)
				) {
					passed = false;
					break;
				}
			}
			if (passed) {
				current_packages[package_key] = package;

				if (!stop && index > 100) {
					self.postMessage(current_packages);
					stop = true;
				}
				index++;
			}
		}
		if (!stop) {
			postMessage(current_packages);
		}
	}
};