Skip to content

Commit

Permalink
008Q better queue (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega authored Nov 23, 2023
1 parent 843877f commit 07e4436
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions packages/008/src/008QWorker.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import { tts } from './008Q';

let BUSY = false;
const QUEUE = [];
import PQueue from 'p-queue';

const process = async () => {
// TODO: put back busy check once error handling is fixed
if (!QUEUE.length) return;
import { tts } from './008Q';

try {
BUSY = true;
const QUEUE = new PQueue({ concurrency: 2 });

const [data] = QUEUE;
self.addEventListener('message', async ({ data }) => {
console.log(`[008Q] Queuing job ${data.id}`);
QUEUE.add(async () => {
const { id, audio } = data;

console.log('[008Q] Transcribing...');
const transcript = await tts({ audio });

self.postMessage({ id, transcript });
} catch (err) {
console.error(err);
} finally {
QUEUE.shift();
BUSY = false;
process();
}
};

self.addEventListener('message', async ({ data }) => {
console.log(`[008Q] Queuing job ${data.id}`);
QUEUE.push(data);
process();
});
});

0 comments on commit 07e4436

Please sign in to comment.