August 2024
Intermediate to advanced
516 pages
11h 47m
English
Queues are common in many applications, ranging from printing jobs to background workers in web applications.
Let’s say we’re programming a simple JavaScript interface for a printer that can accept printing jobs from various computers across a network. We want to make sure we print each document in the order in which it was received.
This code uses our implementation of the Queue class from earlier:
| | import Queue from './queue.js'; |
| | |
| | class PrintManager { |
| | constructor() { |
| | this.queue = new Queue(); |
| | } |
| | |
| | queuePrintJob(document) { |
| | this.queue.enqueue(document); |
| | } |
| | |
| | run() { |
| | while (this.queue.read()) { |
| | this.printDocument(this.queue.dequeue()); |
| | } |
| | } |
| | |
| | printDocument(document) { |
| | // Code ... |
Read now
Unlock full access