forked from cory/tildefriends
		
	Faster activity load through concurrency.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4445 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		| @@ -63,6 +63,24 @@ class GgAppElement extends LitElement { | ||||
| 		await this.get_activities_from_ssb(); | ||||
| 	} | ||||
|  | ||||
| 	/* https://gist.github.com/jcouyang/632709f30e12a7879a73e9e132c0d56b?permalink_comment_id=3591045#gistcomment-3591045 */ | ||||
| 	async promise_all(promises, max_concurrent) { | ||||
| 		let index = 0; | ||||
| 		let results = []; | ||||
| 		async function exec_thread() { | ||||
| 			while (index < promises.length) { | ||||
| 				const current = index++; | ||||
| 				results[current] = await promises[current]; | ||||
| 			} | ||||
| 		} | ||||
| 		const threads = []; | ||||
| 		for (let thread = 0; thread < max_concurrent; thread++) { | ||||
| 			threads.push(exec_thread()); | ||||
| 		} | ||||
| 		await Promise.all(threads); | ||||
| 		return results; | ||||
| 	} | ||||
|  | ||||
| 	async get_activities_from_ssb() { | ||||
| 		this.status = {text: 'loading activities'}; | ||||
| 		this.loaded_activities = []; | ||||
| @@ -75,9 +93,10 @@ class GgAppElement extends LitElement { | ||||
| 			json_extract(mention.value, '$.name') = 'activity_data' | ||||
| 			ORDER BY messages.timestamp DESC | ||||
| 		`, []); | ||||
| 		for (let [index, row] of blob_ids.entries()) { | ||||
| 			this.status = {text: 'loading activity data', value: index, max: blob_ids.length}; | ||||
| 			let blob = await tfrpc.rpc.get_blob(row.blob_id); | ||||
| 		this.status = {text: 'loading activity data'}; | ||||
| 		let blobs = await this.promise_all(blob_ids.map(x => tfrpc.rpc.get_blob(x.blob_id)), 8); | ||||
| 		this.status = {text: 'processing activity data'}; | ||||
| 		for (let blob of blobs) { | ||||
| 			try { | ||||
| 				this.loaded_activities.push(JSON.parse(blob)); | ||||
| 			} catch { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user