class TokenQueue {
constructor() {
this.queue = [];
this.isRunning = false;
}
enqueue(task) {
this.queue.push(task);
}
start() {
if (this.isRunning) {
return; // Prevent concurrent runs
}
this.isRunning = true;
this.runNext();
}
runNext() {
if (this.queue.length === 0) {
this.isRunning = false;
return;
}
const task = this.queue.shift();
// Simulate token authentication (replace with your actual logic)
const token = task.authenticate();
// Execute the task with the token
task.execute(token);
this.runNext(); // Process the next task
}
}
// Example usage:
// Define a task
class AuthenticationTask {
authenticate() {
// Simulate authentication - replace with your real authentication logic
return "validToken_" + Date.now();
}
execute(token) {
console.log(`Executing task with token: ${token}`);
// Perform the experiment task using the token.
console.log("Task completed.");
}
}
// Create a queue
const queue = new TokenQueue();
// Create tasks and enqueue them
const task1 = new AuthenticationTask();
const task2 = new AuthenticationTask();
const task3 = new AuthenticationTask();
queue.enqueue(task1);
queue.enqueue(task2);
queue.enqueue(task3);
// Start the queue
queue.start();
Add your comment