Integrate worker to offload main thread from heavy tasks
This commit is contained in:
27
wwwroot/core/namecards/nameCardWorker.js
Normal file
27
wwwroot/core/namecards/nameCardWorker.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { parentPort } from 'worker_threads';
|
||||
import {NameCardCreator} from "./nameCardCreator.js";
|
||||
|
||||
parentPort.on("message", async (data) => {
|
||||
try {
|
||||
const { templatePath, avatarURL, username } = data;
|
||||
|
||||
const creator = new NameCardCreator(templatePath);
|
||||
const buffer = await creator.getWelcomeCard(avatarURL, username);
|
||||
|
||||
parentPort.postMessage({ result: buffer });
|
||||
} catch (err) {
|
||||
parentPort.postMessage({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
/*(async () => {
|
||||
try {
|
||||
const { creator, avatarURL, userName } = workerData;
|
||||
|
||||
const buffer = await creator.getWelcomeCard(avatarURL, userName);
|
||||
|
||||
parentPort.postMessage(buffer); // return the buffer to main thread
|
||||
} catch (err) {
|
||||
parentPort.postMessage({ error: err.message });
|
||||
}
|
||||
})();*/
|
||||
Reference in New Issue
Block a user