diff --git a/apps/gg/script.js b/apps/gg/script.js index d103c989..cecacded 100644 --- a/apps/gg/script.js +++ b/apps/gg/script.js @@ -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 {