mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			36 lines
		
	
	
		
			860 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			860 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| /* IMPORT */
 | |
| Object.defineProperty(exports, "__esModule", { value: true });
 | |
| /* VARIABLES */
 | |
| const Queues = {};
 | |
| /* SCHEDULER */
 | |
| //TODO: Maybe publish this as a standalone package
 | |
| const Scheduler = {
 | |
|     next: (id) => {
 | |
|         const queue = Queues[id];
 | |
|         if (!queue)
 | |
|             return;
 | |
|         queue.shift();
 | |
|         const job = queue[0];
 | |
|         if (job) {
 | |
|             job(() => Scheduler.next(id));
 | |
|         }
 | |
|         else {
 | |
|             delete Queues[id];
 | |
|         }
 | |
|     },
 | |
|     schedule: (id) => {
 | |
|         return new Promise(resolve => {
 | |
|             let queue = Queues[id];
 | |
|             if (!queue)
 | |
|                 queue = Queues[id] = [];
 | |
|             queue.push(resolve);
 | |
|             if (queue.length > 1)
 | |
|                 return;
 | |
|             resolve(() => Scheduler.next(id));
 | |
|         });
 | |
|     }
 | |
| };
 | |
| /* EXPORT */
 | |
| exports.default = Scheduler;
 |