Recover WebWorkers

This commit is contained in:
KoalaSat 2024-03-28 23:29:27 +01:00 committed by Reckless_Satoshi
parent d8490d7530
commit 383112dc90
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
3 changed files with 20 additions and 26 deletions

View File

@ -26,10 +26,9 @@ class RoboGenerator {
const numCores = 8;
for (let i = 0; i < numCores; i++) {
// FIXME
// const worker = new Worker(new URL('./robohash.worker.ts', import.meta.url));
// worker.onmessage = this.assignTasksToWorkers.bind(this);
// this.workers.push({ worker, busy: false });
const worker = new Worker(new URL('./robohash.worker.ts', import.meta.url));
worker.onmessage = this.assignTasksToWorkers.bind(this);
this.workers.push({ worker, busy: false });
}
}
@ -82,8 +81,6 @@ class RoboGenerator {
hash,
size,
) => {
// FIXME
return '';
const cacheKey = `${size}px;${hash}`;
if (this.assetsCache[cacheKey]) {
return this.assetsCache[cacheKey];

View File

@ -1,18 +1,17 @@
// FIXME
// import { async_generate_robohash } from 'robo-identities-wasm';
import { async_generate_robohash } from 'robo-identities-wasm';
// // Listen for messages from the main thread
// self.addEventListener('message', (event) => {
// void (async () => {
// const { hash, size, cacheKey } = event.data;
// Listen for messages from the main thread
self.addEventListener('message', (event) => {
void (async () => {
const { hash, size, cacheKey } = event.data;
// // Generate the image using async_image_base
// const t0 = performance.now();
// const avatarB64: string = await async_generate_robohash(hash, size === 'small' ? 80 : 256);
// const imageUrl = `data:image/png;base64,${avatarB64}`;
// const t1 = performance.now();
// console.log(`Avatar generated in: ${t1 - t0} ms`);
// // Send the result back to the main thread
// self.postMessage({ cacheKey, imageUrl });
// })();
// });
// Generate the image using async_image_base
const t0 = performance.now();
const avatarB64: string = await async_generate_robohash(hash, size === 'small' ? 80 : 256);
const imageUrl = `data:image/png;base64,${avatarB64}`;
const t1 = performance.now();
console.log(`Avatar generated in: ${t1 - t0} ms`);
// Send the result back to the main thread
self.postMessage({ cacheKey, imageUrl });
})();
});

View File

@ -1,16 +1,14 @@
import { sha256 } from 'js-sha256';
import { Robot, type Order } from '.';
import { robohash } from '../components/RobotAvatar/RobohashGenerator';
// import { generate_roboname } from 'robo-identities-wasm';
import { generate_roboname } from 'robo-identities-wasm';
class Slot {
constructor(token: string, shortAliases: string[], robotAttributes: Record<any, any>) {
this.token = token;
this.hashId = sha256(sha256(this.token));
// FIXME
// this.nickname = generate_roboname(this.hashId);
this.nickname = 'Robot';
this.nickname = generate_roboname(this.hashId);
// trigger RoboHash avatar generation in webworker and store in RoboHash class cache.
void robohash.generate(this.hashId, 'small');
void robohash.generate(this.hashId, 'large');