make filters async
since most of its content is async anyway
This commit is contained in:
		
							parent
							
								
									532295a610
								
							
						
					
					
						commit
						b5c0f7ac68
					
				
					 1 changed files with 94 additions and 94 deletions
				
			
		|  | @ -9,8 +9,10 @@ export default class filters { | ||||||
| 			const secretariat = await import( | 			const secretariat = await import( | ||||||
| 				chrome.runtime.getURL("scripts/secretariat.js") | 				chrome.runtime.getURL("scripts/secretariat.js") | ||||||
| 			); | 			); | ||||||
| 
 | 			 | ||||||
| 			return ((await Promise.all([secretariat.read([`filters`], -1)]))[0]) | 			return(secretariat.read(`filters`, -1).then((filters) => { | ||||||
|  | 				return(filters) | ||||||
|  | 			})); | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -18,7 +20,7 @@ export default class filters { | ||||||
| 
 | 
 | ||||||
| 	@param {string} URL the current URL | 	@param {string} URL the current URL | ||||||
| 	*/ | 	*/ | ||||||
| 	static async select(URL = window.location.href) { | 	async select(URL = window.location.href) { | ||||||
| 		this.one = await (async () => { | 		this.one = await (async () => { | ||||||
| 			// Import the secretariat.
 | 			// Import the secretariat.
 | ||||||
| 			const secretariat = await import( | 			const secretariat = await import( | ||||||
|  | @ -40,109 +42,107 @@ export default class filters { | ||||||
| 	@param {string} URL the URL to update | 	@param {string} URL the URL to update | ||||||
| 	@return {boolean} the state | 	@return {boolean} the state | ||||||
| 	*/ | 	*/ | ||||||
| 	static update(URL) { | 	async update(URL) { | ||||||
| 		this.all = (async () => { | 		// Import the updater.
 | ||||||
| 			// Import the updater.
 | 		const secretariat = await import( | ||||||
| 			const secretariat = await import( | 			chrome.runtime.getURL("scripts/secretariat.js") | ||||||
| 				chrome.runtime.getURL("scripts/secretariat.js") | 		); | ||||||
| 			); | 		const net = await import(chrome.runtime.getURL("scripts/net.js")); | ||||||
| 			const net = await import(chrome.runtime.getURL("scripts/net.js")); | 		const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))) | ||||||
| 			const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))) | 			.default; | ||||||
| 				.default; | 		const alerts = ( | ||||||
| 			const alerts = ( | 			await import(chrome.runtime.getURL("gui/scripts/alerts.js")) | ||||||
| 				await import(chrome.runtime.getURL("gui/scripts/alerts.js")) | 		).default; | ||||||
| 			).default; |  | ||||||
| 
 | 
 | ||||||
| 			// Apparently, JS doesn't have a native queueing system, but it might best work here.
 | 		// Apparently, JS doesn't have a native queueing system, but it might best work here.
 | ||||||
| 			class Queue { | 		class Queue { | ||||||
| 				constructor() { | 			constructor() { | ||||||
| 					this.elements = []; | 				this.elements = []; | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				enqueue(element) { |  | ||||||
| 					this.elements.push(element); |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				dequeue() { |  | ||||||
| 					return this.elements.shift(); |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				isEmpty() { |  | ||||||
| 					return this.elements.length <= 0; |  | ||||||
| 				} |  | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			// Create a queue of the filters.
 | 			enqueue(element) { | ||||||
| 			let filters = new Queue(); | 				this.elements.push(element); | ||||||
| 
 |  | ||||||
| 			if (URL) { |  | ||||||
| 				// Check if the URL is in a valid protocol
 |  | ||||||
| 				if (URL.includes(`://`)) { |  | ||||||
| 					// Append that to the queue.
 |  | ||||||
| 					filters.enqueue(URL); |  | ||||||
| 				} |  | ||||||
| 			} else { |  | ||||||
| 				// Add every item to the queue based on what was loaded first.
 |  | ||||||
| 				if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) { |  | ||||||
| 					Object.keys( |  | ||||||
| 						(await Promise.all([secretariat.read(`filters`, -1)]))[0], |  | ||||||
| 					).every((filter_URL) => { |  | ||||||
| 						if (filter_URL.includes(`://`)) { |  | ||||||
| 							filters.enqueue(filter_URL); |  | ||||||
| 						} |  | ||||||
| 					}); |  | ||||||
| 				} |  | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			if (!filters.isEmpty()) { | 			dequeue() { | ||||||
| 				while (!filters.isEmpty()) { | 				return this.elements.shift(); | ||||||
| 					let filter_URL = filters.dequeue(); | 			} | ||||||
| 
 | 
 | ||||||
| 					// Inform the user of download state.
 | 			isEmpty() { | ||||||
| 					alerts.log( | 				return this.elements.length <= 0; | ||||||
| 						texts.localized(`settings_filters_update_status`, null, [ | 			} | ||||||
| 							filter_URL, | 		} | ||||||
| 						]), |  | ||||||
| 					); |  | ||||||
| 
 | 
 | ||||||
| 					// Create promise of downloading.
 | 		// Create a queue of the filters.
 | ||||||
| 					let filter_download = net.download(filter_URL); | 		let filters = new Queue(); | ||||||
| 					filter_download | 
 | ||||||
| 						.then((result) => { | 		if (URL) { | ||||||
| 							// Only work when the filter is valid.
 | 			// Check if the URL is in a valid protocol
 | ||||||
| 							if (result) { | 			if (URL.includes(`://`)) { | ||||||
| 								// Write the filter to storage.
 | 				// Append that to the queue.
 | ||||||
| 								secretariat.write(["filters", filter_URL], result, -1); | 				filters.enqueue(URL); | ||||||
| 								console.log( | 			} | ||||||
| 									texts.localized( | 		} else { | ||||||
| 										`settings_filters_update_status_complete`, | 			// Add every item to the queue based on what was loaded first.
 | ||||||
| 										null, | 			if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) { | ||||||
| 										[filter_URL], | 				Object.keys( | ||||||
| 									), | 					(await Promise.all([secretariat.read(`filters`, -1)]))[0], | ||||||
| 								); | 				).every((filter_URL) => { | ||||||
| 							} | 					if (filter_URL.includes(`://`)) { | ||||||
| 							 | 						filters.enqueue(filter_URL); | ||||||
| 						}) | 					} | ||||||
| 						.catch((error) => { | 				}); | ||||||
| 							// Inform the user of the download failure.
 | 			} | ||||||
| 							console.log( | 		} | ||||||
|  | 
 | ||||||
|  | 		if (!filters.isEmpty()) { | ||||||
|  | 			while (!filters.isEmpty()) { | ||||||
|  | 				let filter_URL = filters.dequeue(); | ||||||
|  | 
 | ||||||
|  | 				// Inform the user of download state.
 | ||||||
|  | 				alerts.log( | ||||||
|  | 					texts.localized(`settings_filters_update_status`, null, [ | ||||||
|  | 						filter_URL, | ||||||
|  | 					]), | ||||||
|  | 				); | ||||||
|  | 
 | ||||||
|  | 				// Create promise of downloading.
 | ||||||
|  | 				let filter_download = net.download(filter_URL); | ||||||
|  | 				filter_download | ||||||
|  | 					.then((result) => { | ||||||
|  | 						// Only work when the filter is valid.
 | ||||||
|  | 						if (result) { | ||||||
|  | 							// Write the filter to storage.
 | ||||||
|  | 							secretariat.write(["filters", filter_URL], result, -1); | ||||||
|  | 							alerts.log( | ||||||
| 								texts.localized( | 								texts.localized( | ||||||
| 									`settings_filters_update_status_failure`, | 									`settings_filters_update_status_complete`, | ||||||
| 									null, | 									null, | ||||||
| 									[error, filter_URL], | 									[filter_URL], | ||||||
| 								), | 								), | ||||||
| 							); | 							); | ||||||
| 						}); | 						} | ||||||
| 				} | 						 | ||||||
| 			} else { | 					}) | ||||||
| 				// Inform the user of the download being unnecessary.
 | 					.catch((error) => { | ||||||
| 				alerts.warn(texts.localized(`settings_filters_update_stop`)); | 						// Inform the user of the download failure.
 | ||||||
|  | 						alerts.error( | ||||||
|  | 							texts.localized( | ||||||
|  | 								`settings_filters_update_status_failure`, | ||||||
|  | 								null, | ||||||
|  | 								[error, filter_URL], | ||||||
|  | 							), | ||||||
|  | 						); | ||||||
|  | 					}); | ||||||
| 			} | 			} | ||||||
|  | 		} else { | ||||||
|  | 			// Inform the user of the download being unnecessary.
 | ||||||
|  | 			alerts.warn(texts.localized(`settings_filters_update_stop`)); | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 			// Regardless of the download result, update will also mean setting the filters object to whatever is in storage. 
 | 		// Regardless of the download result, update will also mean setting the filters object to whatever is in storage. 
 | ||||||
| 			return(secretariat.read(`filters`, -1).then((filters) => { | 		this.all = (await secretariat.read(`filters`, -1));	 | ||||||
| 				return(filters) | 
 | ||||||
| 			})); | 		return(this.all); | ||||||
| 		})(); |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue