|
| 1 | +import { Synchronizer } from "../synchronizer.interface"; |
| 2 | + |
| 3 | +export class ProducerConsumer<T> implements Synchronizer { |
| 4 | + /** |
| 5 | + * The data available for instant reading |
| 6 | + */ |
| 7 | + private readonly items: T[]; |
| 8 | + |
| 9 | + /** |
| 10 | + * @returns the number of waiting "Threads" on any read |
| 11 | + */ |
| 12 | + public get queueLength(): number { |
| 13 | + throw new Error("Not implemented yet"); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a ProducerConsumer |
| 18 | + * |
| 19 | + * @param initialItems the initial available values |
| 20 | + */ |
| 21 | + public constructor(initialItems: readonly T[] = []) { |
| 22 | + this.items = initialItems.slice(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Reads a given number of items: |
| 27 | + * Wait until the given number of items are available. |
| 28 | + * |
| 29 | + * @param nItem the number of items to read |
| 30 | + * @throws {ProducerConsumerInvalidReadParameterException} when the number of items to read is invalid |
| 31 | + * @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted |
| 32 | + * @returns a promise with the read results |
| 33 | + */ |
| 34 | + public read(nItem: number): Promise<T[]> { |
| 35 | + throw new Error("Not implemented yet"); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Reads one item: |
| 40 | + * Wait until the item is available. |
| 41 | + * |
| 42 | + * @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted |
| 43 | + * @returns a promise with the read results |
| 44 | + */ |
| 45 | + public readOne(): Promise<T> { |
| 46 | + return this.read(1).then(([data]) => data); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Reads a given number of items: |
| 51 | + * Wait until the given number of items are available. |
| 52 | + * |
| 53 | + * Throws an error if the given time exceeds |
| 54 | + * and re-establish the state as if the method was not called. |
| 55 | + * |
| 56 | + * @param timeout maximum time (in ms) to read the items |
| 57 | + * @param nItem the number of items to read |
| 58 | + * @throws {ProducerConsumerInvalidReadParameterException} when the number of items to read is invalid |
| 59 | + * @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted |
| 60 | + * @returns a promise with the read results |
| 61 | + */ |
| 62 | + public tryRead(timeout: number, nItem: number): Promise<T[]> { |
| 63 | + throw new Error("Not implemented yet"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Reads one item: |
| 68 | + * Wait until the item is available. |
| 69 | + * |
| 70 | + * Throws an error if the given time exceeds |
| 71 | + * and re-establish the state as if the method was not called. |
| 72 | + * |
| 73 | + * @param timeout maximum time (in ms) to read the items |
| 74 | + * @throws {ConcurrencyInterruptedException} when the producer-consumer is interrupted |
| 75 | + * @returns a promise with the read result |
| 76 | + */ |
| 77 | + public tryReadOne(timeout: number): Promise<T> { |
| 78 | + return this.tryRead(timeout, 1).then(([data]) => data); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Write some items to the producer-consumer buffer. |
| 83 | + * It releases the await readings or store the data. |
| 84 | + * |
| 85 | + * @param items the items to write |
| 86 | + */ |
| 87 | + public write(items: T | T[]) { |
| 88 | + if (!Array.isArray(items)) { |
| 89 | + items = [items]; |
| 90 | + } |
| 91 | + |
| 92 | + throw new Error("Not implemented yet"); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Interrupts all awaiting "threads" with an [exception]{@link ConcurrencyInterruptedException}. |
| 97 | + * |
| 98 | + * @param reason The reason why this producer-consumer is being interrupted |
| 99 | + * @param items the items to set once everything has been interrupted |
| 100 | + */ |
| 101 | + public interrupt(reason: unknown, items: readonly T[] = []) { |
| 102 | + throw new Error("Not implemented yet"); |
| 103 | + } |
| 104 | +} |
0 commit comments